941,498 Members | Top Members by Rank

Ad:
  • PHP Code Snippet
  • Views: 17776
  • PHP RSS
0

Emulating Register Globals

by on Apr 9th, 2006

Ever since PHP turned register_globals_off I have had problems here and there in my codes. I searched hard and long for a simple fix while keeping globals turned off. Fortunately, I found one! I have posted it below for your convenience and it is also described Here.


I'm not sure if this has ever been posted, but if not I hope it helps you as much as it did me.
PHP Code Snippet (Toggle Plain Text)
  1. Here is the code to emulate register_globals On.
  2.  
  3. [CODE]
  4. <?php
  5. // Emulate register_globals on
  6. if (!ini_get('register_globals')) {
  7. $superglobals = array($_SERVER, $_ENV,
  8. $_FILES, $_COOKIE, $_POST, $_GET);
  9. if (isset($_SESSION)) {
  10. array_unshift($superglobals, $_SESSION);
  11. }
  12. foreach ($superglobals as $superglobal) {
  13. extract($superglobal, EXTR_SKIP);
  14. }
  15. ini_set('register_globals', true);
  16. }
  17. ?> [/CODE]
  18.  
  19.  
  20. And this code will emulate register_globals Off.
  21.  
  22. [CODE]
  23. <?php
  24. // Emulate register_globals off
  25. if (ini_get('register_globals')) {
  26. $superglobals = array($_SERVER, $_ENV,
  27. $_FILES, $_COOKIE, $_POST, $_GET);
  28. if (isset($_SESSION)) {
  29. array_unshift($superglobals, $_SESSION);
  30. }
  31. foreach ($superglobals as $superglobal) {
  32. foreach ($superglobal as $global => $value) {
  33. unset($GLOBALS[$global]);
  34. }
  35. }
  36. ini_set('register_globals', false);
  37. }
  38. ?>
  39. [/CODE]
Comments on this Code Snippet
May 26th, 2007
0

Re: Emulating Register Globals

To emulate register_globals = on one could just use
PHP Syntax (Toggle Plain Text)
  1. <?php extract($_REQUEST); ?>
and, register_globals = off is not being emulated the right way in your snippet.
Newbie Poster
eb1024 is offline Offline
1 posts
since May 2007
Jul 29th, 2008
0

Re: Emulating Register Globals

I use this snippet in all my codes if I have problems postings or getting vars. It works every time for me. I have made this into a class and used it for a long time.

Now why do you say :
and, register_globals = off is not being emulated the right way in your snippet.

Some people may use $_REQUEST in their codes which is fine, but as long as you code with security in mind and you know where the vars and methods are coming from $_REQUEST does not need to be used.
Junior Poster in Training
Banderson is offline Offline
66 posts
since Aug 2004
Message:
Previous Thread in PHP Forum Timeline: Index error in PHP code
Next Thread in PHP Forum Timeline: Pages with duplicate title tags





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC