note 33746 deleted from language.variables.predefined by nicos
| From: | nicos@php.net | Date: | Sun, 06 Jul 2003 14:31:36 +0000 |
| Subject: | note 33746 deleted from language.variables.predefined by nicos | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-51584@lists.php.net to get a copy of this message | ||
Note Submitter: contact@pulseforce.com
----
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!