Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I notice that most popular sites have this function. I'm not sure what its called so I'll try my best to describe it.

Lets say you browse a page, then you click a link thats members only. You'll come across a page stating something like..."Members Area! Please log-in or Register for free!"

Then you enter your member details and when logging in you are taken to the page you were previously trying to view.

What is the best method to retain the previously viewed page and then pass it through the members log-in form, so that when your authentication script is called, it takes you to the correct area.

If anyone knows where I should start with this? Is it cookies? What if cookies is not enabled? Do most popular sites use cookies to do this?

Thank you, helpful replies are greatly appreciated!

Considered by gellyfish: mark as OT, a generalized CGI program not specific to perl
Unconsidered by planetscape: keep votes trump edit votes ( keep:6 edit:20 reap:2 )

Replies are listed 'Best First'.
Re: Member log-in: remembering url
by Corion (Patriarch) on Jul 27, 2006 at 06:06 UTC

    Simply include in your log-in form a hidden parameter which tells the page to return to, and in your log-in program, send a redirect back to that page, either with the session parameter added or with a cookie set or however else you log in your users.

    The form could look like the following (in Template::Toolkit syntax):

    <form action="/login.cgi"> <input type="hidden" value="[% returnurl %]" name="returnurl" /> <input type="text" name="user" /> <input type="password" name="password" /> <input type="submit" value="login" /> </form>

    And the untested receiving code could be something like:

    use CGI; my $q = CGI->new(); if (is_valid_login($q->param('user'),$q->param('password')) { print $q->redirect($q->param('returnurl')); } else { print "Forbidden"; };

    20060727 Fixed thinko in the code. s/redirecturl/returnurl/. Spotted by Hue-Bond, thanks!

Re: Member log-in: remembering url
by Cody Pendant (Prior) on Jul 27, 2006 at 06:26 UTC
    There's a recipe for roughly what you want in the CGI::Session::Cookbook.

    But basically, you send a "where was I before this" variable as part of the GET or POST you do when you send them to your login script, and when they're done logging in, you use it to send them back.



    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print