Monthly Archives: August 2011

Drupal 6: Redirecting the User Destination After Registration

Scenario: An unregistered user visits a page that only logged in users can visit. Say it’s this page:

http://www.your-site.com/protected/page

Drupal throws up the login form, but the URL above remains the same – good as we’re going to use that. If they login, they are brought right back to this page, no problem. But what if they have to register? Assuming you have your User Settings set so that new users are logged in right away (no email confirmation, etc.), you’d of course like to redirect them back to the page they wanted originally. Instead, they just go to their new user account page, and that kinda sucks.

Figuring out a solution to this kinda sucked. After exploring numerous attempts at redirecting the form and using hook_user() – I finally just set a cookie, and blamo, it works. This needs to go into a custom module of yours:

// Implementaion of hook_user
function yourModule_user($op, &$edit, &$account, $category = NULL) {
    $refer = rtrim($_SERVER['HTTP_REFERER'], '/'); // removes any trailing slash
    $refer_array = explode('/', $refer); 
    $len = count($refer_array);
    if ($refer_array[$len - 1] == 'protected' && $refer_array[$len - 2] == 'page') {
        setcookie("YourDrupalDest", 'protected/page/', time()+200);  /* expire in 7.5 minutes */
    }
    switch ($op) {
        case 'insert':
            if ($_COOKIE["YourDrupalDest"]) {
                $_REQUEST['destination'] = 'protected/page/';
            }
            break;

        default:
            # code...
            break;
    }
}

Anyways, hope that helps point someone in the right, erm, destination.