cookies... perl.. javascript

L

Lisa

Can anyone tell me why the cookie created by this javascript...

<script language=javascript type="text/javascript">
<!--
function SetCookie(username, value, expires, path, domain)
{ document.cookie = username + "=" + escape(value) +
((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain);
}
var expiration = new Date();
expiration.setTime(expiration.getTime() + 60000);
SetCookie('username', 'Peter', expiration);
// -->
</script>

is not seen by this perl script?

#!/usr/local/bin/perl
use CGI;
$q = new CGI;
print $q->header;
$cookie_in = $q->cookie("username");
if($cookie_in)
{
print $cookie_in;
}
else
{
print "Can't find cookie\n";
}

-Lisa.
 
T

Thomas 'PointedEars' Lahn

Lisa said:
Can anyone tell me why the cookie created by this javascript...

<script language=javascript type="text/javascript">
<!--
function SetCookie(username, value, expires, path, domain)
{ document.cookie = username + "=" + escape(value) +
((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain);
}
var expiration = new Date();
expiration.setTime(expiration.getTime() + 60000);
SetCookie('username', 'Peter', expiration); ^^^
// -->
</script>

is not seen by this perl script?

When a named argument of a function is not provided, its
value is not `null' (since that represents a null, empty,
or non-existent reference) but `undefined'. So you set the
cookie's `path' and `domain' to `undefined' as you do not
provide those arguments. And a site cannot read the cookies
not of its domain set which explains why your Perl script
fails.

In boolean expressions, `undefined' evaluates to `false',
so you can use the following:

function SetCookie(username, value, expires, path, domain)
{
document.cookie =
username + "=" + escape(value)
+ (expires
? ""
: "; expires=" + expires.toGMTString())
+ (path
? ""
: "; path=" + path)
+ (domain
? ""
: "; domain=" + domain);
}



HTH

PointedEars
 
L

Lasse Reichstein Nielsen

Thomas 'PointedEars' Lahn said:
Lisa wrote: ....

When a named argument of a function is not provided, its
value is not `null' (since that represents a null, empty,
or non-existent reference) but `undefined'. So you set the
cookie's `path' and `domain' to `undefined' as you do not
provide those arguments.

However, since Lisa uses "==" to compare, it still works, since
type conversion makes:
(undefined == null)
true.

(but yes, just using "expires" in the condition is sufficient)

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

cookie path 1
path in javascript 2
Cookies? 5
Adding dynamic cookie variable from php 0
Redirection Javascript via SRC 7
setting cookies across subdomains or paths 2
Redirection with Cookies 0
cookies in HTA 1

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top