Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Cookie problem

by bar10der (Beadle)
on Aug 10, 2004 at 11:54 UTC ( #381534=perlquestion: print w/replies, xml ) Need Help??

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, 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,

    "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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://381534]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2021-05-13 04:14 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (134 votes). Check out past polls.