Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

slowing down for cookie

Status
Not open for further replies.

effennel

Technical User
Oct 15, 2002
60
CA
Hello,

my script writes a cookie and then tries to read from it to write the content to a database.

I beleive it tries to read the cookie before it is available (written I suppose). The cookie returns an empty value. Or if I run the script a second time, it returns the content from the previous run.

How can I slow down things a bit? Is there some wait or pause command I can use?

Thanks
FNL

FNL
 
There is the sleep function: [tt]sleep 1[/tt] makes the program sleep for 1 second, but that's probably a bit long for your purpose. There might be a millisecond-timed equivalent at CPAN.
 
Thanks TG,

I use the sleep function without success. I am starting to wonder: is it possible to set and read a cookie form the same script?

FNL
 
This is the code I use:

#!/usr/bin/perl
srand(time);

$cid = int(rand(1000000));
print "Set-Cookie: genid=$cid; path=/; domain=.domain.com\n";
print "Content-type:text/html\n\n";
...

...
if($action =~ /ok/i){
if($login && $password) {
&search_data;
if($count == 1){
&databasecookie;
foreach $line(@results)
...

...
sub databasecookie {
$recordcookie = $ENV{'HTTP_COOKIE'};
($nom,$coid) = split (/=/, $recordcookie);

$record=$coid;
$record.= "\|@results"; #results comming from &search_data

open (ACCESS, ">>$cookiebase") || die;
flock ACCESS, 2;
seek ACCESS, 0, 2;
print ACCESS "$record";
close(ACCESS);

}
 
You can't set and read a cookie in the same HTTP request. If you think about it, there's absolutely no need to be able to. Any information that you've put in the cookie must already be available to your script somehow so you shoudn't have any reason to read it from the cookie when you have it already.

As an aside, I'd recomment using the cookie methods in CGI.pm for setting/retrieving cookies, rather than what you're doing now.
 
Thanks for your reply ishnid,

I'll look into that.

FNL
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top