954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Simple Cookie script

<?php
$time1=time();
$time2=$time1+5;
echo $time2-time()."Current time";
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";
}
if(!isset($_COOKIE["user"]))
{
echo "hello";
echo time()."Current time else";
}

?>


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

swissknife007
Junior Poster in Training
74 posts since Oct 2008
Reputation Points: 13
Solved Threads: 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
Master Poster
709 posts since Aug 2007
Reputation Points: 214
Solved Threads: 120
 

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 ";
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]"]."";	
  echo "".time()."Current time if";
}
if(!isset($_COOKIE["user[$i]"]))
{
echo "hello  $i cookie not present ";
echo "".time()."Current time else";
}
$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
74 posts since Oct 2008
Reputation Points: 13
Solved Threads: 0
 

Threads merged.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
Threads merged.


I am asking a completely different thing now

swissknife007
Junior Poster in Training
74 posts since Oct 2008
Reputation Points: 13
Solved Threads: 0
 
I am asking a completely different thing now


U can check the content
is fairly different

swissknife007
Junior Poster in Training
74 posts since Oct 2008
Reputation Points: 13
Solved Threads: 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
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: