Accepting selected projects for Q2 2024 Check availability →
SERVER 02SYSTEM ONLINELAST SYNC: 03:17:44RSS 2.0 — ONLINE
HALF ASSEDTECHNICAL NOTES_
EST. 1999ISSUE 04.2BEST VIEWED AT 1024 × 768
TECHNICAL NOTES / TECHNICAL NOTES / PHP / RECORD e5b206
[PHP]NOTE

Removing a PHP application’s reliance on register_globals

POSTED: 10.06.2004AUTHOR: ADMIN2 MIN READCOMMENTS: 0

PHP 4.2 changed the default so external variables are no longer automatically registered in the global scope. Older applications can still depend on that behaviour. Migrate them by tracing each input, rather than turning the setting back on to silence missing-variable notices.

Identify where each value should come from

Use $_GET for query parameters, $_POST for submitted form data and $_COOKIE for cookies. These superglobal arrays are available from PHP 4.1. Avoid using $_REQUEST when the distinction matters. An order identifier read from a form and an authenticated account identifier held by the application should not be interchangeable simply because their variable names are similar.

Initialise internal state explicitly

Set defaults before evaluating conditions, especially flags controlling authentication, permissions or file inclusion. Check presence and expected type before using an input; a request can supply an array where the application expects text. Do not recreate the old behaviour with extract on request data or a loop assigning arbitrary variable names. Test with register_globals disabled in the actual web-server PHP configuration.

Test every entry point

Exercise directly requested scripts, included files, forms and administrative actions. Development error reporting can reveal uninitialised variables, while production diagnostics should go to a protected log. Continue validating values and escaping them for their output context. Turning register_globals off does not make a query assembled from untrusted text safe or authorise a user to edit another person’s record.

MIGRATION CHECK

Compare the effective configuration of the web-server PHP module and command-line PHP. They may load different configuration files, so a passing command-line check is not enough.

References

PHP 4.2.0 release announcement

PHP 4.1.0 release announcement