Jump to content

Modal Box Pop Up, Setting cookie with timer


Go to solution Solved by Jacques1,

Recommended Posts

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

 

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.

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

  • Solution

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.