Simply Program

Simply programming - It’s a life style
Filed under Programming, PHP

My problem - when a user logged into my website and then went to my "blog" area (which did not require any logging in) Wordpress would removed all the sessions I had set.

Why did it do this?

If we open wp-settings.php the first few lines of code completely remove all session information (as well as other).

CODE:
  1. function wp_unregister_GLOBALS() {
  2. if ( !ini_get('register_globals') )
  3. return;
  4.  
  5. if ( isset($_REQUEST['GLOBALS']) )
  6. die('GLOBALS overwrite attempt detected');
  7.  
  8. // Variables that shouldn't be unset
  9. $noUnset = array('GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES', 'table_prefix');
  10.  
  11. $input = array_merge($_GET, $_POST, $_COOKIE, $_SERVER, $_ENV, $_FILES, isset($_SESSION) && is_array($_SESSION) ? $_SESSION : array());
  12. foreach ( $input as $k => $v )
  13. if ( !in_array($k, $noUnset) && isset($GLOBALS[$k]) ) {
  14. $GLOBALS[$k] = NULL;
  15. unset($GLOBALS[$k]);
  16. }
  17. }

To fix this issue I did the following:

CODE:
  1. function wp_unregister_GLOBALS() {
  2. if ( !ini_get('register_globals') )
  3. return;
  4.  
  5. if ( isset($_REQUEST['GLOBALS']) )
  6. die('GLOBALS overwrite attempt detected');
  7.  
  8. // Variables that shouldn't be unset
  9. $noUnset = array('GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES', 'table_prefix','_SESSION');
  10.  
  11. $input = array_merge($_GET, $_POST, $_COOKIE, $_SERVER, $_ENV, $_FILES);
  12. foreach ( $input as $k => $v )
  13. if ( !in_array($k, $noUnset) && isset($GLOBALS[$k]) ) {
  14. $GLOBALS[$k] = NULL;
  15. unset($GLOBALS[$k]);
  16. }
  17. }

Posted by sp on Thursday, May 8th, 2008


You can follow any responses to this entry through the magic of "RSS 2.0" and leave a trackback from your own site.