This is a specific problem that affects older versions of Drupal on a server running PHP 5.2 or later. Often it arises when PHP is upgraded (sometimes without your knowing!).

Symptom: You can appear to log in OK, but on the very next page view the system seems to forget that you have just logged in and you revert to being an "anonymous" user.

If you are running Drupal 5.x or later then this specific problem won't affect you, so if you still have trouble staying logged in then check here.

This problem occurs due to the way PHP 5.2+ handles objects in session handlers. It affects all Drupal versions up to Drupal 4.6.10, Drupal 4.7.4 and Drupal 5-beta 1 inclusive.

The best method of fixing this is to upgrade to the latest official release of a supported version of Drupal. Currently (Dec 2008) this means the latest release of either the 5.x or the 6.x series.

For those wishing not to do a full upgrade yet but who need an "instant fix", you can add the following line at the bottom of your settings.php file:

// Temporary fix to login/sessions problem.
// Remove this line when upgrading to 4.6.11, 4.7.11 or 5.x or later.
register_shutdown_function('session_write_close');

You should regard this as a temporary fix only. If you subsequently upgrade your site to a newer version of Drupal you should remove this line from settings.php to prevent potential incompatibility with future versions of Drupal.

The problem is fixed in the 4.6.11 and 4.7.5 releases of Drupal. So another option for 4.6.x and 4.7.x users is to upgrade to the final release of the 4.6.x/4.7.x series - 4.6.11 or 4.7.11, which are available here.

Note that while Drupal 5.0 doesn't exhibit this particular problem with PHP 5.2 it is still not fully compatible with this version of PHP; Drupal 5.1 is fully compatible (but of course doesn't contain the security fixes in the latest 5.x release).

Here is the original issue in which this problem was identifed and fixed.

Comments

HS’s picture

Hi,

My users are constantly logged out and have to re-login. I am Drupal 6.8 the latest release. I am on MYSQL 5.0.67.

Thanks in advance,
H

mike876’s picture

Server: Debian etch
MySQL: 5.0.32
PHP: 5.2.0

None of the other fixes that can be found on the net work either...

gpk’s picture

>None of the other fixes that can be found on the net work either
The specific problem described on this page does not occur in 6.8 or 5.14. You are probably experiencing something similar but fundamentally different, and possibly server-related. First place to look would be cookie handling in PHP. Occasionally an incorrectly configured cache between the user and the server is implicated. Suggest you open a new forum topic.

gpk
----
www.alexoria.co.uk

dsandif’s picture

I'm dealing wit the same issue, only a little different in action. After installation I am logged in to the system and I can apply changes which are visible while logged in. However, after I logout and try to view the pages as an outsider viewing the web site, I get the "Access denied" error. To be clear, I just want to backout and view the web site as if I was a visitor looking from the outside in and just check out the pages to see how they look and if the look right, instead, I get "Access Denied" and I can't view anything until I log back in as admin. My drupal version is 6.6, My PHP version is 5.2.6 and my SQL version is 5.0.41, although there is also present a SQLiteManager whichsays its version 2.8.17 - 3.3.17. Thxs.

D-

gpk’s picture

Does the "anonymous" user have "access content" permission? If this doesn't help then I suggest opening a new forum topic.

gpk
----
www.alexoria.co.uk

dsandif’s picture

Thanks!, That did the trick!. You guys are the greatest!

espertus’s picture

Thanks for posting this explanation and fix. It was a big help.

big67’s picture

This was very useful after moving to a new provider with a newer PHP version.

hychanhan’s picture

It helps me too.

Thanks you very much for this guide.

ChanHan Hy

tkuben’s picture

Hi,

I have same problem where when I log in as the admin user it brings me back to the main page without actually logging me in. I see the login prompts again. I added the following to the end of settings.php:

// Temporary fix to login/sessions problem.
// Remove this line when upgrading to 4.6.11, 4.7.11 or 5.x or later.
register_shutdown_function('session_write_close');

I am on drupal 6.17 with php: 5.2.13. Anyone else having similar problems with a fix?

Thanks
Thusjanthan

mediamike’s picture

I am having this annoying problem and I don't know why. Even as admin I log on and then click to another page and it acts as if I have logged out. Also, sometimes, my users cannot log in using their password and they have to get a new one sent to them.

I tried disabling facebook connect, but that did not fix the problem.

Michael
www.christian-faith.com

mediamike’s picture

People complain about it regularly also.
The fundamental basics of drupal no longer work for me.

And some other large drupal sites have exactly the same kind of problem - e.g. christianvolunteering.org

gpk’s picture

I'm not seeing this problem on 6.x. The only time was when a user had cookies disabled. Most likely to be a server/browser config problem IMO.

If you want to investigate it further I'd suggest opening a new topic/issue and post as many details as you can of symptoms and troubleshooting steps you've taken.

Fayna’s picture

I had the same problem where the admin login wouldn't take; tried everything, confirming user #1 had the correct password, we didn't have the globalredirect module installed, etc. Turned out the 'sessions' table had crashed in our drupal database. Once I repaired it through phpMyAdmin, I was able to login. hope this helps someone.