www.webdeveloper.com
Results 1 to 4 of 4

Thread: Question regarding naming $_SESSION variables

  1. #1
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582

    Red face Question regarding naming $_SESSION variables

    Hello, everyone.

    There is something that I can do in ColdFusion that I am wondering if the same thing can be done in PHP5.

    In CF, you can name a session variable using dot-syntax that will allow you to "group" session variables under one parent session variable.
    Code:
    <cfset session.groupA.firstName = "Jack">
    <cfset session.groupA.lastName = "Smith">
    <cfset session.groupB.firstName = "John">
    <cfset session.groupB.lastName = "Doe">
    Now, if I wanted to delete JUST the groupB variables, but keep the groupA variables, instead of going through and deleting each and every one of them, I can simply:
    Code:
    <cfset StructDelete(session,"groupB")>
    .. and that will remove all session variables under the groupB key.

    Does PHP have a naming convention for $_SESSION variables that is similar? Something like, maybe, $_SESSION['groupA']['firstName']?

    Thanks,

  2. #2
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Arrays are referenced the way you already know, and deleted using unset:

    PHP Code:
    <?php

        $_SESSION
    ['groupA'] = array(
            
    'firstName' => 'Jack',
            
    'lastName'  => 'Smith'
        
    );
        
    $_SESSION['groupB'] = array(
            
    'firstName' => 'John',
            
    'lastName'  => 'Doe'
        
    );

        echo 
    $_SESSION['groupB']['firstName'];

        unset(
    $_SESSION['groupB']); //Removes groupB

    ?>
    I guess it's similar?

  3. #3
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582

    Thumbs up

    Looks like it is similar, to me, bionoid. I'll give it a shot and let you know.

    Much appreciated!

  4. #4
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Spot on, bionoid! Thanks!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles