10.8 Encode That Data


You want to save all the current session variables in string format and extract them back into variables at a later point.

Technique

Use the session_encode() and session_decode() functions:

write.php:

 <?php session_register("monkey"); $monkey = array("see", "do"); $string_monkey = session_encode(); $fp = @fopen ("save_monkey.txt", "w")               or die ("Cannot open save_monkey.txt"); @fwrite ($fp, $string_monkey); @fclose ($fp)     or die ("Cannot close save_monkey.txt"); print "Actions Written"; ?> 

read.php:

 <?php $fp = @fopen("save_monkey.txt", "r")              or die("Cannot open save_monkey.txt"); $data = fread($fp, filesize ($fp)); @fclose ($fp)      or die ("Cannot close save_monkey.txt"); session_decode($data); foreach ($monkey as $action) {     print "$action\n<br>\n"; } ?> 

Description

The session_encode() function takes all the current session information and encodes it in to a string that can be parsed by the session_decode() function. The session_decode() function then parses that string and creates the session variables from the information contained within that string.



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net