Jump to content

Limit views per 24 hours


TEENFRONT

Recommended Posts

Hey

 

I have a little custom ad server script i made that just simply selects a random advertisers ad code and shows the advert and this works great.

 

But now, I have an advertiser who wants to only show 4 impressions per user, per 24 hours. Im just totally stumped how to go about this.

 

Whats the best way to limit showing a visitors something to x times per 24 hours?

 

So my ad script runs, randomley selectcs an ad code but not one thats been shown to the visitors 4 times before.

 

This is my "show advert" function that i call from around my site.

 

function display_ad($ad_size) {

$result = mysql_query("SELECT * FROM tags WHERE ad_size='$ad_size' AND status='1'  ORDER BY rand() LIMIT 1");
$row = mysql_fetch_array($result);

return $row['tag'];
}

 

As you can see as people navigate through the site, this ad code will show ads randomley. Im just clueless where to start "limiting ad views per 24 hours, per user".

 

Any help or guidance would be very much appreciated!

Link to comment
Share on other sites

This is not a simple endeavor.  It's going to require a custom solution, and while I can suggest to you a hack that can probably developed and tested in an hour or so, the first question you might want to ask yourself is-- is it worth doing this for this one client?  If the money amount is worth it, then -- sure.

 

So you have 2 options --> cookie as p2grace suggested.  Pros- will work nicely and limited overhead for your server.  Cons-- debugging testing harder.  Doesn't work for anyone who has cookies set off.  Depending on the nature of your site, this may be acceptable.

 

Serverside solution -> pros, impervious to clientside environment, doesn't require cookie debugging or intrusive installation of cookie code.  Cons: tracking only by IP, some people share IP's hence they will artficially throttle views.

 

I'd rather not go into the specifics of both, so it would be good if you expressed a preference for one solution or the other.

Link to comment
Share on other sites

if you doing this for many adverts

and there are a max amount, and size that u can save in a cookie

so, u cant do this for all of  your ads

u know users might load 100 of your ad, if they stays that long

so, this isnt a good idea

and with this method, a user will be able to modify it (go to temp folder to change the saved text)

and it wont fits your client's need

u know there will be this kind of person, although like 0.01%, but still, u should cover this bug

Link to comment
Share on other sites

Ok, well, php has the nice wrapper function setCookie.  The problem with cookies is that they go into the http header, so this must be set before any output is sent.  Because session handling typically involves setting a cookie, sessions tend to have the same issues.  In other words, you need to make sure that you do this in a way where no output has been returned prior to your function being called.  The pseudocode to this, you write 2 functions:

 

function adViews() returns count of views in last 24 hours.

 

All this function need do is check for the value of the cookie.

 

function adViews() {
  if (isset($_COOKIE['adViews']) && is_array($_COOKIE['adViews']) {
     $adView = $_COOKIE['adView'];
     foreach ($adView as $name => $value) {
        if ($name == 'viewcount') {
            return (int) $value;
        }
     }
     return 0;
  } else {
     return 0;
  }
}

 

In your ad code query, what I'd suggest is that you alter that query to include a "WHERE tag_id NOT IN (...)" clause when the results of the adViews() function is > 3.  I don't know the schema of the tags table, so I'm guessing at how you identify this ad(s).  So in essence, when the ad is this special ad, the query simply excludes it from the result set, no muss no fuss.

 

Now you simply need to deal with the possibility that the person has seen the ad.  This again, may be tricky, because it's a chicken and egg situation.  Prior to even showing them the ad, you need to count it as being shown, if you understand my meaning here.  If you already have sessions for everyone, you might be able to substitute a session variable, and use that to create the variable when you start up the session. 

 

What this function needs to do, is check for the cookie.  If it exists increment its value, otherwise create it with setCookie.  There's no way to "update" a cookie, so updating it involves setting it again with setCookie.

 

function countViews() {
   $firstdate = time();
   $viewcount = 0;
   if (isset($_COOKIE['adViews']) && is_array($_COOKIE['adViews']) {
      $adViews = $_COOKIE['adViews'];
      foreach ($adViews as $name => $value) {
         if ($name == 'viewcount') {
           $viewcount = (int) $value;
         } elseif ($name == 'firstdate') {
            $firstdate = $value;
         }         
      }
   }
   // increment the count
   $viewcount++;
   // Set the cookie
   setCookie("addViews[firstdate]", "$firstdate", $firstdate + 86400, '/', '.yourdomain.com');
   setCookie("addViews[viewcount]", "$viewcount", $firstdate + 86400, '/', '.yourdomain.com');    
}

 

I by no means tested any of this code, just typed it in off the top of my head, so don't be surprised if there are issues.

Link to comment
Share on other sites

I think your close Gizmola, but there is a serious flaw in the code.

It only supports one ad, 'viewcount'.

But it does seem to be on track.

 

You will probably add some fields to the db, to have these conditions in place. and use most of what Gizmola has said/coded as a guide.

 

and depending on the amount of ads, you may want to avoid a cookie altogether. if its low amount of ads, than it will work fantastic.

but if u have a large number of ads, storing this amount of info in a cookie could exceed limits of the max size cookie (4kb/4096 bytes);

 

The hard part is going to be, ad selection, as Gizmola said, in order to set a cookie, nothing should be sent for display.

so the ad selection, should be above your display section, in order to set/adjust a cookie

 

 

Link to comment
Share on other sites

I think your close Gizmola, but there is a serious flaw in the code.

It only supports one ad, 'viewcount'.

But it does seem to be on track.

 

You will probably add some fields to the db, to have these conditions in place. and use most of what Gizmola has said/coded as a guide.

 

and depending on the amount of ads, you may want to avoid a cookie altogether. if its low amount of ads, than it will work fantastic.

but if u have a large number of ads, storing this amount of info in a cookie could exceed limits of the max size cookie (4kb/4096 bytes);

 

The hard part is going to be, ad selection, as Gizmola said, in order to set a cookie, nothing should be sent for display.

so the ad selection, should be above your display section, in order to set/adjust a cookie

 

 

I guess this falls under assumptions -- my assumption is that this is a one off, so whether it's one ad or 5 from this publisher, it's only going to allow for 4 views of *any* of those ads.

 

Also, again, I assume that adViews() would be used inside of his existing display_ad function.  Could be something as simple as:

 

 

function display_ad($ad_size) {
  $customerviews = adViews();

  If ($customerviews > 3) {
     // Has already seen the ad 4 times
     // Don't show ads 232, 233, or 234
     $sql = "SELECT * FROM tags WHERE add_id NOT IN (232, 233, 234) AND ad_size='$ad_size' AND status='1'  ORDER BY rand() LIMIT 1"
  } else {
    // normal query
     $sql = "SELECT * FROM tags WHERE ad_size='$ad_size' AND status='1'  ORDER BY rand() LIMIT 1"
  } 

  $result = mysql_query($query);
  
  $row = mysql_fetch_array($result);
  $countads = array(232, 233, 234);
  foreach ($countads as $value) {
     if ($row['add_id'] == $value) {
       countViews();
     } 
  }

return $row['tag'];
}

 

This could certainly be refactored into something more generic, and possibly database driven, but I'm not going there ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.