Perl

Moderators: Jonathan
Number of threads: 1236
Number of posts: 3605

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Perl Cookies - Basic Help and Examples Please Posted by rafiquea on 28 Dec 2003 at 1:56 PM
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}\n";
print <<__;
Content-type: text/html
$ENV{'HTTP_COOKIE'}
<br>
<form method=post name=upform action="e2.cgi" onSubmit="return cc()">
<INPUT type="submit" value="Submit">
</FORM>
__


#perl script e2.cgi
...
print <<__;
Content-type: text/html
$ENV{'HTTP_COOKIE'}
<br>
__

Report
Re: Perl Cookies - Basic Help and Examples Please Posted by Jonathan on 28 Dec 2003 at 5:10 PM
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=/;\n";

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=/;\n";
print "Content-type: text/html\n\n";

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 future 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:-

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;
}


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.");




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.