1

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();

migrated from stackoverflow.com Nov 19 '11 at 15:25

This question came from our site for professional and enthusiast programmers.

  • FYI, I´ve tried (unsuccessfully) this: $_SESSION['redirect_to'] = $_SERVER['REQUEST_URI']; header("Location: mysite.net/user"); exit(); // On successful login $redirect = $_SESSION['redirect_to']; // unset the session var unset($_SESSION['redirect_to']); header("Location: mysite.net/$redirect"); exit(); – Rosamunda Nov 19 '11 at 2:33
  • Please post it into your question, so it is readable and easily accessible to everyone willing to answer your question. – Tadeck Nov 19 '11 at 2:34
  • Your approach should work, make sure you use session_start(); and set a session of your current url, then redirect. – Wesso Nov 19 '11 at 3:35
4

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.

1

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?

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.