Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Session Array

  1. #1
    New Coder
    Join Date
    Jan 2006
    Posts
    25
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Session Array

    General Qstn;

    Is it best practice when dealing wit lots of session variables. to.

    Insert them in array as such, because i cant see what advantage it would b.

    $_SESSION['serv'] = array();
    $_SESSION['date'] = $_POST['date'];
    $_SESSION['service'] = $_POST['service'];

  • #2
    Regular Coder Tyree's Avatar
    Join Date
    Sep 2003
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What you've shown there is exactly what I do. Whether it's "best practice" or not is open to opinion, I guess.
    Matt Tyree
    TyreeOnline
    If I didn't just "make it worse," show me some love! Hit me with the rep points! :) (The white scales icon on the left)

  • #3
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Well, in your example you haven't actually done anything different, you declared $_SESSION['serv'] as an array, but you never use it after. Maybe you meant:
    PHP Code:
    $_SESSION['serv'] = array();
    $_SESSION['serv']['date'] = $_POST['date'];
    $_SESSION['serv']['service'] = $_POST['service']; 
    if so, I can see advantages of this, quick examples that pop into my head are when dealing with shopping carts.
    Last edited by Brandoe85; 12-21-2006 at 02:57 PM.

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    If you meant as per Brandoe85's example then the advantage is that your are giving your variables a `namespace` of sorts , its really easy to end up with lots of session variables with names that may confuse ..
    e.g.

    $_SESSION['num']

    Is that the number of items in a cart or the number of login attempts or ..?
    OK you can use $_SESSION['cart_num'] etc but personally I prefer the array access.

    Variable naming conventions can save you hours of headaches for little effort, & I wish I had realised that the first time I was told so !
    Last edited by firepages; 12-21-2006 at 03:12 PM.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •