register_globals was removed from PHP 5.4. The historical examples below are retained for migration/reference only; do not recreate register-globals behavior in current applications.The register_globals directive was removed in PHP 5.4. Current PHP code should read external values explicitly from superglobals such as $_POST, $_GET, $_COOKIE and $_SERVER, then validate them before use.
$username = trim($_POST['username'] ?? '');
if ($username !== '') {
echo htmlspecialchars($username, ENT_QUOTES, 'UTF-8');
}
This is the recommended starting pattern. The detailed examples below are retained for explanation, comparison and related variations.
The following material explains how older PHP installations behaved. It is preserved for understanding legacy code, not as a recommendation for current PHP.
When we submit a form or pass variables by query sting or read from Cookies the variables will be available by default if the register_globals is set ON in php.ini file. But this way all these variables , or the user data will interfere with the code of the page so many time the register_globals setting will be kept in Off condition. Keeping it on is also a security problem if proper care is not taken.if (ini_get('register_globals')){echo "<font color=red>Registor global is ON</a><br>This is a security issue, please change settings inside your php.ini file</font>";}
else{echo "Register global is OFF";}
This setting of global variables is kept inside php.ini file and is not available for edit in shared hosting plans. So you may have to contact your host to change the setting. If you are using your local machine or in windows then search for php.ini file inside your windows directory.
$username=$_POST['username'];
Here the form is submitted by POST method and similarly we can get the values for GET method or variables from the URL, server variables etc.
$username=$_GET['username'];
Here is a code example for making available the variables if register global is off
while (list ($key,$val) = each ($_POST)) {
$$key = $val;}
But note that by using above code you are defeating the very purpose of keeping the register_global off for security reasons.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| Jamal Faiye | 26-08-2013 |
| Very usefull | |