1

I'm trying to create a little php function, which if a cookie exists, then use the css styles, else if it does not exist use these.

I'm using the code below, but it is using the if styles regardless if the cookie exists or not.

What am i doing wrong?

<?php if ( isset($_COOKIE['CookieLawInfoConsent']) ) : ?>
<style>
    html.has-not-scrolled #desktop-header { top: 0px; }
    #mobile-header { top: 0px; }
    main {top: 0px;}
</style>
<?php else : ?>
<style>
    @media all and (min-width: 1201px) and (max-width: 1235px) {
       html.has-not-scrolled #desktop-header { top: 80px; }
    #mobile-header { top: 80px; }
    main {top: 80px;
}
</style>
<?php endif; ?>
Abilogos
  • 4,777
  • 2
  • 19
  • 39
Jon
  • 25
  • 7
  • `var_dump($_COOKIE['CookieLawInfoConsent'])` and look at what's in it before the if statement. That should give you a good indication why it keeps hitting the if part of the block. You've likely set that variable elsewhere in your code. – DiddleDot Aug 27 '21 at 20:32
  • you can do this by Js [check here](https://stackoverflow.com/a/5968306/9287628) – Abilogos Aug 28 '21 at 15:34

1 Answers1

0

You can use an html class that is unique set_not_cookie or set_cookie based on cookie input:

<div class="<?php if ( isset($_COOKIE['CookieLawInfoConsent']) ) : ?> 
set_cookie 
<?php else : ?> 
set_not_cookie <?php endif; ?>">

class attributes: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes#attr-class class selectors: https://developer.mozilla.org/en-US/docs/Web/CSS/Class_selectors

rhand
  • 1,176
  • 4
  • 17
  • 45