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

Dani AI

Generated

Brief summary of the real causes here (and what to do):

The cookie header must be sent before any output, its expiry is an absolute Unix timestamp, and a cookie set in the current response is only available to PHP on the next request. In ’s posts the script prints timestamps, calls setcookie and then immediately checks $_COOKIE — that won’t show the newly set cookie. is right about using time() correctly, and ’s question about the brackets is relevant: PHP turns names like user[1] into an array in $_COOKIE, so the access pattern matters.

Quick fixes you can apply now:

  • Call setcookie() before any echo/HTML (or check headers_sent()).
  • Pass an absolute expiry: use time()+N for N seconds into the future.
  • After setcookie(), redirect or reload the page so the browser returns the cookie on the next request.

Example pattern to set and then immediately test (do not output before this):

if (!headers_sent()) {
  $ok = setcookie('user', 'Abhi', time() + 30, '/');
  if ($ok) {
    header('Location: '.$_SERVER['REQUEST_URI']);
    exit;
  }
}

Reading array-style cookies correctly (common pitfall): PHP exposes these as an array. Do not use string keys like "user[1]" when checking $_COOKIE; instead use the array index:

if (isset($_COOKIE['user']) && isset($_COOKIE['user'][1])) {
  echo $_COOKIE['user'][1];
}

Troubleshooting checklist:

  • Look for “headers already sent” warnings or stray BOM/whitespace before <?php.
  • Check browser dev tools (Storage/Cookies) to confirm name, domain, path and expiry.
  • For tests, use a longer TTL (e.g. 30s or 1min) — 5s is fragile because of clock skew and network delays.
  • Check setcookie() return value and use headers_sent() to diagnose.

Recommended Answers

All 6 Replies

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 Member #120589
$_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.