johnnylak Posted January 22, 2017 Share Posted January 22, 2017 Hello, I have a modal box on my site and don't want it to show up each time a user refreshes a page. I have placed this code on my HTML site and it works fine, however when I place it in my Wordpress blog it does not work. I placed this code snipped after the head tag and before the body tag. I'v also tried to set the cookie in the head first but then can't figure out the logic to run the modal. <?php if (!isset($_COOKIE['modal'])){ setcookie("modal", "true", time()+3660, "/"); ?> <script type="text/javascript"> $(window).load(function(){ $('#modal-box').modal('show'); }); </script> <? } ?> Any thoughts or suggestion would be highly appreciated. Thanks, Johnny Quote Link to comment https://forums.phpfreaks.com/topic/303002-modal-box-pop-up-setting-cookie-with-timer/ Share on other sites More sharing options...
ginerjm Posted January 22, 2017 Share Posted January 22, 2017 What kind of "code" did you place "after the head and before the body"? Html? Js? Php? Hopefully not php. Quote Link to comment https://forums.phpfreaks.com/topic/303002-modal-box-pop-up-setting-cookie-with-timer/#findComment-1541834 Share on other sites More sharing options...
johnnylak Posted January 22, 2017 Author Share Posted January 22, 2017 Regardless of where I place that code snippet I have provided. it does not work. Quote Link to comment https://forums.phpfreaks.com/topic/303002-modal-box-pop-up-setting-cookie-with-timer/#findComment-1541838 Share on other sites More sharing options...
Jacques1 Posted January 22, 2017 Share Posted January 22, 2017 Can you be a bit more specific than “doesn't work”? We're not clairvoyant. Does the application crash? Do you always see the dialog? Do you never see the dialog? Is the cookie set in the browser? You're using a short PHP tag at the bottom: <? This should be avoided, because it requires a special setting in the PHP configuration. Use standard tags at all times. You should also consider setting and checking the cookie with JavaScript to avoid this kind of PHPHTMLJavaScript spaghetti code altogether. Quote Link to comment https://forums.phpfreaks.com/topic/303002-modal-box-pop-up-setting-cookie-with-timer/#findComment-1541839 Share on other sites More sharing options...
ginerjm Posted January 22, 2017 Share Posted January 22, 2017 My question is still awaiting an answer. If you want help you have to cooperate with those trying to help. Perhaps you could show a bit more of the context that you are describing to us. Quote Link to comment https://forums.phpfreaks.com/topic/303002-modal-box-pop-up-setting-cookie-with-timer/#findComment-1541860 Share on other sites More sharing options...
johnnylak Posted January 23, 2017 Author Share Posted January 23, 2017 I'm sorry, I'll try and be a little more specific. I have a modal window that pops-up when a visitor lands on my page. However, I don't want the modal to appear again straight away hence I have placed the time to expire after a short while. In the head I have not set a cookie. I have set the cookie straight after the body tag by using the code snippet above. When testing to see if the cookies has been set by using another if statement and echo'ing the result it seems to have not been set. If however, I set the cookies in the head it does set but then I am unable to achieve the result I am trying in the above snippet. Handball Player - The kind of kind you ask for is the snippet I've provided. It's php and javascript and i've moved it to just under the body tag. Confused - It doesn't crash, the cookie just does not set. I am using those special tags because if I don't the snippet it being read correctly and then the whole page crashes. I hope this is much more clarity. You're help in invaluable. Thanks heaps, Johnny Quote Link to comment https://forums.phpfreaks.com/topic/303002-modal-box-pop-up-setting-cookie-with-timer/#findComment-1541884 Share on other sites More sharing options...
Solution Jacques1 Posted January 23, 2017 Solution Share Posted January 23, 2017 You cannot set cookies after you've generated HTML markup. Cookies are set in the HTTP header, but the markup is in the HTTP body which comes after the header. Once you start sending body data to the client, it's too late to do anything with the header. PHP cannot travel back in time (it can theoretically buffer the message, but this is complex and not recommended in your case). So you have to set the cookie before you generate any page content. There must be no output before the setcookie() call. No <html>, no <head>, not even whitespace. Nothing. As I already said, you need to fix your spaghetti code. I -- again -- strongly recommend you set the cookie with JavaScript. Not with PHP. With JavaScript. If, for some strange reason, that's not an option for you, you'll have to reorganize your PHP script to look something like this: <?php // this is the first line of the script; there must be nothing before this if (isset($_COOKIE['modal_seen'])) { $show_modal = false; } else { $show_modal = true; setcookie("modal_seen", "true", time() + 3660, "/"); } // now the HTML markup ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Title</title> </head> <body> <!-- ... --> <?php if ($show_modal): ?> <script type="text/javascript"> $(window).load(function(){ $('#modal-box').modal('show'); }); </script> <?php endif; ?> <!-- ... --> </body> </html> I strongly recommend against this. It's just bad programming. Quote Link to comment https://forums.phpfreaks.com/topic/303002-modal-box-pop-up-setting-cookie-with-timer/#findComment-1541885 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.