Connecting Tech Pros Worldwide Forums | Help | Site Map

PHP session variable problem

Newbie
 
Join Date: Dec 2007
Posts: 2
#1: Dec 5 '07
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.

Newbie
 
Join Date: Mar 2007
Posts: 16
#2: Dec 6 '07

re: PHP session variable problem


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.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. $_SESSION["value"] = "Tested" ;
  4. echo "Session value is ".$_SESSION["value"];
  5. echo "<BR>";
  6. ?>
  7. <a href="http://localhost/two.php"> Two.php</a>
  8.  
  9.  
  10. <?php
  11. session_start();
  12. if (isset( $_SESSION["value"]))
  13. {echo 'set';}
  14. else
  15. {echo 'not set';}
  16. ?>
Newbie
 
Join Date: Dec 2007
Posts: 2
#3: Dec 6 '07

re: PHP session variable problem


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
Member
 
Join Date: Dec 2007
Posts: 37
#4: Dec 7 '07

re: PHP session variable problem


Quote:

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.
Reply