1,105,333 Community Members

Simple Cookie script

Member Avatar
swissknife007
Junior Poster in Training
74 posts since Oct 2008
Reputation Points: 3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
<?php
$time1=time();
$time2=$time1+5;
echo $time2-time()."Current time<br>";
setcookie("user","Abhi",$time2);


// ECHO "<html>
// <body>
// <form action='cookie2.php' method='get'>
// <input type='Submit'> sybmit
// </form>
// </body>
// </html>
// ";

?>
<?php




while(isset($_COOKIE["user"]))
{
  echo "hi ".$_COOKIE["user"];	
  echo time()."Current time if<br>";
}
if(!isset($_COOKIE["user"]))
{
echo "hello";
echo time()."Current time else<br>";
}

?>

When does the cookie expire?
When I run it on Firefox ,the cookie doesn't expire after 5 seconds ....
Can you tell why?

Member Avatar
cereal
Posting Virtuoso
1,710 posts since Aug 2007
Reputation Points: 461 [?]
Q&As Helped to Solve: 345 [?]
Skill Endorsements: 44 [?]
 
0
 

Exactly. The function time() gives you the number of seconds since 1970-1-1, so if you want to expire your cookie after a week write:

$time2=$time1+ (7 * 24 * 60 * 60); # days * hours * minutes * seconds

bye.

Member Avatar
swissknife007
Junior Poster in Training
74 posts since Oct 2008
Reputation Points: 3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

SETTING COOKIE OR COOKIE1

<?php
$i=1;$time1=time();
$time2=$time1;
while($i<11)
{
$time2=$time2+5;
echo $time2-time()."  seconds is the time for expiry <br>";
setcookie("user[$i]",$i,$time2);
$i++;

}
?>

GETTING COOKIE OR COOKIE2

<?php


$i=1;
while($i<11)
{if(isset($_COOKIE["user[$i]"]))
{
  echo "hi ".$_COOKIE["user[$i]"]."<br>";	
  echo "<br>".time()."Current time if<br>";
}
if(!isset($_COOKIE["user[$i]"]))
{
echo "hello  $i cookie not present <br>";
echo "<br>".time()."<br>Current time else<br>";
}
$i++;
}
?>

I have made an array of cookies
each having a different expiry
the output i am getting is
coookie1

5 seconds is the time for expiry
10 seconds is the time for expiry
15 seconds is the time for expiry
20 seconds is the time for expiry
25 seconds is the time for expiry
30 seconds is the time for expiry
35 seconds is the time for expiry
40 seconds is the time for expiry
45 seconds is the time for expiry
50 seconds is the time for expiry

OUTPUT FOR COOKIE 2

hello 1 cookie not present

1323789262
Current time else
hello 2 cookie not present

1323789262
Current time else
hello 3 cookie not present

1323789262
Current time else
hello 4 cookie not present

1323789262
Current time else
hello 5 cookie not present

1323789262
Current time else
hello 6 cookie not present

1323789262
Current time else
hello 7 cookie not present

1323789262
Current time else
hello 8 cookie not present

1323789262
Current time else
hello 9 cookie not present

1323789262
Current time else
hello 10 cookie not present

1323789262
Current time else

Why is the cookie not set even within expiry time?
Please explain.

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 80 [?]
Team Colleague
 
0
 

Threads merged.

Member Avatar
swissknife007
Junior Poster in Training
74 posts since Oct 2008
Reputation Points: 3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Threads merged.

I am asking a completely different thing now

Member Avatar
swissknife007
Junior Poster in Training
74 posts since Oct 2008
Reputation Points: 3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I am asking a completely different thing now

U can check the content
is fairly different

Member Avatar
diafol
Where are my eyes?
12,964 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,847 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
0
 
$_COOKIE["user[$i]"]

So your cookies are called:

user[1]
user[2]
user[3]
..etc..?

Why the []? Wouldn't it be easier to call them user1, user2 etc?

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: