I need help with cookies

SejtanovRatnik

Regular Member
Joined
Nov 26, 2015
Messages
484
Reaction score
238
Hy

Can anyone tell me how to specific <div> to show only once per user in 24 hours?

I use wordpress themes, and want to show specific ad only once per user in 24 hours.

Also I would like to know how to restart cookies for that <div>.

Thanks in advance
 
You can use the PHP function setcookie with expire parameter as time()+60*60*24 == 24 hours, so the cookie would automatically expire in 24 hours.

Check with $_COOKIE['unique-identifier'] if the cookie exists and if so don't display the div
 
http://stackoverflow.com/questions/8132173/show-div-once-a-day



I like the localstorage one more as it is supported by all modern browsers and does not depend on cookie..

Code:
if(localStorage.last){
    if( (localStorage.last - Date.now() ) / (1000*60*60*24) >= 1){ //Date.now() is in milliseconds, so convert it all to days, and if it's more than 1 day, show the div
        document.getElementById('div').style.display = 'block'; //Show the div
        localStorage.last = Date.now(); //Reset your timer
    }
}
else {
    localStorage.last = Date.now();
    document.getElementById('div').style.display = 'block'; //Show the div because you haven't ever shown it before.
}
 
Last edited:
http://stackoverflow.com/questions/8132173/show-div-once-a-day



I like the localstorage one more as it is supported by all modern browsers and does not depend on cookie..

Code:
if(localStorage.last){
    if( (localStorage.last - Date.now() ) / (1000*60*60*24) >= 1){ //Date.now() is in milliseconds, so convert it all to days, and if it's more than 1 day, show the div
        document.getElementById('div').style.display = 'block'; //Show the div
        localStorage.last = Date.now(); //Reset your timer
    }
}
else {
    localStorage.last = Date.now();
    document.getElementById('div').style.display = 'block'; //Show the div because you haven't ever shown it before.
}


Thank you very much. This link help me!!!
 
You are welcome. Be sure to catch the exeption and use cookie if you are using localStorage. That way, you prepare for the dinosaur browsers too :p

Thank you very much. This link help me!!!
 
Hy

Can anyone tell me how to specific <div> to show only once per user in 24 hours?

I use wordpress themes, and want to show specific ad only once per user in 24 hours.

Also I would like to know how to restart cookies for that <div>.

Thanks in advance

I can help you with this, add me
 
Back
Top