Simple Cookie script
<?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?
Related Article: Simple Blog Script
is a PHP discussion thread by BleepyE that has 2 replies and was last updated 10 months ago.
swissknife007
Junior Poster in Training
75 posts since Oct 2008
Reputation Points: 13
Solved Threads: 0
Skill Endorsements: 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.
cereal
Veteran Poster
1,146 posts since Aug 2007
Reputation Points: 344
Solved Threads: 223
Skill Endorsements: 22
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.
swissknife007
Junior Poster in Training
75 posts since Oct 2008
Reputation Points: 13
Solved Threads: 0
Skill Endorsements: 0
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 54
Threads merged.
I am asking a completely different thing now
swissknife007
Junior Poster in Training
75 posts since Oct 2008
Reputation Points: 13
Solved Threads: 0
Skill Endorsements: 0
I am asking a completely different thing now
U can check the content
is fairly different
swissknife007
Junior Poster in Training
75 posts since Oct 2008
Reputation Points: 13
Solved Threads: 0
Skill Endorsements: 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?
diafol
Keep Smiling
10,675 posts since Oct 2006
Reputation Points: 1,632
Solved Threads: 1,514
Skill Endorsements: 57