Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    79
    Thanks
    1
    Thanked 0 Times in 0 Posts

    problem with perl/cgi and cookie

    I want to set cookie in prova.html page.

    prova.html page is:
    <html>
    <head>
    <title>Prova</title>
    <head>
    <body>
    <form action="prova.cgi" method="POST">
    <fieldset>
    <legend><h2>Set your cookie...</h2></legend>
    <input type="text" name="name" /><br/>
    <input type="text" name="value" /><br/>
    <input type="text" name="expires" /><br/>
    <input type="submit" name="submit" /><br/>
    <fieldset>
    </form>
    <body>
    </html>
    prova.cgi script is:
    #!/usr/bin/perl

    use CGI;
    use CGI::Carp qw(fatalsToBrowser);

    my $cgi = CGI->new();

    print $cgi->header(-type => 'text/html');
    print $cgi->start_html('Risposta a prova');

    my $cookie = $cgi->cookie(-name => 'sessionID', -value => 'user@networkstore', -expires => '+1h');
    my $mycookie = $cgi->cookie('sessionID');

    print "<h2>Your cookie is $mycookie</h2>";
    but cookie is not set.

    Where is the problem?

    savio
    Last edited by dark0s; 03-01-2010 at 02:58 PM.

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,944
    Thanks
    2
    Thanked 170 Times in 165 Posts
    You need to send the cookie in the header.

    http://search.cpan.org/~lds/CGI.pm-3...m#HTTP_COOKIES

  • #3
    New Coder
    Join Date
    Jan 2010
    Posts
    79
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok, I modified my code:
    #!/usr/bin/perl

    use CGI;
    use CGI::Carp qw(fatalsToBrowser);

    my $cgi = CGI->new();

    print $cgi->header(-type => 'text/html');
    print $cgi->start_html('Risposta a prova');

    my $cookie = $cgi->cookie(-name => 'sessionID', -value => 'user@networkstore', -expires => '+1h');
    print $cgi->header(-cookie => $cookie);
    my $mycookie = $cgi->cookie('sessionID');

    print "<h2>Your cookie is $mycookie</h2>";
    now I can I set correctly $mycookie?
    Last edited by dark0s; 03-01-2010 at 06:22 PM.

  • #4
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,944
    Thanks
    2
    Thanked 170 Times in 165 Posts
    Yes, that line is correct, but you need to pass $cookie to the header.

    Also, you can't send and retrieve the same cookie at the same time.

    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use CGI;
    use CGI::Carp qw(fatalsToBrowser);
    
    my $cgi = CGI->new();
    my $cookie = $cgi->cookie(-name => 'sessionID',
                              -value => 'user@networkstore',
                              -expires => '+1h');
    
    print $cgi->header(-type => 'text/html',
                       -cookie => $cookie);
    
    print $cgi->start_html('Risposta a prova');

  • #5
    New Coder
    Join Date
    Jan 2010
    Posts
    79
    Thanks
    1
    Thanked 0 Times in 0 Posts
    How can I retreive $mycookie values?

  • #6
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,944
    Thanks
    2
    Thanked 170 Times in 165 Posts
    my $mycookie = $cgi->cookie('sessionID');

    That will retrieve the cookie, however, not in the same script execution where the cookie is being created and sent. Once it's been sent, you will be able to retrieve it in subsequent calls to your script(s).

  • #7
    New Coder
    Join Date
    Jan 2010
    Posts
    79
    Thanks
    1
    Thanked 0 Times in 0 Posts
    How can I retreive $mycookie in correctly way.
    What is the exact code.

  • #8
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,944
    Thanks
    2
    Thanked 170 Times in 165 Posts
    Quote Originally Posted by dark0s View Post
    How can I retreive $mycookie in correctly way.
    What is the exact code.
    Code:
    my $mycookie = $cgi->cookie('sessionID');


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •