http://www.perlmonks.org?node_id=381534

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

Hi Monks, I am writing a web based tool using CGI-Application & HTML-Template. I need to recognise re-visiting users. For this I am writing a cookie. So far so good and following code works fine...
my $self = shift; my $query = $self->query(); my $redirectTo = $query->param('redirectTo'); ---- ---- $userDetails='<all info that needs to be stored>'; my $expires = '+12M'; my $kpi_cookie = $query->cookie(-name=>'kpi', + -value=>$userDetails, + -expires=>$expires,); $self->header_props(-cookie=>[$kpi_cookie]); my $template = $self->load_tmpl('index.html'); my $lmess = "Successfully logged in"; $template->param(MSG => $lmess); return $template->output;
However problem comes when I try to redirect user to the section of the website where user intended to go after writing cookie. So for example if user wants to enter/update his monthly report, when he clicks on that link, system should check for existing cookie and if no cookie is found, write cookie and then redirect to 'enter/update data' section. When I try to re-direct immediately after writing cookie, cookie information is lost!! Here is the code-
............ ............ my $redirectTo = $query->param('redirectTo'); my $this_url = $query->script_name(); $userDetails='<all info that needs to be stored>'; my $expires = '+12M'; my $kpi_cookie = $query->cookie(-name=>'kpi', + -value=>$userDetails, + -expires=>$expires,); $self->header_props(-cookie=>[$kpi_cookie]); my $redirect_url=$this_url.'?rm='.$redirectTo; $self->header_type('redirect'); $self->header_props(-url => $redirect_url); return;
I am not sure where I am making mistake!!

Replies are listed 'Best First'.
Re: Cookie problem
by derby (Abbot) on Aug 10, 2004 at 12:07 UTC
    From the CGI::Application docs:

    Calling header_props will clobber any existing headers that have previously set.

    So just put them together in one call

    my $redirect_url=$this_url.'?rm='.$redirectTo; $self->header_type('redirect'); $self->header_props(-url => $redirect_url, -cookie=>[$kpi_cookie] );

    -derby
      Derby, Thanks for your help. It works.
Re: Cookie problem
by Velaki (Chaplain) on Aug 10, 2004 at 12:26 UTC

    This site has a decent explanation of cookie mayhem.

    A snippet from that site states:

    The URL must use an absolute path; otherwise, the web server may attempt to avoid another request and response cycle by simply returning the content for the new URL as the content of the initial response via an internal redirect.

    perldoc CGI gives:

    script_name() Return the script name as a partial URL, for self-refering scripts.

    Hope this helps,

    -v
    "Perl. There is no substitute."

    Edited by davido: Disabled link to website violating O'Reilly's copyright.

      Isn't that a link to illegally posted copyrighted material? Please don't do that, and please remove it from your post. O'Reilly are our friends, and you should support your friends.

      -Any sufficiently advanced technology is
      indistinguishable from doubletalk.

      My Biz