I have created a cookie which get set for the first time with the value 24. What I want is that every time a user comes back to the page, the cookie value should decrease. And actually that works really well if you load the page multiple times and DON'T DROP OFF the page. But if you load the page, drop off, load another page and then come back, the cookie still only get set and doesn't get read out and decreased... But what I want is, that everytime, no matter if the user leaves the page and comes back or if he only reloads the page, the value of the cookie get decreased... Right now that happens only if you load the page multiple times consecutively, not if you drop off and come back...
My question now is, how can I implement that?
You can check this page out by yourself. I am printing out the values of the cookie at the top of the page at the moment!
This is my code so far:
PHP Snippet #1 (set/get Cookie - gets executed in the header)
<?php
if(is_page(161)){
if(isset($_COOKIE['limitedProduct'])){
$old = $_COOKIE['limitedProduct'];
$value = $_COOKIE["limitedProduct"] - rand(5,8);
setcookie('limitedProduct', $value, (time()+31536000));
echo 'I got set! -> Old: ' . var_export($old) . " - New: " . var_export($value);
}else{
setcookie('limitedProduct', '24', (time()+31536000));
echo 'I got init! -> 24';
}
}
?>
PHP Snippet #2 (prints out the value of the cookie)
<?php
//Asking for NULL because at the very first time there is no cookie!
if($_COOKIE['limitedProduct'] == NULL) {
echo "<p style='text-align: center; color: red;'><strong>Der Kurs ist stark limitiert - Nur noch 31 verfügbar!</strong></p><br> " . var_export($_COOKIE['limitedProduct']);
}else if ($_COOKIE['limitedProduct'] > 2) {
echo "<p style='text-align: center; color: red;'><strong>Der Kurs ist stark limitiert - Nur noch " . $_COOKIE['limitedProduct'] . " verfügbar!</strong></p>";
}else if($_COOKIE['limitedProduct'] < 2){
echo "<p style='text-align: center; color: red;'><strong>Der Kurs ist stark limitiert - Nur noch 2 verfügbar!</strong></p>";
}
?>