note 33746 added to language.variables.predefined
| From: | contact at pulseforce dot com | Date: | Sun, 06 Jul 2003 14:07:43 +0000 |
| Subject: | note 33746 added to language.variables.predefined | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-51583@lists.php.net to get a copy of this message | ||
Globals = Off is advised for newbies, since they are more than likely to use something like:
if ($pass = "admin") { $loggedin = 1; }
if ($loggedin == 1) { show_admin_interface(); }
This is not a threat at all if you know how to code safely, not using variables such as $loggedin.
But what if us advanced coders who enjoy Globals want to move to a restricted host where Globals is
Off? Would you really want to rewrite all scripts to use the ugly $_POST $_GET etc vars?
I wrote a solution for this, for every script that you want to use Globals in, add this at the top:
include "defineGlobals.php";
defineGlobals(); // Register variables
then make new scriptfile called "defineGlobals.php" and fill it with the following code:
<?php
function defineGlobals() {
function defineVars($array) {
foreach ($array as $Var => $Value) {
// Dynamically allocate variables
global ${$Var};
${$Var} = $Value;
} // Arrayloop
}
// Define global scope variables. Emulating Globals = On
// Order:
// E=Environment
defineVars($_ENV);
// G=Get
defineVars($_GET);
// P=Post
defineVars($_POST);
// C=Cookie
defineVars($_COOKIE);
// S=Built-in variables
defineVars($_SERVER);
return 0;
}
?>
It follows the EGPCS convention, which means that this is EXACTLY the same as Globals = On. Get
overrides Env, Post overrides Get, Cookies override Post, and built in vars (such as $PHP_SELF)
override Cookies.
EGPCS baby ;)
If you found this helpful just send me a small email to chris@pulseforce.com
Happy coding!
----
Manual Page -- http://www.php.net/manual/en/language.variables.predefined.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+33746
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+33746&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+33746&report=yes