Originally posted by Jeremy Stowell
Hi. I have a question on sessions. I am having trouble getting variables to work with using sessions.
The code I have wrote so far is this.
test.php
<?php
session_start();
?>
<?php
$myvar = "Hello";
$SESSION["myvar"]=$myvar;
echo "session_id();
echo $SESSION["myvar"];
?>
test2.php
<?php
session_start();
?>
<?php
$temp=$_SESSION["myvar"];
echo $temp;
echo session_id();
?>
These are the two web pages that I have. I can not see the variable displayed on the second page. If anyone knows what I am doing wrong, that would be greatly appreciated.
Also I am able to see the session_id on both test and test2 pages. But the variable is unable to be seen!
--Jeremy
Your code looks fine --- there are a couple of minor points, but everything tested OK for me.
In the posted example you have a spare double quote in test1.php prior to the [man]session_id()[/man] call.
Also, standard practice is probably to use the single quote instead of the double when referring to array elements that are strings, i.e. $SESSION['myvar'] rather than $SESSION["myvar"].
Try this as test1.php:
<?php
session_start();
?>
<?php
$myvar = "Hello";
$_SESSION['myvar']=$myvar;
echo session_id()." is the Session ID<br><br>";
echo $_SESSION['myvar']. " is the Variable...";
?>
and this as test2.php:
<?php
session_start();
?>
<?php
$temp=$_SESSION['myvar'];
echo $temp. " is the Variable<br><br>";
echo session_id(). " is the Session ID...";
?>
Otherwise, some problem with the browser (accepting cookies, for example?) or the settings in php.ini (enable-trans-id?)??