PHP session variable problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maingroup
    New Member
    • Dec 2007
    • 2

    PHP session variable problem

    Hello,

    I am using PHP 5, Apache on WinXP.

    Here is the first php named 1.php

    <?php
    session_start() ;
    $_SESSION[0] = 22 ;
    echo $_SESSION[0];
    ?>
    <a href="http://localhost/2.php"> 2php </a>

    Here is second php named 2.php
    <?php
    session_start() ;
    if (isset( $_SESSION[0]))
    {echo 'set';}
    else
    {echo 'not set';}
    ?>

    When I ran http://localhost/1.php, there was no problem at all. When I click on the link, 2.php gives 'not set'. Why is the session variable $_SESSION[0] not passed on for 1.php to 2.php? Please help.
  • arunj82
    New Member
    • Mar 2007
    • 15

    #2
    Hi,
    Dont use 0 inside $_SESSION['']. I think '0' or '1' indicates true or false. This might be the problem. Instead of 0 I used the variable value inside session, now it is displaying the set.


    This code works.

    Code:
    <?php
    session_start();
    $_SESSION["value"] = "Tested" ;
    echo "Session value is ".$_SESSION["value"];
    echo "<BR>";
    ?>
    <a href="http://localhost/two.php"> Two.php</a>
    
    
    <?php
    session_start();
    if (isset( $_SESSION["value"]))
    {echo 'set';}
    else
    {echo 'not set';}
    ?>

    Comment

    • maingroup
      New Member
      • Dec 2007
      • 2

      #3
      Hi,

      Thanks for you help. It still does not work on my system.

      I looked into Cookies in the browser (I use Firefox). Under localhost, it shows localhost PHPSESSID. Session variable of $_SESSION["value"] isn't there. I thought all session variables are stored as cookies along with the session id until the session is destroied.


      By the way, session variable is essentially an array. I believe the syntax of $_SESSION[0] is perfectly legitimate.

      Thanks

      Comment

      • clai83
        New Member
        • Dec 2007
        • 41

        #4
        Originally posted by maingroup
        Hi,

        Thanks for you help. It still does not work on my system.

        I looked into Cookies in the browser (I use Firefox). Under localhost, it shows localhost PHPSESSID. Session variable of $_SESSION["value"] isn't there. I thought all session variables are stored as cookies along with the session id until the session is destroied.


        By the way, session variable is essentially an array. I believe the syntax of $_SESSION[0] is perfectly legitimate.

        Thanks
        You mentioned that session variables are stored as "cookies along with the session id", however, that is not true in your case. The method that you are using I believe would not store the variables in the cookie, rather it would store it in a session file on the server side. The cookie is just to tell the server, which session it should be looking for.

        Here is an example.

        [PHP]
        <?php
        session_start() ;
        $_SESSION['one'] = 1;
        $_SESSION['two'] = 2;
        echo session_id();
        ?>
        [/PHP]

        now if I were to go to my C:/wamp/www/tmp/ [whatever my session id was] file

        and open it in a text editor.

        one|i:1;two|i:2 ;

        The above is stored in that file. There is documentation on session on www.php.net .


        Anyhow, I tried your code, and I also tried putting in numbers like $_SESSION[3] , $_SESSION[4] and so on. They all give the same effect. I'm not an expert, but I think the above post might be right about using numbers are your keys in a superglobal.

        Comment

        Working...