<?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?

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.

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.

Threads merged.

Threads merged.

I am asking a completely different thing now

I am asking a completely different thing now

U can check the content
is fairly different

Member Avatar for diafol
$_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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.