Question

I´ve got a Drupal site that after the user logs in it is redirected to his profile. I want to create a specific rule so people from specific characteristics can retur to the page they were before they´ve logged in.

I´m planning to do that with the rules module.

So, I´ve set the parameters and conditions, but when I try to create the rule, telling Drupal where it should redirect people, I just don´t know how to say that.

I think I should put some php code inside that page redirect rule, but don´t know how. Hopw you can help me out.

FYI, I've tried (unsuccessfully) this:

$_SESSION['redirect_to'] = $_SERVER['REQUEST_URI'];
header("Location: http://www.mysite.net/user");
exit();

// On successful login
$redirect = $_SESSION['redirect_to'];
// unset the session var
unset($_SESSION['redirect_to']);
header("Location: http://www.mysite.net/$redirect");
exit();
Was it helpful?

Solution

Have a look at the Login Destination module.

In custom code, to issue a redirect in Drupal use the drupal_goto() function. Drupal has a massive API, use it.

OTHER TIPS

First approach: read the referer. BUT it is not a safe and pretty option https://stackoverflow.com/questions/165975/determining-referer-in-php

Also, you can create a session ( session_start() ) for every anonymous user and later, create a new one when they log-in, extracting the current page URI or something stored in the "guest session" and using it to redirect. Cookies can somehow be forged BUT no personal info is on the session so... why not?

you could append the current URI as an argument to the login page:

header("Location: http://mysite.com/user?return_to=" . $_SERVER['REQUEST_URI']);
exit;

Then have the login script read that argument and use it for a redirect after successful login.

$retTo = $_GET['return_to'];

etc, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top