SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: about sessions

  1. #1
    SitePoint Enthusiast webmasts's Avatar
    Join Date
    Apr 2003
    Location
    usa
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    about sessions

    Does session actually stores the $variable or does it just stores some value you assign it to?

    For example, can the session store the $password and $username submitted by a form or do I have to register some value as 'password' and 'username'?

    Thankx

  2. #2
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    session_start
    ();
    $_SESSION['test'] = 'This is a test of session variables';
    echo 
    '$test = '.$test;
    echo 
    '<br />$_SESSION["test"] = '.$_SESSION['test'];
    $test $_SESSION['test'];
    $anotherTest $_SESSION['test'];
    echo 
    '<br />$test = '.$test;
    echo 
    '<br />$anotherTest = '.$anotherTest;
    ?>
    Output:
    Code:
    Notice: Undefined variable: test in E:\tests\sessionVars.php on line 7
    $test = 
    $_SESSION["test"] = This is a test of session variables
    $test = This is a test of session variables
    $anotherTest = This is a test of session variables
    John

  3. #3
    SitePoint Guru
    Join Date
    Feb 2002
    Posts
    625
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It stores a value that you assign to it.

    For example
    PHP Code:
    $myName 'datune';

    $_SESSION['name'] = $myName;

    // will print datune
    echo $_SESSION['name']; 
    But, you can also serialize objects. For example

    PHP Code:
    class User
    {
        function 
    User()
        {
            
    $this->name 'datune';
        }
      
        function 
    getName()
        {
            return 
    $this->name;
        }
    }
        
    $user = new User();
    $_SESSION['userObject'] = serialize($user);

    //on another page you can do this
    $user =& unserialize($_SESSION['userObject']);

    //will return datune
    echo $user->getName(); 
    Hope this answers your questions

  4. #4
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Datune,

    The "&" in this line:

    $user =& unserialize($_SESSION['user']);

    is a reference to the object?
    John

  5. #5
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe when you store data in the $_SESSION array it is automatically serialized, and passed across pages.

    (I could be wrong, but I think that's how it works)
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  6. #6
    SitePoint Guru
    Join Date
    Feb 2002
    Posts
    625
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PHP John
    Datune,

    The "&" in this line:

    $user =& unserialize($_SESSION['user']);

    is a reference to the object?
    No, it is a reference to the object which is stored in the session.

    I believe when you store data in the $_SESSION array it is automatically serialized, and passed across pages.

    (I could be wrong, but I think that's how it works)
    That is correct, but not if it comes to objects

  7. #7
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok [img]images/smilies/wink.gif[/img]

    but here is an important note [img]images/smilies/wink.gif[/img]

    the class must be defined already in the script (via an include or whatever) before unserializing an object.



    Oh, and about passing object across pages using $_SESSION, It's probably because I am using PHP5, but I don't need to serialize it first.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.


Bookmarks

Posting Permissions

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