TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack (1) Thread Tools Search this Thread Display Modes
Old 11-22-2007, 10:21 PM   1 links from elsewhere to this Post. Click to view. #1 (permalink)
The Contributor
 
Join Date: Oct 2007
Posts: 35
Thanks: 2
Sled is on a distinguished road
Default 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:
Sled is offline  
Reply With Quote
Old 11-22-2007, 10:49 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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';
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-23-2007, 05:37 AM   #3 (permalink)
The Wanderer
 
Join Date: Nov 2007
Posts: 20
Thanks: 1
iisbum is on a distinguished road
Default

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
iisbum is offline  
Reply With Quote
Old 11-23-2007, 05:23 PM   #4 (permalink)
The Contributor
 
Join Date: Oct 2007
Posts: 35
Thanks: 2
Sled is on a distinguished road
Default

Thanks a lot guys, very helpfull!
I'll try them :)
Sled is offline  
Reply With Quote
Old 11-23-2007, 06:44 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

You're quite right, Mubs. I totally overlooked that.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-07-2007, 10:12 PM   #6 (permalink)
The Contributor
 
Join Date: Oct 2007
Posts: 35
Thanks: 2
Sled is on a distinguished road
Default

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!
Sled is offline  
Reply With Quote
Old 12-08-2007, 07:02 AM   #7 (permalink)
The Wanderer
 
Join Date: Nov 2007
Posts: 20
Thanks: 1
iisbum is on a distinguished road
Default

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
iisbum is offline  
Reply With Quote
The Following User Says Thank You to iisbum For This Useful Post:
Sled (12-14-2007)
Old 12-08-2007, 09:05 AM   #8 (permalink)
Jay
The Contributor
Good Samaritan 
 
Join Date: Dec 2007
Posts: 60
Thanks: 5
Jay is on a distinguished road
Default

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
Jay is offline  
Reply With Quote
Old 12-08-2007, 10:31 AM   #9 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
Rendair is on a distinguished road
Default

You can always use Cronjobs, might be old style but hey
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 12-08-2007, 02:21 PM   #10 (permalink)
The Wanderer
 
Join Date: Nov 2007
Posts: 20
Thanks: 1
iisbum is on a distinguished road
Default

Quote:
Originally Posted by Jay View Post
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
iisbum is offline  
Reply With Quote
Old 12-14-2007, 12:33 PM   #11 (permalink)
The Contributor
 
Join Date: Oct 2007
Posts: 35
Thanks: 2
Sled is on a distinguished road
Default

Thanks man! I'll try it :)
Sled is offline  
Reply With Quote
Reply


LinkBacks (?)
LinkBack to this Thread: http://www.talkphp.com/general/1510-show-code-only-once-every-24-hours-each-user.html
Posted By For Type Date
Random background - Pixel2Life Forum This thread Refback 01-09-2008 06:52 AM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:54 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design