Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    1
    Thanked 5 Times in 4 Posts

    How do i make a different ad come up when someone refreshes the page?

    You know how google has different ads everytime you go to a page? Well i want to do the same thing. I got two banners and i want one to change to the other banner when someone refreshes the page.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    Use cookies. It's not perfect, because a user could turn off cookies, but that's rare enough you don't need to worry about it.

    So just check for the cookie and, if it is not there, pick an ad at random. Then "remember" the ads the user has seen in the cookie. If the cookie is there, then just pick a random ad the user hasn't already seen.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    On the next page load, start from step 2.
    Ummm...actually, since it's always the same page being loaded, you won't have any choice but to start from step1. But that's okay, because if you didn't, then you wouldn't have any ads to display.

    *********

    If you are concerned about users who don't have cookies enabled, then instead of pulling the ads sequentially from the array, you would pull them randomly. And in the cookie you would keep a list of *all* the ads that have so far been displayed, so that you could remove them from the array before picking one at random. When the array is empty, you just start all over: clear out the list and pick any of them at random.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,081
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by bigcasey123 View Post
    You know how google has different ads everytime you go to a page? Well i want to do the same thing. I got two banners and i want one to change to the other banner when someone refreshes the page.
    FWIIW, many people delete their cookies at frequent intervals.

    Rather than cookies you could display the banner based on whether the number of milliseconds at page load was odd or even. That would give a 50% chance of a new banner.
    Last edited by Philip M; 05-19-2012 at 07:42 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •