17 January 2014

Recovering an edited view/session in Drupal

Recently I was editing a view in Drupal when I suffered a crash on my computer leading to me losing the Drupal session I was using. On logging in again, I returned to the view I was editing only to be confronted by the message:

This view is being edited by user x, and is therefore locked from editing by others. 
This lock is x sec old. Click here to break this lock.

There was quite a lot of work saved in my previous session that I didn't want to lose so I decided to try to recover it. Luckily, Drupal stores session ids in the database so it is possible to find all session ids associated with my user, then use a cookie editor to regain the session. Here's how I did it.

If you have access to the MySQL console or PHPMyAdmin you can execute the following SQL on your Drupal database to retrieve your stored sessions:

SELECT sid FROM sessions WHERE uid=1; # Make sure you replace the uid with your user id.

If you don't have access to the MySQL console or PHPMyAdmin, but you can execute PHP in pages, blocks or using the Devel module on your Drupal site, you can execute the following code to print all your session ids to the screen:

<?php

$query = db_query('SELECT sid FROM {sessions} WHERE uid=%d', array(1));

while ($result = db_result($query)) {
  print $result . '<br />';
}

?>

Next you'll need a cookie editor. I used Edit This Cookie for Chrome, but Cookies Manager+ for Firefox is great too.

Editing the Drupal session cookie

Once you have found your session ids and have a cookie editor, visit your Drupal site then using your cookie editor alter the content of your Drupal session cookie. The session cookie is the one with the name that starts with "SESS". Once you have found the right cookie, change its content to match each of session ids until you find the correct one. To test, visit the edit page of your view, then refresh the page after editing your cookie. If your previous edits appear, you have found the right session!

Post a comment

(required)

(required)

(required, not published)

(optional)