Skip to content

Instantly share code, notes, and snippets.

@adduc
Created July 16, 2014 18:34
Show Gist options
  • Save adduc/be043e165790db0587ed to your computer and use it in GitHub Desktop.
Save adduc/be043e165790db0587ed to your computer and use it in GitHub Desktop.
<?php
/* Emulate register_globals behavior:
*
* @see http://php.net/manual/en/ini.core.php#ini.variables-order
*
* If variables_order is set to "EGPCS", and both $_GET['action'] and
* $_POST['action'] are set, then $action will contain the value of
* $_POST['action'] as P comes after G in our example directive value.
*
* @param bool $include_session Whether to register session variables or not.
* @return void
*/
function emulate_register_globals($include_session = false)
{
$variables_order = strtolower(ini_get('variables_order'));
$translation = array(
'e' => $_ENV,
'g' => $_GET,
'p' => $_POST,
'c' => $_COOKIE,
's' => $_SERVER
);
foreach (range(0, strlen($variables_order) - 1) as $i) {
$char = $variables_order[$i];
$arr = isset($translation[$char]) ? $translation[$char] : array();
foreach($arr as $key => $value) {
$GLOBALS[$key] = $value;
}
}
if ($include_session) {
if (!session_id()) {
session_start();
}
foreach ($_SESSION as $key => $value) {
$GLOBALS[$key] = $value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment