Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Perl Cookies - Basic Help and Examples Please

rafiquearafiquea Posts: 1Member
Firstly, will someone please point me in the right direction to set and retrieve cookies properly in Perl (i.e. simple example would be fantastic).

Second, trying on my own, I have hit two problems. What am I doing wrong?

1. When the cookie is initally set, I have to refresh the page otherwise it does not appear!!! How do I avoid this? (e1.cgi)

2. When I call e2.cgi via a form (using post) from e1.cgi, there is no cookie set. However, when I call e2.cgi direct from the web page (assuming I have called e1.cgi first & refreshed the page), the cookie is there. Why can't I get the cookie back from e2.cgi when its called from form in e1.cgi?

Again simple examples would be ideal. Thank you loads!!!!

#perl script e1.cgi
...
$cookievalue = "User 1";
print "Set-Cookie: user=${cookievalue}
";
print




__


#perl script e2.cgi
...
print

__

Comments

  • JonathanJonathan Posts: 2,914Member
    Hi,

    Think you're slightly confused about the way cookies work for starters. You set a cookie by sending a HTTP Set-Cookie header which may look like this:-

    print "Set-cookie: name=value; path=/;
    ";

    You may follow it by other headers, making sure the last one has two newlines after it. So you might have:-

    print "Set-cookie: name=value; path=/;
    ";
    print "Content-type: text/html

    ";

    These headers are then sent to the web browser with your page. The web browser looks at these cookies and then says "OK, I'll store then and send them with [b]future[/b] requests." Note that cookies are a browser side thing in terms of storing them - what is in %ENV is what the browser sent. So you do not see cookies until after a refresh or another script grabs them.

    As for grabbing cookies, I like to have a sub like this:-

    [code]sub parseCookies {
    #Parse cookie data.
    my %cookies = ();
    my @pairs = split(/; /, $ENV{'HTTP_COOKIE'});
    foreach my $pair (@pairs) {
    my ($name, $value) = split(/=/, $pair);
    $cookies{$name} = $value;
    }

    #Return cookie hash.
    return %cookies;
    }[/code]

    Then you can do the following in your main code:-

    my %cookies = parseCookies;

    Then you can access them by name, e.g.

    print $cookie{'name'};

    Will print the value of the cookie called "name", which in the example I gave about was literally the word "value".

    Like with form data, you should consider escaping characters like < and > as well as possibly ' and even " - these can be used to do cross-site scripting attacks and SQL injection attacks. Do whatever is appropriate for your situation.

    Hope this helps,

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Sign In or Register to comment.