简体   繁体   中英

Can't get cookie expiration to set

Can't get cookie expiration to set

I'm using php to set a cookie on a wordpress site, but having a bit of difficulty as the content shows up every time the page is loaded instead of only showing up the first time a user visits the site (expires once a day).

Forgive me if this is obvious, I'm still learning ;)

Here's the function, I have this in my functions.php page:

function set_newuser_cookie() {
    if ( !is_admin() && !isset($_COOKIE['jtc_newvisitor'])) {
        setcookie('jtc_newvisitor', 1, time()+60*60*24, COOKIEPATH, COOKIE_DOMAIN, false);
    }
}
add_action( 'init', 'set_newuser_cookie');

Here is how I'm calling it:

<?php if (isset($_COOKIE['jtc_newvisitor'])) { ?>
    <div id="modal-notices">content</div>
<?php } else { } ?>

Here's my js, I'm using to have the content display when the page loads:

$(window).load(function(){
    $('#modal-notices').modal('show');
});

I'd like to have the content load if it's the first time the user has visited the page within 24 hours, but as of now, it just loads every time.

If anyone can point me in the right direction here or explain what I'm doing wrong, it would be much appreciated.

Thanks!

It looks like you're checking if the cookie is set rather than if it's not set. Try this as your if statement:

if (!isset($_COOKIE['jtc_newvisitor']))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM