0

I have two banners on the same page and I want to display just one at a time, each time I refresh the page it should display the other one. The containers of the two banners have a specific class called "cta1" and "cta2". Unfortunately, it does not alternate. The PHP code looks like this:

<?php
$time = strtotime(date('Y-m-d 23:59'));
if(isset($_COOKIE['cta'])){
    if($_COOKIE['cta']=='two') {
        echo '<style>.cta2{display:block !important;}</style>';
        echo '<style>.cta1{display:none !important;}</style>';
        setcookie('cta', 'one', $time+186400, '/');
    }else{  
        echo '<style>.cta1{display:block !important;}</style>'; 
        echo '<style>.cta2{display:none !important;}</style>';
        setcookie('cta', 'two', $time+186400, '/');
    }
}else{
    setcookie('cta', 'two', $time+186400, '/');
    echo '<style>.cta1{display:block !important;}</style>';
    echo '<style>.cta2{display:none !important;}</style>';
}
?>

The HTML code looks like this:

<div class="cta1">banner 1</div>
<div class="cta2">banner 2</div>
Val
  • 63
  • 9
  • 3
    Make sure the browser isn't caching the page. – Barmar Aug 31 '23 at 22:21
  • I tested your code, and it works. However, your code is clearly incomplete, the HTML is missing, so I added that myself. Perhaps there's a problem there? Or it could be [the caching](https://stackoverflow.com/questions/13640109/how-to-prevent-browser-cache-for-php-site) as Barmar says. – KIKO Software Aug 31 '23 at 22:21
  • At this point I'm pulling my hair. Google Chrome works. Edge and Firefox not. I will turn white hair before I get married :) – Val Aug 31 '23 at 22:40
  • Yes, that's the same HTML that I created. Have you tried the answer in the link I gave? It will prevent caching. – KIKO Software Aug 31 '23 at 22:49
  • It was the hosting. They are caching everything. I asked them to exclude this cookie from caching. Weirdest thing ever :) – Val Sep 01 '23 at 11:43

0 Answers0