Results 1 to 14 of 14

Thread: PHP session variable help

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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

Posting Permissions

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