Forgive my ignorance, but I am just starting to learn about sessions in
PHP and how to pass data from one page to the next. What I'm about to
explain could just be my misunderstandin g of how sessions work.
I have two test scripts. The first starts a session, displays a few
details and a form. When the form is submitted it jumps to the second
page. The first script looks like this.
<?php
session_start() ;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Sessio n test : Page 1</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
/>
</head>
<body>
<ul>
<?php
$_SESSION['session_var'] = "testing";
echo "<li>Your session id is ".session_id(). "</li>\n";
echo "<li>The SESSION array contains<br />\n";
print_r($_SESSI ON);
echo "</li>\n";
echo "<li>The SID is ".SID."</li>\n";
echo "<li>PHPSES SID = ".$PHPSESSI D."</li>\n";
?>
</ul>
This is the test of the sessions feature
<form action="session 2.php" method="post">
<input type="hidden" name="form_var" value="testing" />
<input type="submit" value="Go to next page" />
</form>
</body>
</html>
The second script displays the same variables as the first. In addition
it dislpays the value of a variable passed by the _$SESSION array and a
value passed via a form. It looks like this.
<?php
session_start() ;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Sessio n test : Page 2</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
/>
</head>
<body>
<ul>
<?php
echo "<li>Your session id is ".session_id(). "</li>\n";
echo "<li>The SESSION array contains<br />\n";
print_r($_SESSI ON);
echo "</li>\n";
echo "<li>The SID is ".SID."</li>\n";
echo "<li>PHPSES SID = ".$PHPSESSI D."</li>\n";
echo "<li>session_va r = {".$_SESSION['session_var']."}</li>\n";
echo "<li>form_v ar = {".$_POST['form_var']."}</li>\n";
?>
</ul>
</body>
</html>
When run under IE6, I get the following output.
Script 1
· Your session id is d7e201df8eb6ac9 fd76915e5305166 6d
· The SESSION array contains
Array ( [session_var] =testing )
· The SID is PHPSESSID=d7e20 1df8eb6ac9fd769 15e53051666d
· PHPSESSID =
Script 2
· Your session id is 40211048ab6869c 74dc8e9dff6098d c0
· The SESSION array contains
Array ( )
· The SID is PHPSESSID=40211 048ab6869c74dc8 e9dff6098dc0
· PHPSESSID =
· session_var = {}
· form_var = {testing}
Note how the session id is different and the variable passed by the
_$SESSION array is blank.
When run under Firefox 2.0 I get the following;
· Your session id is 00802b0742fdb87 cda553cba85027c 30
· The SESSION array contains
Array ( [session_var] =testing )
· The SID is PHPSESSID=00802 b0742fdb87cda55 3cba85027c30
· PHPSESSID =
and
· Your session id is 00802b0742fdb87 cda553cba85027c 30
· The SESSION array contains
Array ( [session_var] =testing )
· The SID is
· PHPSESSID =
· session_var = {testing}
· form_var = {testing}
This time, the session ids are the same and the variable is passed.
Any ideas why the difference?
With Firefox, why is SID populated on the first page and not the
second?
Why is PHPSESSID blank?
In case it helps, the values from the php.ini file for session are;
session.auto_st art = 0
session.cache_e xpire = 180
session.cache_l imiter = nocache
session.cookie_ domain =
session.cookie_ lifetime = 0
session.cookie_ path = /
session.entropy _file =
session.entropy _length = 0
session.gc_maxl ifetime = 1440
session.gc_prob ability = 1
session.name = PHPSESSID
session.referer _check =
session.save_ha ndler = files
session.save_pa th = /tmp
session.seriali ze_handler = php
session.use_coo kies = 1
session.use_tra ns_sid = 1
Regards
Steve Wright
PHP and how to pass data from one page to the next. What I'm about to
explain could just be my misunderstandin g of how sessions work.
I have two test scripts. The first starts a session, displays a few
details and a form. When the form is submitted it jumps to the second
page. The first script looks like this.
<?php
session_start() ;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Sessio n test : Page 1</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
/>
</head>
<body>
<ul>
<?php
$_SESSION['session_var'] = "testing";
echo "<li>Your session id is ".session_id(). "</li>\n";
echo "<li>The SESSION array contains<br />\n";
print_r($_SESSI ON);
echo "</li>\n";
echo "<li>The SID is ".SID."</li>\n";
echo "<li>PHPSES SID = ".$PHPSESSI D."</li>\n";
?>
</ul>
This is the test of the sessions feature
<form action="session 2.php" method="post">
<input type="hidden" name="form_var" value="testing" />
<input type="submit" value="Go to next page" />
</form>
</body>
</html>
The second script displays the same variables as the first. In addition
it dislpays the value of a variable passed by the _$SESSION array and a
value passed via a form. It looks like this.
<?php
session_start() ;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Sessio n test : Page 2</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
/>
</head>
<body>
<ul>
<?php
echo "<li>Your session id is ".session_id(). "</li>\n";
echo "<li>The SESSION array contains<br />\n";
print_r($_SESSI ON);
echo "</li>\n";
echo "<li>The SID is ".SID."</li>\n";
echo "<li>PHPSES SID = ".$PHPSESSI D."</li>\n";
echo "<li>session_va r = {".$_SESSION['session_var']."}</li>\n";
echo "<li>form_v ar = {".$_POST['form_var']."}</li>\n";
?>
</ul>
</body>
</html>
When run under IE6, I get the following output.
Script 1
· Your session id is d7e201df8eb6ac9 fd76915e5305166 6d
· The SESSION array contains
Array ( [session_var] =testing )
· The SID is PHPSESSID=d7e20 1df8eb6ac9fd769 15e53051666d
· PHPSESSID =
Script 2
· Your session id is 40211048ab6869c 74dc8e9dff6098d c0
· The SESSION array contains
Array ( )
· The SID is PHPSESSID=40211 048ab6869c74dc8 e9dff6098dc0
· PHPSESSID =
· session_var = {}
· form_var = {testing}
Note how the session id is different and the variable passed by the
_$SESSION array is blank.
When run under Firefox 2.0 I get the following;
· Your session id is 00802b0742fdb87 cda553cba85027c 30
· The SESSION array contains
Array ( [session_var] =testing )
· The SID is PHPSESSID=00802 b0742fdb87cda55 3cba85027c30
· PHPSESSID =
and
· Your session id is 00802b0742fdb87 cda553cba85027c 30
· The SESSION array contains
Array ( [session_var] =testing )
· The SID is
· PHPSESSID =
· session_var = {testing}
· form_var = {testing}
This time, the session ids are the same and the variable is passed.
Any ideas why the difference?
With Firefox, why is SID populated on the first page and not the
second?
Why is PHPSESSID blank?
In case it helps, the values from the php.ini file for session are;
session.auto_st art = 0
session.cache_e xpire = 180
session.cache_l imiter = nocache
session.cookie_ domain =
session.cookie_ lifetime = 0
session.cookie_ path = /
session.entropy _file =
session.entropy _length = 0
session.gc_maxl ifetime = 1440
session.gc_prob ability = 1
session.name = PHPSESSID
session.referer _check =
session.save_ha ndler = files
session.save_pa th = /tmp
session.seriali ze_handler = php
session.use_coo kies = 1
session.use_tra ns_sid = 1
Regards
Steve Wright
Comment