Returning the user to the page he/she was last viewing


An non-authenticated user navigates his/her website, reviewing the content of each page. Soon, the user encounters a page that he/she would like to edit. The user clicks the "log in" link, and is either: a) redirected back to the front page of the website or b) redirected to a specific page. The problem is that now the user must re-navigate the site back to the page he/she wishes to edit in order to begin the editing process.


The solution to the problem stated above is to return the user back to the page he/she was viewing before attempting to "log in"

The Patch (against 6.x-1.2)

In order to preserve the current functionality of pubcookie, the patch only works if the "Successful login URL:" setting is empty. So if the administrator has decided that he wants users to be directed to a specific page after "log in" that will still happen. Otherwise, the user will be returned the page they were last viewing.

pubcookie-return-to-last-viewed.patch1.93 KBmannkind


jeastham’s picture

Thanks. While I wish this was a configurable option in the pubcookie module, applying this patch solved my problem.
For a beginner like myself, it was also important to remember that you must pass the destination to pubcookie using a query string for this to work. For example:

mannkind’s picture

@jeastham - Glad you've found the patch useful. I know it's been quite useful at my place of work.

jameswoods’s picture

[Updated code/results, still need help]
Great patch, thanks! 8^)

I would like to add a menu item (like a real drupal menu) that uses the path "login/pc?destination=$_GET['q']" so that whenever this menu is displayed, it has a link that includes the "get back here" functionality your awesome patch provides.

I have the following working great, when it gets put into a tpl.php file (blech!)

<a href="<?php print base_path() . 'login/pc?destination=' . $_GET['q'];?>">Pubcookie login test</a>

However --

The following code puts the menu item into a menu, but the link is always login/pc%3Fdestination%3Dnode/1

function widget_display_menu(){
  $items = array();
  $items['login/pc?destination=' . $_GET['q']] = array(
    'title' => 'Pubcookie redirect test',
    'description' => 'my description',
    'access callback' => 'user_access',
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
    'menu_name' => 'menu-masthead-menu',
    'page callback' => array('widget_display_sendtopubcookie'),
  return $items;

and then using drupal_goto in the widget_display_sendtopubcookie function...but I get a white screen with some devel debug info.

Am I on the right track? Is it possible to create a menu item as described above? If not, could you point me in the right direction?


jameswoods’s picture

Issue tags: +redirect, +pubcookie

The following code works for me. I'm admittedly chagrined because I don't completely understand why...I sort of iterated this process until I got something to work: 1) Read about some new function, 2) Try the function how it's supposed to be used, 3) Cry when it didn't, 4) Try every hacky/brute-forcey tactic possible. 5) Repeat 1-4.

Eventually something gave way and I was able to bend Drupal to my will.

Below the code, I'll explain what little I do understand.

 * Provide Pubcookie-redirect friendly menu item containing the current path.
 * See the pubcookie module and the awesome
 * patch 'Returning the user to the page he/she
 * was last viewing'.
 * I'm sure this could be integrated into the pubcookie module via a patch, but
 * I'm just not that good yet.  For now it stands alone.
 * The patch requires that a user provide specific query string such as
 * '?destination=node/777' or '?destination=taxonomy/term/3'. It proved easy to
 * accomplish this by adding code to a tpl.php file. However I could find no
 * way to do this in a Drupal menu.  I've used the to_arg() function to
 * generate a custom link that passes the path to a function that generates
 * the needed query string and redirects.
 * Some notes: 1) All attempts to use hook_menu_alter with a query string were
 * met with stiff resistance from something that kept encoding the query string
 * in a way that made it useless. 2) I'm using header('Location') instead of
 * drupal_goto() because I needed to get out of https.

 * An implementation of hook_menu().
 * The login/ portion of the item should be what you put into the
 * 'Login directory: '. The value you use here will be used in the
 * MY_MODULENAME_sendtopubcookie() function.

function MY_MODULENAME_menu(&$items){
  $items = array();
  $items['login/%dest'] = array( 
    'title' => t('Pubcookie login'),
    'type' => MENU_NORMAL_ITEM, 
    'access arguments' => array('access content'),
    'page callback' => 'MY_MODULENAME_sendtopubcookie',
    'menu_name' => 'menu-band-menu',
  return $items;

 * An implementation of to_arg();
 * Returns the drupal path to the hook_menu() function.

function dest_to_arg(){
  $daurl = $_GET['q'];
  return $daurl;

 * Sends the user to the pubcookie login page with the query string.
 * Strips the login from the Drupal path so we can get the Drupal alias
 * via drupal_lookup_path(). Sets the base to non ssl and redirects to
 * the pubcookie modules folder with the proper query string.

function MY_MODULENAME_sendtopubcookie($myarg){
  $drupalpath = str_replace('login/' , '', $_GET['q']);
  $drupalalias = drupal_lookup_path('alias', $drupalpath);
  $base = '' . base_path();
  $path = 'login/pc?destination=' . $drupalalias;
  header('Location: ' . $base . $path);

$items['login/%dest'] is a 'wildcard' for the special 'to_arg()' function. Essentially, it's a helper function that Drupal can use to produce a replacement (paraphrased from Mr. Awesome, John K. VanDyk, author of Pro Drupal Development 2nd Edition).

By naming a function with the wildcard and appending _to_arg($arg) (e.g. dest_to_arg($arg), I'm able to return some value. In this case, I'm just returning the value of the query string varible 'q' (which ends up being 'node/123' or 'taxonomy/term/3' or whatever).


dalearyous’s picture

Version: 6.x-1.2 » 7.x-1.0

how would i do this in d7 ?

jvandyk’s picture

Version: 7.x-1.0 » 7.x-2.0-beta1
Status: Active » Fixed

This feature has been added to 7.x-2.0-beta1.

On the configuration page there is a place to enter "Successful Login URL". If that is left empty, the user is now redirected to the page they were on.

Status: Fixed » Closed (fixed)
Issue tags: -redirect, -pubcookie

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