Page is a not externally linkable
- Code, Content, and Presentation
-- Perl Server Side CGI Scripting
---- Getting results from Javascript back to Perl


rocknbil - 6:10 pm on Dec 9, 2011 (gmt 0)


Not sure what you mean, but below is an example that works for you to grok the concepts. Name it "cookie-with-js.cgi" (or rename the link, otherwise the link won't work), upload it to your cgi-bin and set permissions to 755.

If you have any other cookies set for this domain (like analytics, etc) it will give you some unexpected results on the second page but you should still see "Cookie name is Fred, value is Wilma" on the second page. You're supposed to "cycle through" (loop) the cookies set for the domain and pull their values by the key name - this just sets and reads one cookie.


#!/usr/bin/perl
%qs=&readParse;
print "Content-type:text/html\n\n";
print qq|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Set with JS, read with Perl</title>
<script type="text/javascript">
function setCookie() {
document.cookie = 'Fred=Wilma';
document.getElementById('open-me').style.display='block';
return false;
}
</script>
</head>
<body>
|;
if ($qs{'readit'}) {
$cdata = $ENV{'HTTP_COOKIE'};
($name,$value) = split(/=/,$cdata);
print "Cookie name is $name, value is $value";
}
else {
print qq|
<p><a href="#" onclick="return setCookie();">click here to set the cookie</a></p>
<p id="open-me" style="display:none;">
<a href="cookie-with-js.cgi?readit=1">Cookie set. Click to read it with Perl</a>
<p>
|;
}
print '</body></html>';
## just meant for this script
sub readParse {
my (%query_hash,$query_string,$_qsname,$_qsvalue,$pair);
$query_string = $ENV{'QUERY_STRING'};
foreach $pair (split(/&/, $query_string)) {
($_qsname, $_qsvalue) = split(/=/, $pair);
$_qsname =~ s/[\+]/ /g;
$_qsname =~ s/%([\da-f]{2})/pack("C",hex($1))/ieg;
$_qsvalue =~ s/[\+]/ /g;
$_qsvalue =~ s/%([\da-f]{2})/pack("C",hex($1))/ieg;
$query_hash{$_qsname} = $_qsvalue;
}
return %query_hash;
}


Thread source:: http://www.webmasterworld.com/perl/4395572.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com