http://qs321.pair.com?node_id=165734

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

I have a slight dilemma...

I'm working on a "Back" script that uses the information stored in cookies to reload a CGI page. The information (username, password, and project file) are stored in three different cookies. The program reads these cookies, then redirects the user to the CGI page. If one of the cookies is empty, it has the user manually sign back in by sending him or her to a default page.

This code works, but only for Netscape 6. IE 5 and Netscape 4.75 get 500 errors.

Here's the code...

use vars qw( $q $redirect ); use CGI; $q = new CGI; checkCookies($q); print $q->redirect(-url=>"http://www.mysite.com/index.pl$redirect");

and checkCookies looks like:

sub checkCookies { my $q = shift; my $client = $q->cookie("cuser") || ''; my $password = $q->cookie("cpswd") || ''; my $project = $q->cookie("cproj") || ''; if(($client ne '') && ($password ne '') && ($project ne '')){ return "?action=display&uid=$client&pass=$password&ppa=$project"; } else{ return ''; } }

Is this something I've misprogrammed, or is this merely a browser bug?

Also, does anyone know of a way I can save state while a person is outside of a CGI page?

For now, I'll have users open new windows to enter the unCGI area, and just close them instead of attempting to keep them entirely in one window.

Thanks in advance...

John J Reiser
newrisedesigns.com

Replies are listed 'Best First'.
Re: Redirection problems with Perl, Netscape and IE
by vladb (Vicar) on May 10, 2002 at 18:14 UTC
    Sorry, I can't provide you with a valid answer to your direct question, but let me at least suggest a few things related to this:

    Also, does anyone know of a way I can save state while a person is outside of a CGI page?

    I have looked at the CGI::Persistent before and think that it may be worth your consideration. Simply put, it allows you to store client state on your server (inside data files). You simply have to tie this 'state data' to a session id of your user. The way I did it was assign a unique session id to new clients (say, right after they are successfully signed in) and save that id in a cookie on their side. On the server side, you create a new CGI::Persistent object to store this client's state. Whenever a user returns to your page, you simply look up his/her session id cookie value and use it to refer to earlier saved state data(using the same CGI::Persistent object).

    Note: where is your $redirect variable set in the code? You call your checkCookies() in void context althought the sub returns a value. Shouldn't you save it in your $redirect variable?

    "There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith
Re: Redirection problems with Perl, Netscape and IE
by newrisedesigns (Curate) on May 10, 2002 at 18:35 UTC

    Thanks vladb for your help with CGI::Persistent.

    I have found the major extent of my problem, after a lot of poking around.

    The Redirection header was (and I thought remained as) 'Location:'

    This is not the case.
    Location: is no longer the definitive "Redirect Header." URI: is now the preferred redirection header.

    If you look in my code, you'll see:

    print $q->redirect(-URL=>...

    By changing URL to URI, CGI.pm changes Location: to URI:. If you check the perlfaq9, it says that CGI.pm sends both, but I'm not sure that the version I'm using - on a virtually hosted machine. :( - does what the perlfaq says.

    Thanks to those (especially vladb) for your help.

    John J Reiser should have known the difference
    newrisedesigns.com
      Now I am interested in what is going on there.

      Since I started CGI programming with perl, I have always used the 'Location:' HTTP header for redirecting users.
      I have never once used the 'URI:' header and it has always worked.

      Also, I just tried a script using this 'URI' header and it did not work under either IE 6 or NS 6.
      So what is going on? *scratches his head and goes off to eat a taco*

Re: Redirection problems with Perl, Netscape and IE
by giulienk (Curate) on May 11, 2002 at 15:59 UTC
    I don't know about the problem you ask, but i can tell you storing a number of cookies in the user browser with user information it's not the best way to deal with session.
    I point you out to a relatively famous column by merlyn, Basic cookie management where you find how to deal with cookies and session in a right way.
    Also check Apache::Session that despite the name works in every CGI environment.


    $|=$_="1g2i1u1l2i4e2n0k",map{print"\7",chop;select$,,$,,$,,$_/7}m{..}g