note 18296 added to ref.session
| From: | luca-php4 at luca dot 2y dot net | Date: | Thu, 17 Jan 2002 15:30:04 +0000 |
| Subject: | note 18296 added to ref.session | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-24979@lists.php.net to get a copy of this message | ||
Here's a simple way to have Embperl like sessions. This consist in a "special"
variable (a hash) wich is persistent and holds all the persistent data. In this case the easyest way
is to take directly the $HTTP_SESSION_VARS variable, and add a prepend.inc and append.inc files to
automaticly register and unregister variables like this:
---- prepend.inc ----
<?php
# In case you don't use sessions auto start
session_start();
foreach ( $HTTP_SESSION_VARS as $var => $val )
$OLD_SESSION_VARS[] = $var;
?>
---- append.inc ----
<?php
foreach ( $OLD_SESSION_VARS as $var )
if ( !isset( $HTTP_SESSION_VARS[$var] ) )
session_unregister( $var );
foreach ( $HTTP_SESSION_VARS as $var => $val )
session_register( $var );
----------
The add this files to your php.ini as auto_prepend and auto_append, and that's it...
All you have to do to use a persistent variable is to use it in the $HTTP_SESSION_VARS hash. Try
this script out with your new prepend and append files:
---- test.php ----
<?= $HTTP_SESSION_VARS["counter"]++ ?>
------------------
and refresh it several times.
To unregister a variable, just unset() it, like <? unset( $HTTP_SESSION_VARS["counter"]
); ?>
Hope it helps! :)
--
http://www.php.net/manual/en/ref.session.php
http://master.php.net/manage/user-notes.php?action=edit+18296
http://master.php.net/manage/user-notes.php?action=delete+18296
http://master.php.net/manage/user-notes.php?action=reject+18296