 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
11-22-2007, 10:21 PM
|
#1 (permalink)
|
|
The Contributor
Join Date: Oct 2007
Posts: 35
Thanks: 2
|
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:
|
|
|
|
11-22-2007, 10:49 PM
|
#2 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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.
|
|
|
11-23-2007, 05:37 AM
|
#3 (permalink)
|
|
The Wanderer
Join Date: Nov 2007
Posts: 20
Thanks: 1
|
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
|
|
|
|
11-23-2007, 05:23 PM
|
#4 (permalink)
|
|
The Contributor
Join Date: Oct 2007
Posts: 35
Thanks: 2
|
Thanks a lot guys, very helpfull!
I'll try them :)
|
|
|
|
11-23-2007, 06:44 PM
|
#5 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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.
|
|
|
12-07-2007, 10:12 PM
|
#6 (permalink)
|
|
The Contributor
Join Date: Oct 2007
Posts: 35
Thanks: 2
|
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!
|
|
|
|
12-08-2007, 07:02 AM
|
#7 (permalink)
|
|
The Wanderer
Join Date: Nov 2007
Posts: 20
Thanks: 1
|
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
|
|
|
|
|
The Following User Says Thank You to iisbum For This Useful Post:
|
|
12-08-2007, 09:05 AM
|
#8 (permalink)
|
|
The Contributor
Join Date: Dec 2007
Posts: 60
Thanks: 5
|
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 
|
|
|
|
12-08-2007, 02:21 PM
|
#9 (permalink)
|
|
The Wanderer
Join Date: Nov 2007
Posts: 20
Thanks: 1
|
Quote:
Originally Posted by Jay
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
|
|
|
|
12-08-2007, 10:31 AM
|
#10 (permalink)
|
|
The Addict
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
|
You can always use Cronjobs, might be old style but hey 
|
|
|
12-14-2007, 12:33 PM
|
#11 (permalink)
|
|
The Contributor
Join Date: Oct 2007
Posts: 35
Thanks: 2
|
Thanks man! I'll try it :)
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|