Scenario: A user chooses to view a node without being logged in. I want my website to redirect the user to a login page, and then after logging in, return to the requested node. If the user enters the wrong login info at first, I still want them to be able to view the requested node after they provide the correct info.

I have both a login block on the very first page of the site, and a separate login page (a node) which is viewable to users who have not logged in.

I am very new to Drupal, but have implemented this functionality before without Drupal (using just php). There's a few things that I'm trying to get under my control.

1) Being able to designate a page to redirect to when the user tries to view a node and is not logged in. For instance, I would like the user to go to /login_page&destination=/user/node/### provided I have my separate login_page (cck node type with input set as PHP) created. I have tried to implement this by editing page.tpl.php for my respective theme (by setting a condition for if (!$user->uid) and using the traditional header(Location: ...) PHP redirect method. This does not seem to work however, as any page I try to access (existing or not) while I am not logged in redirects me to what I am calling home. Any ideas how to override this incorrect redirection?

2) I want to add some PHP into my login_page node to be able to retrieve the GET field "destination" (from part 1). I have specified that the code I am entering is PHP code, not HTML, but when I submit the code and view the node, the " ... " tags and code are still viewable in the HTML source; they weren't even parsed. Anyone having this problem?

3) I want to be able to modify my login block to be able to accept the destination URL as a GET parameter (or use Drupal's drupal_get_destination(); method). The problem is, drupal_get_destination() returns the node that I am currently viewing my login block from. I've tried previously being at a node and then going to the login block. This kind of makes sense as I'm not arriving to my block as a form action, but what is the correct use of this function? Also I've tried the traditional global variable $_SERVER['HTTP_REFERER'] in this block, but the value always appears as blank. By viewing my browser status, it seems there may be at least one or two degrees of PHP redirection; would this lose the HTTP_REFERER information?

If anybody has any ideas to help me in any of these parts, or provide a much easier alternative, it would be much appreciated. Thanks.

Comments

tm’s picture

would the front page module help? you can designate "landing pages" for roles, including anonymous. even if it is not what you want, maybe the module can give you ideas on where to go.

neopoet’s picture

Try the "logintoboggan" module
http://drupal.org/project/logintoboggan
I believe it has a feature that does this.

-Andrew