For three days I had nearly despaired when I tried to extend a WordPress site with a self-written login script. Everything worked properly on my local server, but as soon as I had uploaded it, the session variables didn’t seem to be passed any more: after the login, I stayed logged in for only one more click and then got logged out again.
After several unsuccessful Google searches (with the wisdom of hindsight it’s quite easy to google, but try and find something about this issue without reading any further than this) I found the reason at last, namely the function wp_unregister_GLOBALS()
inside the file wp-settings.php
. Here it is:
function wp_unregister_GLOBALS() {
if ( !ini_get('register_globals') )
return;
if ( isset($_REQUEST['GLOBALS']) )
die('GLOBALS overwrite attempt detected');
// Variables that shouldn't be unset
$noUnset = array('GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES', 'table_prefix');
$input = array_merge($_GET, $_POST, $_COOKIE, $_SERVER, $_ENV, $_FILES, isset($_SESSION) && is_array($_SESSION) ? $_SESSION : array());
foreach ( $input as $k => $v )
if ( !in_array($k, $noUnset) && isset($GLOBALS[$k]) ) {
$GLOBALS[$k] = NULL;
unset($GLOBALS[$k]);
}
}
What does this function do?
- Line 27/28:
Ifregister_globals
is not activated in the PHP settings of the web server, it won’t do anything at all (which explains the different behaviours of my script on my local and the live server). If, however,register_globals
is active, it proceeds as follows:- Line 34:
As the comment already says, all global variables listed here will not be reset. As you can see,$_SESSION
is not being mentioned! - Line 36-41:
Now, all global variables are being shifted and deleted one by one.
- Line 34:
In short: If register_globals
is activated, $_SESSION
will be deleted on every single page view. No surprise that we can’t get at our session variables any more!
What’s it all about?
Well, for one thing, WordPress assumes that it’s meant to take care of the complete site – after all it is some kind of content management system. And that includes the administration of all variables as well.
What’s much more important though, is the fact, that the use of register_globals
has been deprecated for a long time and the feature will even be completely removed in PHP 6. Today, it is strongly recommended not to use these kind of variables, because they involve safety hazards. So, WordPress is obviously trying to imitate the behaviour of a deactivated register_globals
in order to ensure safety and avoid potential variable conflicts.
Remedy
After having solved this riddle, the solution was perfectly obvious: I have to deactivate register_globals
on the web server, so WordPress wouldn’t even need to execute that stupid function. For this purpose, simply place a text file called php.ini
inside your WordPress directory, containing the following line:
register_globals Off
Further information on the subject can be found on php.net:
73 responses to “Session variables in WordPress”
Hi Ginchen,
I ‘m having the same session unset issue while trying to add captcha to my form. I just follwed you php.ini step to solve the issue. But it did not solve my problem then i modified wp-settings.php file to add $_session variable to nounset array.
just this, now my site is even not loading. I’m unable to access both the admin and blog sections.
It just displays blank page, no error, nothing.
I have replaced wp-config.php and wp-setting.php files with old files. But still problem is not resolved.
My site is hosted on shared host.
Please help me.
Thanks
Priya
Well, if you recovered the original files, they won’t probably be the problem. Did you delete the not working php.ini? On some servers, php.ini files are not supported. Instead, you can sometimes set certain PHP options in the admin panel of your host.
Thanks Ginchen!
I get to feel smug for Googling this first ;-) It was actually just fluke, but I’m pretty surpised there isn’t more documentation out there on this.
Did you try using using the WP database for session stuff at all? Was thinking a script for getting and setting some custom user meta fields might work but I’ve not tried. That just ignorance on my part?
Hmm, no, I didn’t do anything with the WP database. I just wrote a login script that used the login data from a phpBB installation on the same database. But I don’t see why your idea shouldn’t work. :)
You are a saviour…..
I tried so many options…putting session_start whereever i can but couldnt get it to work consistently.
Thanks a ton….i just updated the wp-settings.php function with a return
Hehe, the radical method. ;) But this way, you have to edit
wp-settings.php
after every WordPress update. That is why I would rather try to disableregister_globals
than changing the WordPress files.Vielen Dank erstmal für die Infos. Hat mir schon weiter geholfen, bei meinem Problem mit WordPress!
Wo, ich jetzt allerdings immer noch hänge, ist eigentlich nicht dass Thema des Blogeintrags, aber du hast es in den Kommentaren erwähnt. Ich suche nach einem Login-Skript, um die phpBB3-Userdaten in meinem WordPressblog nutzen zu können. Ich binde wie auf phpbb.de beschrieben die common.php ein, erhalte jedoch auf den WordPressseiten dann immer die Fehlermeldung: “Fatal error: Call to a member function sql_query() on a non-object in /pages/48/f6/d0004241/home/htdocs/lupa_neu5/phpBB3/includes/cache.php on line 51”. Trotz intensiver Google-Suche habe ich noch nichts gefunden, was mir hilft. Habe jetzt angefangen über die phpBB-Cookies die Sitzung auszulesen und bin wegen eines anderen Problems auf deiner Seite gelandet.
Um es abzukürzen, daher meine Frage: Gibt es das von dir geschriebene Login-Skript irgendwo als Plugin oder Beispiel zum runterladen?
Also, das was ich da damals programmiert hatte, könntest Du, glaube ich, sowieso nicht brauchen. Das war für eine ganz spezielle Seite gedacht und ist damit wohl nicht wirklich wiederverwertbar. ;)
Ich nehme an, Du willst, daß Leute sich im phpBB einloggen und dann auch gleich ins WordPress-Backend rein können (oder umgekehrt: sich in WP einloggen und dann auch gleich im Forum eingeloggt sind)? Dafür habe ich inzwischen eine tolle Lösung gefunden: die WordPress to phpBB3 Bridge. Allerdings geht die im Moment nur bis WordPress-Version 2.7.1. (was ich aber eigentlich auch nicht sooo schlimm finde). Das Tolle an dieser Bridge ist, daß sie überhaupt keine große Konfiguration oder Rumgefummel benötigt – einfach wie ein Plugin installieren und fertig.
Alternativ gibt es noch WP-United. Das habe ich vor langer Zeit mal getestet, aber da gab es anscheinend noch ein paar Bugs, durch die es bei mir überhaupt nicht funktionierte und ich es wieder gelöscht habe. Aber inzwischen hat sich da ja auch wieder was getan – vielleicht also auch nochmal einen Versuch wert.
Hallo,
WP-United kenne ich, ist mir allerdings bei der Weiterentwicklung immer etwas zögerlich.
Die andere Bridge kannte ich noch nicht. Da ich aber bereits auf WordPress 2.8.x geupdatet habe, fällt die momentan aus.
Habe aber von dort aus weiter recherchiert und zwei weitere Bridges gefunden:
Onepress: http://onepresscommunity.com/
Single Sign On: http://wordpress.org/extend/plugins/phpbb-single-sign-on/
Um es kurz zu machen: Beide laufen bei mir irgendwie nicht. Aber vielleicht helfem einem anderen User die Links weiter.
Werde mir also jetzt doch noch WP-United anschauen und wenn das nicht funktioniert manuell eine Übergangslösung programmieren, bis eine der Bridges mit der neuesten WordPress und phpBB-Version funktioniert.
Thanks so much for this, it saved me a lot of time integrating a music download system I wrote a while ago with a new WordPress website / theme I have developed.
For anyone who wants to disable Register_Globals but can’t edit their php.ini, it also possible to do it by putting this line in your .htaccess file:
php_flag register_globals off