note 7511 deleted from session_id by ronabop
| From: | ronabop@php.net | Date: | Fri, 25 Aug 2000 08:46:28 +0000 |
| Subject: | note 7511 deleted from session_id by ronabop | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-3163@lists.php.net to get a copy of this message | ||
If you've read the newly published SAMS book, "Teach Yourself PHP4 in 24 Hours" by
Matt Zandstra (the other PHP4 books are not yet published), and the documentation on PHP's
web-site, and became as frustrated as I did trying to get "sessions" to work, I hope my
description here will help.
The example on page 340 in SAMS book does not work.
A simplified example (according to the book) is:
Here's the first page:
testsession1.php:
=====================================================
<?php
session_start();
session_register("fname");
session_register("lname");
$fname = "Mickey";
$lname = "Mouse";
print session_id()."<BR>";
print session_encode()."<BR>";
print session_save_path()."<BR>";
?>
=====================================================
Browsing testsession1.php sets the session variables.
Here is what is displayed in Browser (testsession1.php)
6ef4f3c6ff1bab0d7b1f274c2f538c5e
fname|s:6:"Mickey";lname|s:5:"Mouse";
/tmp
Here's the next page:
testsession2.php
=====================================================
<?php
session_start();
print $fname;
print "<BR>";
print $lname;
print session_save_path();
?>
=====================================================
Supposedly, browsing the second page will get and display the session variables. However, it does
not work. A separate session is started.
Here is what is displayed in the browser (testsession2)
/tmp
Looking at the "/tmp" directory on the Linux/Unix server reveals what happened in the
directory:
Here is the result from the first "testsession1.php" page:
[root@www /tmp]# ls -la
total 4
drwxrwxrwt 2 root root 2048 Aug 4 22:41 .
drwxr-xr-x 16 root root 1024 Apr 17 04:16 ..
srwxrwxrwx 1 postgres postgres 0 Jun 1 16:44 .s.PGSQL.5583
srwxrwxrwx 1 root root 0 Jun 12 14:55 mysql.sock
-rw------- 1 httpd httpd 37 Aug 4 22:41 sess_6ef4f3c6ff1bab0d7b1f274c2f538c5e
Here is the result from the second "testsession2.php" page:
[root@www /tmp]# ls -la
total 4
drwxrwxrwt 2 root root 2048 Aug 4 22:44 .
drwxr-xr-x 16 root root 1024 Apr 17 04:16 ..
srwxrwxrwx 1 postgres postgres 0 Jun 1 16:44 .s.PGSQL.5583
srwxrwxrwx 1 root root 0 Jun 12 14:55 mysql.sock
-rw------- 1 httpd httpd 37 Aug 4 22:41 sess_6ef4f3c6ff1bab0d7b1f274c2f538c5e
-rw------- 1 httpd httpd 0 Aug 4 22:44 sess_82a894a3816e83f38268fd36695fccc2
You can see that the second page did not access the original session and its variables. It created
a new session and hence a new session/cookie/file on the server.
*** HERE IS WHAT WORKS *** -- the example on page 345 of SAMS book:
Here is the first page...
testsession1.php:
=====================================================
<?php
session_start();
session_register("fname");
session_register("lname");
$fname = "Mickey";
$lname = "Mouse";
print session_id()."<BR>";
print session_encode()."<BR>";
print session_save_path()."<BR>";
?>
<A HREF="testsession2.php?<? print SID; ?>">Next Page</A>
=====================================================
Here is the second page...
testsession2.php:
=====================================================
<?php
session_start();
print $fname;
print "<BR>";
print $lname;
print "<BR>";
print session_save_path();
?>
=====================================================
When you click on the link to the next page, here is the ADDRESS in the browser:
<WEBSITE_URL>/testsession2.phtml?PHPSESSID=d66198107f6cfaa1dee0f657a286feb2
Here is what is displayed in the browser.....
Mickey
Mouse
/tmp
As you can see, the variable "SID" was magically changed to the variable
"PHPSESSID", and the second page magically opened the session with the session_start()
function.
I hope this helps.