4

On one certain page of my site, users have to log in before going any further. They don't have to be logged in to see most of the rest of the site content, but on this one specific page, they do have to be logged in. I figured out how to put a barrier up that forces users to log in before seeing that page's content (i.e., if users aren't logged in, the only thing on the entire page is a line that says "To create content, you must first log in" and there's a link to the login page), but how do I redirect newly logged-in users directly back to that same page (which now will have content on it) so they can now see its content?

At the moment, after they log in, it just takes them to their profile page.

In other words, if users log in on any other page, it's fine if they're redirected to their profile page, but on this one specific page, they need to stay put. I've seen lots of variation of the following code:

function hook_user_login(&$edit, $account) {
drupal_goto('node/1');
}

But I don't think this is what I'm trying to accomplish. The current code I have in there looks like this:

$sample1 = '<a href="../user">log in</a>.';
$sample2 = 'To create a classified ad, you must' . " " . $sample1;

global $user;
if ($user->uid == false){
return $sample2;

}

I've seen some code like this that looks promising:

#action' => url($_GET['q'], array('query' => drupal_get_destination()))

But I can't figure out how to make it work. Anyone got any ideas?

  • If you don't mind to use rules module, take a look at this answer - It's exactly what you're looking for, I think. – Jack-PL Jul 29 '13 at 23:10
5

Hope this will give you,

In Drupal 7, you can achieve it by using destination parameter.

Just add the destination parameter in the URL where you want to redirect it.

For example if you are in login page and after login if he want to redirect to some node say about page then

YOUR_DOMAIN/user/login?destination=about

Just add the destination parameter in "To create content, you must first log in" link. On click of this link it will redirect to User login page with the destination parameter. After login it will redirect to the path you have specified in the destination value.

  • This worked! Thanks, inzio. However, URLs with equals signs and question marks in them always worry me -- using destination parameters doesn't open me up to SQL injection attacks, does it? – Drupguy Jul 30 '13 at 9:21
  • SQL injection is a technique used to take advantage of non-validated input vulnerabilities to pass SQL commands through a Web application for execution by a backend database. As we are not executing any backend database, we are just redirecting to the particular URL. So no worries :) – inizio Jul 30 '13 at 9:45
  • Awesome, thanks for that explanation. That makes sense. I appreciate your help very much. – Drupguy Jul 30 '13 at 14:41
  • Your welcome :) – inizio Jul 30 '13 at 14:54
1

Use the r4032login module and it will redirect users to the login page anytime they hit a page that they don't have permission to access. Once they login it will redirect them back to the page they were trying to access.

1

You can use the Rules module in Drupal. Use something like a 'execute custom PHP code' where you can use request_uri to preg_match with the page you need identified. That's it. Below is a simple block you could use:

$request_url = $_SERVER['REQUEST_URI'];
if (preg_match("/content-page/", $request_url)) {
  return TRUE;
}
  • Keep the event "User has logged in".
  • In the condition, "execute custom PHP code"
  • Create a page redirect below that option to redirect to that page
  • Assign a greater weight to this Rule if there's already login-redirect rules on your site.

Should get you working.

0

This can be achieved by using a combination of content access and login toboggan modules. You'll be able to restrict pages and prompt user to login to access them.

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.