note 52474 deleted from ref.session by bjori
| From: | bjori@php.net | Date: | Sat, 30 Dec 2006 16:40:33 +0000 |
| Subject: | note 52474 deleted from ref.session by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-121100@lists.php.net to get a copy of this message | ||
Note Submitter: frank723 AT gmail DOT com
----
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