TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Show code only once every 24 hours to each user? (http://www.talkphp.com/general/1510-show-code-only-once-every-24-hours-each-user.html)

Sled 11-22-2007 10:21 PM

Show code only once every 24 hours to each user?
 
Hey,
I'm trying a new way to monetize ads, but it can be pretty annoying, and therefor, I'd like the ads to load only once every 24 hours for each user.

I have no idea how to do this, so can someone please explain/suggest a way?

Preferably without using database, I think a cookie or a session or something would be the easiest way to do this, though I don't know how these work...


Thanks! :rolleyes:

Wildhoney 11-22-2007 10:49 PM

Quite simple actually. Just takes a little thinking about to get the initial logic. I have wrote you the following script which displays Show Advert every 24 hours. This can be modified as you please by modifying the integer 24 to whatever you please. The date is stored in a cookie and then sent to the server upon every page request. Even if an individual hasn't visited your website for 72 hours, it will still display the advert and then hide it until another 24 hour's time.

php Code:
$iCurrentTime = strtotime(date('Y-m-d h:i:s'));
$iHours = ($iCurrentTime - $_COOKIE['last_displayed']) / 3600;

if($iHours > 24 || !isset($_COOKIE['last_displayed']))
{
    setcookie('last_displayed', $iCurrentTime, time() * 3600, '/');
   
    echo 'Show Advert';
}

iisbum 11-23-2007 05:37 AM

Couldn't you just set the cookie to expire in 24 hours and if it exists, don't show the ad?

PHP Code:

if(!isset($_COOKIE['last_displayed'])) {
  
setcookie('last_displayed'date("Y-m-d h:i:s"), time() + (24 3600), '/');
  echo 
'Show Advert';


Instead of figuring out the the hours in the PHP. I always like to do as little as possible :)

Mubs

Sled 11-23-2007 05:23 PM

Thanks a lot guys, very helpfull!
I'll try them :)

Wildhoney 11-23-2007 06:44 PM

You're quite right, Mubs. I totally overlooked that.

Sled 12-07-2007 10:12 PM

Ok, iisbum's script has been working well.
Now I was wondering if there's any way, to show the code on the 2nd pageview?
I don't want my visitors to get hit by it on the first time they see the site, so 2nd pageview would be better, and then for the rest, again, once every 24 hours on the 2nd pageview.

Thanks!

iisbum 12-08-2007 07:02 AM

PHP Code:


if(!isset($_COOKIE['last_displayed'])) {
  
setcookie('last_displayed'"1"0'/');
} else {
  if (
$_COOKIE['last_displayed'] == "1") {
    
setcookie('last_displayed'date("Y-m-d h:i:s"), time() + (24 3600), '/');
    echo 
'Show Advert';
  } 


Basically you set the cookie on the first pageview to a control value, say 1, and to expire at the end of the current session, so if the user leaves the site after that first pageview if they comeback later that day they will again see the ad on the 2nd pageview.

If the cookie is set to 1, show the ad and set to another value, with an expiry in 24 hours.

Mubs

Jay 12-08-2007 09:05 AM

Sorry iisbum, your last tip was incorrect ;-)

In your example you set expire to '0', read this:

Quote:

Originally Posted by PHP Manual
If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).

You should probably set it to

(30 days)
PHP Code:

time() + (60 60 24 30

That way you get more ads, it will truly be (for 30 days, at least) not his first visit ^^

Rendair 12-08-2007 10:31 AM

You can always use Cronjobs, might be old style but hey :-)

iisbum 12-08-2007 02:21 PM

Quote:

Originally Posted by Jay (Post 5860)
Sorry iisbum, your last tip was incorrect ;-)

It wasn't a mistake if you read my explanation it was exactly what I intended.

If the user only looks at 1 page in that visit, you want the cookie to expire immediately, so that when they return to the site again later that day or tomorrow, they don't see the ad on their first pageview.

I guess that may not be what Sled wanted, as you say he'd show more ads. That's they way I would code it to maintain the fact that users only see the ad on their 2nd pageview of each visit, but only see the ad once every 24 hours at most.

Mubs

Sled 12-14-2007 12:33 PM

Thanks man! I'll try it :)


All times are GMT. The time now is 06:38 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0