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

when switching from one cgi page to another, i need to refresh the page to remove the old cookie and replace it with the new one.

any reason this may be happening? that the cookie isn't replaced automatically, that is.

and/or how do i refresh the page right away and only once?

Replies are listed 'Best First'.
•Re: how to refresh a page immediately
by merlyn (Sage) on May 04, 2003 at 13:16 UTC
    The most robust thing to do is to place one unchanging cookie into the browser, then make all your state changes by tying that cookie into a time-limited login session on server side. I illustrate that point in the oft-referenced cookies article, including sample code. Someone even coded that into a module, but I can't remember where it is now.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: how to refresh a page immediately
by little (Curate) on May 04, 2003 at 11:50 UTC

    YOU CAN NOT FORCE THE BROWSER TO MAKE A REFRESH utilizing HTML. (Only the user can "reload" or "refresh" the page wich means to repeat the request for the document(s) from the server) with all parameteres unchanged utilizing his / her browser. It's a client side action)

    You can REDIRECT THE BROWSER to a URL utilzing HTML even if this is the same as the former one.

    Why I prenounce that is simply the way the browser requests a page. When a browser requests a document from a webserver that browser will also remit ALL cookies originating from that webservers qualified domain and all those cookies which the browser is additionally allowed to submit to that specific web server. In repsonse the webser will return the requested document AND the new cookies. If a new cookie has the same identifier as one previously written by the same server the old cookie will be overwritten or actualized so to speak.) So when your script is being executed due to a call for your scripts URI the webserver will hand all received data to your script. When using CGI.pm you have an easy task. Simply write the new cookies before you start delivering content as the cookies are being sent in the document header.

    A small example:

    #!/usr/bin/perl -wT use strict; $|++; use CGI qw/:standard /; use CGI::Cookie; # dont allow that tiny script to crack this server $CGI::DISABLE_UPLOADS = 1; # set this to a reasonable high amount preventing server attacks with +posting hughe files $CGI::POST_MAX = 15000; # please identify, somehow my $usrPc = remote_host(); # if you've been here before, tell me, as whom you identified my $usrWasLast = (cookie('ID'))? cookie('ID'):'none'; # what kinda browser ? my $usrAgent = user_agent(); # well, get a cookie, please accept it, and if not, I don't need that +info from you anyway :-) my $hiddenInfo = new CGI::Cookie( -name=>'ID', -value=>$usrPc, -expires=>'+3M', -domain=>'little.perlmonk.org' ); my $referrer = param('ref') if (param('ref')); # now give the browser what it requested print header( -charset=>'iso-8859-1', -cookie=>[$hiddenInfo] ); print start_html( -dtd=>'-//W3C//DTD HTML 4.01 Transitional//EN', -lang=> 'en', -title=> 'yourPageTITLE' ); print p(($usrWasLast)? "You've been here before using IP: " . $usrWasL +ast : "I don't know you."); print p('You have followed a link to this site from' . $referrer) if d +efined $referrer; print end_html(); # 1;

    Have a nice day
    All decision is left to your taste

        nice snippet but that does not force a reload or refresh (just a repetetion of the first request) instead it just forces the browser to NOT to CACHE the document content you delivered, so he will noct used previously cahced version to be displayed.

        You could instead use HTTP (via CGI.pm), send from your script a "document created - see here"-Header follwed by your own scripts URL but that prohibits sending any content after such redirection header.

        Have a nice day
        All decision is left to your taste

Re: how to refresh a page immediately
by Jaap (Curate) on May 04, 2003 at 09:28 UTC
    You probably need to expire the first cookie first, and then set a new cookie. This would require two pages to be sent.

    A page can be refreshed immediately with a http field called 'refresh' which is also avaliable as HTML http-equiv.