PHP refresh countdown stop at zero
I have a website that shows a random number to new visitors and stores
that number in a cookie.
If the user refreshes the page then it pulls the cookies value and
subtracts a random number between 1 & 3 before displaying it.
This is what I have hacked together so far:
<?php
if (isset($_COOKIE['places'])) {
$places=$_COOKIE['places'];
$decrease=mt_rand(1, 3);
$places=$places-$decrease;
$expire=time()+60*60*24*30;
setcookie('places', $places, $expire);
}
else {
$randomString = mt_rand ( 11 , 23 );
$expire=time()+60*60*24*30;
setcookie('places', $randomString, $expire);
$places=$randomString;
}
?>
The issue I have is that if you carried on refreshing (which I doubt
anyone would do), it carries on through 0 into negative numbers. I have
looked through php.net but I can't find the solution I need to stop this
happening.
Any Ideas?
No comments:
Post a Comment