Hi,

I'm seeing a lot of the error "Undefined property: stdClass::$timestamp in _drupal_session_write()" in my logs. The error comes from a path/location for a custom module. However, It is unclear to me wether the module is the root of the error since the error somehow seems to come when Drupal attempts to write the session to the database.

Any ideas what the cause might be?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

sbrattla’s picture

It seems that _drupal_session_write() in session.inc always assumes that the $user->timestamp exists. Even though this is an unfamiliar territory to me, it seems that $user->timestamp not get's set before the user has received a cookie. This is what i
did to reproduce the problem:

1. Add print_r($user); as the second line in _drupal_session_write() in session.inc.
2. Register a new user and log in as that user.
3. The first request made after the user has logged in shows no trace of the property $user->timestamp. However, the subsequent request does show it along with a few more properties (such as hostname, cache, session and the like).

LIne 173 in session.inc does not check wether $user->timestamp exists, and my log files gets flooded with warnings telling me that the property does not exist.

As far as i can see, this is fairly easy to fix. However, does anyone know about any "pitfalls" with regard to this?

sbrattla’s picture

Category: support » bug

I'm changing this to a bug report, since Drupal assumes that the property always exists even though that is not the case.

amanaplan’s picture

Does your code do something like this?

global $user;
$user = user_load($user->uid);

If so, you are loading a plain user (i.e. account) object as the global $user which is mixing apples and oranges even if the uid is the same. The global $user object has session information. The plain user entity object is just a database select of a user, which you might need to load into a new variable in order to get Field API fields, etc. for the current user. However, if you overwrite the global $user object, you will erroneously remove session information, giving you that error. Simple design solution though, try this instead:

global $user;
$current_user = user_load($user->uid);
amanaplan’s picture

sbrattla’s picture

Category: bug » support
Status: Active » Fixed

Indeed, that is what I did. I only thought that user_load would load additional details for the current users (such as custom fields). However, I need to make sure in the future to keep those two instances clearly separated. Thank you for your feedback and help! It probably saved me a few hours :-)

I'll change the category to support request and set it as fixed.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

geerlingguy’s picture

Version: 7.0 » 8.x-dev
Status: Closed (fixed) » Active

I'm still getting this error in the db on many logins, and all I'm doing is calling global $user; in my own login form submit callback...

/**
 * Login form - submit callback.
 */
function custom_user_login_submit($form, &$form_state) {
  // Redirect user to profile after login.
  global $user;
  $form_state['redirect'] = 'user/' . $user->uid;
}

I can't tell if anything else could be causing this, though... it's just annoying that my logs include a $timestamp error every time a user logs in.

amanaplan’s picture

Does the error persist when you comment out everything in your custom submit callback function?

geerlingguy’s picture

Status: Active » Postponed (maintainer needs more info)

I'll continue looking into the problem.

geerlingguy’s picture

Status: Postponed (maintainer needs more info) » Active

I removed that hook entirely, and flushed all caches, and I'm still getting the error. Funny thing, though - I don't seem to get the error on my local MAMP environment, but the live server is showing the error. I have the databases synced via drush sql-sync, and the data structure is all the same.

Maybe a PHP version discrepancy or something strange going on?

bochen87’s picture

subscribe

blasthaus’s picture

sub, same issue

NancyDru’s picture

I'm getting Notice: Undefined property: stdClass::$timestamp in _drupal_session_write() (line 175 when adding an Article. I am not even looking at the user object.

7.10
PHP 5.2.11
MySql 5.5.8-log

batigol’s picture

same here - Notice: Undefined property: stdClass::$timestamp in _drupal_session_write() (line 175 of....

This notice message is freaking me out !

sah62’s picture

Category: support » bug
FileSize
666 bytes

I'm seeing this same error in the 7.x branch when users login. This is definitely a bug in session.inc. The simple fix seems to be a check to make sure that $user->timestamp is set before attempting to use it. Here's a 7.x patch that eliminates the login-related error on my site. It's a one-line change that should also work with the 8.x version of session.inc.

batigol’s picture

tested - patch is working for D7.12

Jody Lynn’s picture

Status: Active » Reviewed & tested by the community

I have the same problem in my logs. Patch looks good.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, session.inc_.patch, failed testing.

sah62’s picture

FileSize
655 bytes

Patch re-roll with Unix-style line endings...

geerlingguy’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, session.inc_.patch, failed testing.

sah62’s picture

The patch is failing testing because of a directory name problem caused by my ignorance of how the patch file system should be set up. If someone with more experience in patch development can fix it, please do.

geerlingguy’s picture

Status: Needs work » Needs review
FileSize
745 bytes

I checked out drupal core with git, then made the change in session.inc, and cd'ed to the drupal root directory (the directory with the core directory in it), then said 'git diff > patch-name-here-[issue]-[comment].patch. This should work for any core patch. Of course, YMMV on Windows...

james.williams’s picture

Issue #1410130: Notice: Undefined property: stdClass::$timestamp in _drupal_session_write() is a duplicate of this one, but some useful discussion about this patch was there.

seanburlington argued that the underlying problem should be resolved and not just hidden by suppressing the warning.

For me, this patch is good since this warning is unnecessary, but the underlying problem should also be identified & perhaps a more relevant/helpful warning thrown instead.

So, when should $user->timestamp be set? It looks to come from the $_SESSION rather than the original account object but I don't fully understand how that gets merged into the user object.

kpa’s picture

Status: Needs review » Needs work
global $user;
$user = user_load($user->uid);

This was my error, I assumed the global $user was the user_load() return.

This fixed for me:

global $user;
$user = (object)array_merge((array)$user, (array)user_load($user->uid));

This preserves everything in the global $user object, whilst appending into it the extra details I wanted to add, requiring the user_load().

Steven Jones’s picture

Seems like this issue may be a duplicate now as this issue got fixed as part of #1050746: HTTPS sessions not working in all cases I think?

Jody Lynn’s picture

Status: Needs work » Closed (duplicate)

Confirmed that #1050746 fixed this bit.