★ wanayoo — archive 1999 http://www.php.net/manual/function.session-id.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Sessions
Quick Reference
German version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
Sessions
* session_start
* session_destroy
* session_name
* session_module_name
* session_save_path
* session_id
* session_register
* session_unregister
* session_unset
* session_is_registered
* session_get_cookie_params
* session_set_cookie_params
* session_decode
* session_encode
Manual: session_id
View the source code for this pageSearch the site



Previous page
 session_save_path
 Updated
Sat, 12 Aug 2000
session_register 
Next page


session_id

(PHP4 )

session_id -- Get and/or set the current session id

Description

string session_id ([string id])

session_id() returns the session id for the current session. If id is specified, it will replace the current session id.

The constant SID can also be used to retrieve the current name and session id as a string suitable for adding to URLs.


User Contributed Notes: session_id


dangitall@hotmail.com
17-Oct-1999 10:03
It would be pretty cool for this function or perhaps some other part of the session management stuff to be able to either specify that we don't want it to even try to set a cookie ...would be great for cases like a SSL connection where there already is a handy dandy session id available ...where one might be tempted to do: session_id($SSL_SESSION_ID)




stefan@digiconcept.net
29-Jun-2000 03:58
Definign a sessionId by your own is a good idea. but theres is one thing you've have to know: sessionID must not contain any whitespace or _,צה ans so on. best way seems to warp a md5() around it.


marius@neogenix.co.za
18-Jul-2000 08:41
Setting the session_id it clears all my old varibles.
How do I set the session id and use all my old varibles?



rayhar@kc.rr.com
05-Aug-2000 06:20
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()."
";
print session_encode()."
";
print session_save_path()."
";

?>
=====================================================


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 "
";
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()."
";
print session_encode()."
";
print session_save_path()."
";


?>

<A HREF=/old?u=http%3A%2F%2Fwww.php.net%2Fmanual%2F%26quot%3Btestsession2.php%3F%26amp%3Blt%3B%3F&y=1999 print SID; ?>">Next Page</A>
=====================================================



Here is the second page...

testsession2.php:
=====================================================
&lt;?php

session_start();

print $fname;
print "
";
print $lname;
print "
";


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.



 About Notes


Previous page
 session_save_path
 Updated
Sat, 12 Aug 2000
session_register 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.