Results 1 to 14 of 14

Thread: PHP session variable help

  1. #1
    Old school, like an old fool. Flying Mullet's Avatar
    Join Date
    Apr 2003
    Location
    Napping in a peach tree.
    Posts
    19,185
    Articles
    6
    Blog Entries
    7
    Contributions
    • Former Administrator
    • Former Cid's Knight
    • Former Senior Site Staff

    Default PHP session variable help

    I haven't used PHP session variables before, and I'm trying to now without much success.

    From what I've read online, you need to create/access the session at the beginning of your page. Then you register variables with the session and afterwards any page that accesses the session can call these variables.

    So here's my first page's code:
    Code:
       // access/register the session
       session_start();
       // initialize the variable
       $test = "This is a test";
       // register the variable in the session
       session_register ("test");
    Then I submit the first page to a new page, which accesses the session at the beginning:
    Code:
       // access the session
       session_start();
    And then displays the contents of the session variable on the page:
    Code:
          echo "The variable value is :".$test.".";
    But I am getting an empty string on the resulting page, which leads me to assume that it's not finding $test in the session and is treating it as a new, local variable.

    Also, I have seen some examples set the value of the session variable as such:
    Code:
       $HTTP_SESSION_VARS ["test"] = $test;
    in regards to forms, but I'm not sure if I need to do this or what it does, although I know that right now it doesn't help me out at all.

    Any help is appreciated. :yellowkin

    <b>UPDATE:</b>

    Okay, so I found a different example that works, but it doesn't seem as clean. The difference is that ratehr than registering the variable, I set it via the following code:
    Code:
       $_SESSION['test'] = $test;
    And access it in a similar manner:
    Code:
       $test = $_SESSION['test'];
    This works, but I had read that by registering the variables (as in the code in the first part of my post), you only have to have any pages start the session and then they can access the variables directly without any extra code, i.e. you don't have to pull $test from $_SESSION, you can just reference $test directly. This seems a much cleaner approach, but I can't get it to work.
    Last edited by Flying Mullet; 06-06-2006 at 04:13 PM.
    Figaro Castle

  2. #2
    Hypnotising you crono_logical's Avatar
    Join Date
    May 2001
    Location
    Back in Time
    Posts
    9,313
    Contributions
    • Former Administrator
    • Former Cid's Knight

    Default

    It would be interesting to see if there's any plugins for Firefox to let you see all session data and stuff currently stored and allow you to manipulate it, much like you can for cookies
    Problems playing downloaded videos? Try CCCP


  3. #3
    Lives in a zoo Recognized Member Renmiri's Avatar
    Join Date
    Nov 2005
    Location
    Wai out there
    Posts
    6,034
    Contributions
    • Former Site Staff

    Default

    This is a good site to see sintax and examples
    http://us3.php.net/manual/en/functio...ecialchars.php
    Me and my kids have dragon eggs:



  4. #4
    Old school, like an old fool. Flying Mullet's Avatar
    Join Date
    Apr 2003
    Location
    Napping in a peach tree.
    Posts
    19,185
    Articles
    6
    Blog Entries
    7
    Contributions
    • Former Administrator
    • Former Cid's Knight
    • Former Senior Site Staff

    Default

    Quote Originally Posted by Renmiri
    This is a good site to see sintax and examples
    http://us3.php.net/manual/en/functio...ecialchars.php
    Figaro Castle

  5. #5
    Lives in a zoo Recognized Member Renmiri's Avatar
    Join Date
    Nov 2005
    Location
    Wai out there
    Posts
    6,034
    Contributions
    • Former Site Staff

    Default

    Oh!

    Just search for it

    http://us3.php.net/manual-lookup.php...iables&lang=en

    This guy had a note about what may have caused your problem

    Make sure you put session_start() at the beggining of your script.

    My sessions kept unsetting and I finally figured out why.
    http://us3.php.net/manual/en/functio...n-register.php
    Me and my kids have dragon eggs:



  6. #6
    Prinny God Recognized Member Endless's Avatar
    Join Date
    Aug 2000
    Location
    Prinny Moon
    Posts
    2,641
    Contributions
    • Former Cid's Knight

    And then there is Death

  7. #7
    Ominous Wanderer Tech Admin Samuraid's Avatar
    Join Date
    Oct 2001
    Posts
    5,522

    Default

    I'm not sure specifically what the issue is from a quick read of your first post, but I have a few comments and ideas for you.

    First, don't use $HTTP_SESSION_VARS (which is deprecated), use $_SESSION.

    Secondly, it sounds like you have register_globals on, BAD idea. It's a bad (and often disabled) feature of PHP.
    http://us2.php.net/register_globals

    I would suggest as the cleanest approach, always set your session variables with:
    Code:
    $_SESSION['name'] = value;
    and always read the variables as
    Code:
    $name = $_SESSION['name'];
    and always use a session_write_close(); when you are done manipulating the session data instead of letting PHP do so automatically when the script terminates.

    you only have to have any pages start the session and then they can access the variables directly without any extra code, i.e. you don't have to pull $test from $_SESSION, you can just reference $test directly.
    You can only do this if register_globals is on. And it's a bad idea because if the session is not actually started, then anyone can inject a value for $test as GET data in the query string, which is great example why register_globals should never be used.

  8. #8
    Old school, like an old fool. Flying Mullet's Avatar
    Join Date
    Apr 2003
    Location
    Napping in a peach tree.
    Posts
    19,185
    Articles
    6
    Blog Entries
    7
    Contributions
    • Former Administrator
    • Former Cid's Knight
    • Former Senior Site Staff

    Default

    Quote Originally Posted by Renmiri
    Oh!

    Just search for it

    http://us3.php.net/manual-lookup.php...iables&lang=en

    This guy had a note about what may have caused your problem

    Make sure you put session_start() at the beggining of your script.

    My sessions kept unsetting and I finally figured out why.
    http://us3.php.net/manual/en/functio...n-register.php
    You mean like I have session_start() at the beginning of each script?
    Quote Originally Posted by Endless
    I misunderstood the manual on session_start() and thought it called session_register(), not the other way around. :P

    And Samuraid, I didn't realize that being able to access the variables through the anywhere approach was because they were global, I figured that session_start()/register() did the dirty work under the scenes of accessing everything from the session. I'll leave it as I have it now using $_SESSION. Thanks
    Figaro Castle

  9. #9
    Old school, like an old fool. Flying Mullet's Avatar
    Join Date
    Apr 2003
    Location
    Napping in a peach tree.
    Posts
    19,185
    Articles
    6
    Blog Entries
    7
    Contributions
    • Former Administrator
    • Former Cid's Knight
    • Former Senior Site Staff

    Default

    Okay, I have a new problem.

    I have the following code that certain pages call when they first load:
    Code:
       function doCommonPageLoad()
       {
          session_start();
          $isAuthorized = $_SESSION['isAuthorized'];
          if($isAuthorized == "")
          {
             $_SESSION['isAuthorized'] = "authorized";
             session_write_close();
          }
       }
    The problem is that when I check the value of $_SESSION['isAuthorized'] later, it doesn't exist.

    I read up some more on sessions and session variables and found a couple of others having the same problem here: http://us3.php.net/manual/en/functio...n-register.php
    Two things that they mention:
    1) Make sure you put session_start() at the beggining of your script.
    2) Call session_start() before you write html, because once html has been outputed, session_start() can't use the header function to set cookies, hence session_start() fails and no session can be started.

    So I checked that I'm not writing any html before calling doCommonPageLoad() and I call session_start() at the beginning of it. The documentation says that you are not supposed to use session_register() if you are accessing the session variables with $_SESSION['xxxx'].

    Does anyone have any idea as to why $_SESSION['isAuthorized'] is not being saved in the session?
    Figaro Castle

  10. #10
    Ominous Wanderer Tech Admin Samuraid's Avatar
    Join Date
    Oct 2001
    Posts
    5,522

    Default

    My guess is that $_SESSION may not be available after session_write_close(). I haven't tested this theory yet, but I do know that none of my PHP in the past has tried to use $_SESSION after calling session_write_close().

  11. #11
    Old school, like an old fool. Flying Mullet's Avatar
    Join Date
    Apr 2003
    Location
    Napping in a peach tree.
    Posts
    19,185
    Articles
    6
    Blog Entries
    7
    Contributions
    • Former Administrator
    • Former Cid's Knight
    • Former Senior Site Staff

    Default

    I tried commenting session_write_close() out as well and it still didn't work. I can always try calling it at the end of my page too. Don't know if that would help, but you never know.
    Figaro Castle

  12. #12
    Ominous Wanderer Tech Admin Samuraid's Avatar
    Join Date
    Oct 2001
    Posts
    5,522

    Default

    Before bipper undeletes his post...

    If you are using frames at all, keep this in mind:
    Session data is usually stored after your script terminated without the need to call session_write_close(), but as session data is locked to prevent concurrent writes only one script may operate on a session at any time. When using framesets together with sessions you will experience the frames loading one by one due to this locking. You can reduce the time needed to load all the frames by ending the session as soon as all changes to session variables are done.

  13. #13
    Old school, like an old fool. Flying Mullet's Avatar
    Join Date
    Apr 2003
    Location
    Napping in a peach tree.
    Posts
    19,185
    Articles
    6
    Blog Entries
    7
    Contributions
    • Former Administrator
    • Former Cid's Knight
    • Former Senior Site Staff

    Default

    God no. Frames are the Devil.

    Aww, I missed bipper's post.
    Figaro Castle

  14. #14
    Ominous Wanderer Tech Admin Samuraid's Avatar
    Join Date
    Oct 2001
    Posts
    5,522

    Default

    He said something similar and was going to edit his post to explain further.

Posting Permissions

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