Changes

Jump to: navigation, search

Understanding PHP Sessions

1,046 bytes added, 17:38, 6 June 2007
Creating and Reading PHP Session Variables
The same PHP code to read the value can be used on any page on your server to access the current value of the variable.
 
== Writing PHP Session Data to a File ==
 
Session data only stays active on the web server until it expires or the session is deleted. Once deleted, all the data associated wiht the session is lost. A snapshot of the session data can be taken at any time and written out to a file. Once saved it can be reloaded when required.
 
To save a session state the ''session_encode()'' function is used combined the PHP file I/O functions (see [[PHP, Filesystems and File I/O]] for details of reading and writing files). The ''session_encode()'' function returns an encoded string containing the session data. Once this string has been obtained it can be written to a file:
 
<pre>
<?php
 
$_SESSION['userName'] = 'JohnW';
$_SESSION['emailAddress'] = '[email protected]';
 
$session_data = session_encode(); // Get the session data
 
$filehandle = fopen ('/tmp/session.txt', 'w+'); // open a file write session data
 
fwrite ($filehandle, $session_data); // write the session data to file
 
fclose ($filehandle);
 
?>
</pre>

Navigation menu