1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

I need help with cookies

Discussion in 'General Programming Chat' started by SejtanovRatnik, Jan 6, 2016.

  1. SejtanovRatnik

    SejtanovRatnik Regular Member

    Joined:
    Nov 26, 2015
    Messages:
    474
    Likes Received:
    226
    Location:
    Google Adsense
    Home Page:
    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
     
  2. Overfl0w

    Overfl0w Newbie

    Joined:
    Jan 8, 2016
    Messages:
    15
    Likes Received:
    0
    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
     
  3. Gogol

    Gogol Jr. VIP Jr. VIP

    Joined:
    Sep 10, 2010
    Messages:
    3,411
    Likes Received:
    3,057
    Gender:
    Male
    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: Jan 8, 2016
  4. SejtanovRatnik

    SejtanovRatnik Regular Member

    Joined:
    Nov 26, 2015
    Messages:
    474
    Likes Received:
    226
    Location:
    Google Adsense
    Home Page:

    Thank you very much. This link help me!!!
     
  5. Gogol

    Gogol Jr. VIP Jr. VIP

    Joined:
    Sep 10, 2010
    Messages:
    3,411
    Likes Received:
    3,057
    Gender:
    Male
    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

     
  6. wisdomkid

    wisdomkid Jr. VIP Jr. VIP

    Joined:
    Jun 20, 2011
    Messages:
    2,710
    Likes Received:
    790
    Why would you want to show ads to users per 24 hours?
     
  7. SejtanovRatnik

    SejtanovRatnik Regular Member

    Joined:
    Nov 26, 2015
    Messages:
    474
    Likes Received:
    226
    Location:
    Google Adsense
    Home Page:
    I'm sorry but I don't want to share answer.
     
  8. Kory1

    Kory1 Registered Member

    Joined:
    Oct 13, 2015
    Messages:
    97
    Likes Received:
    35
    perhaps the ad was bought only to be shown once for every unique visitor, this is option all the ad networks give.
     
  9. lukaGardel

    lukaGardel Newbie

    Joined:
    May 30, 2015
    Messages:
    40
    Likes Received:
    1
    I can help you with this, add me