note 52474 added to ref.session
| From: | frank723ATgmailDOTcom at osu1 dot php dot net | Date: | Tue, 03 May 2005 02:27:37 +0000 |
| Subject: | note 52474 added to ref.session | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-88881@lists.php.net to get a copy of this message | ||
Here is a script a made to initialize global variables when both GET and SESSION variables are
defined. This is common when SESSION variables retain information that is changed by a GET value.
For example, when a user selects a style, the url is tagged with ?style=STYLETYPEONE, so
$_SESSION['style'] should be set to the get. However, if no get is set then the session
variable should remain what it was. If the session variable isnt set either, then it should be set
to the default value of the variable.
Here is the code:
# a session helpful thing, start a session, with default session values
# if get for that var is set set the var to th get otherwise use session
session_start();
$ses_vars = array('var1'=>var1defaultvalue, # list of passed get vars
'var2'=>var2defaultvalue,
'var3'=>var3defaultvalue);
foreach ($ses_vars as $var=>$default) {
if (!isset($_SESSION[$var])) $_SESSION[$var] = $default;
if (!isset($_GET[$var])) ${$var} = $_SESSION[$var];
else ${$var} = $_GET[$var];
}
-Frank
http://entelekheia.net
----
Manual Page -- http://www.php.net/manual/en/ref.session.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52474
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52474&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52474&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52474&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52474&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52474&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52474&report=yes
Search -- http://master.php.net/manage/user-notes.php