That guy
That guy

Reputation: 339

How to check for existence of a cookie every x seconds

My video sites get a very high click rate on video ads. So I'm creating an affiliates program plugin for my php site.

To check if my video ads were clicked I need to check for the existence of a cookie. So I have
written a php script that basically checks for the existence of a cookie in a loop.

I have understood that the user can click on a video ad at any time so I thought using a loop would be a good idea. It turns out that if I have the loop on, the rest of the page wont load until the loop criteria is satisfied. So I imagine having this check done every minute or so but I don't know how to do it.Any way I think I have said enough. Below is the php code the I've written.

$cookie1= $_COOKIE['PBCBD2A0PBP3D31B'];
$cookie2= $_COOKIE['PBC0622FPBP3D31B'];
$cookie3= $_COOKIE['PBC669C9PBP3D31B'];
$count= "1";
while ($count >0 )
{
if (isset($cookie1) || isset($cookie2) || isset($cookie3))
    {$count= "0"; echo "You clicked on an AD";} else{$count= "1";}
}

Upvotes: 0

Views: 1284

Answers (5)

Cypress Frankenfeld
Cypress Frankenfeld

Reputation: 2537

I agree with Matt. You would be better off detecting the cookie only in the instance when a cookie is likely to change (when they click on the video). However, it depends on the exact situation. Are you setting the cookies, or are the cookies being set by an external site when they click the ads?

EDIT:

Your two goals seem to be:

  1. Determining what ads to serve based on the cookies attached to the user.
  2. Logging which users click on which ads.

If you are generating all the ads in PHP and serving them to the user, then before you choose which ads to display, check the user's cookie once and go from there.

If you are placing the ads in your page with javascript, you should check for the cookies in javascript every time you refresh the ads.

In any case, abandon the loops. Simply check for cookies directly before the ads are served up.

In oder to determine which users click on which ads, you should detect the clicks in javascript, and log them to your server using an AJAX.

Upvotes: 1

Johndave Decano
Johndave Decano

Reputation: 2113

Use ajax and setInterval function

setInterval(function(){
    $.ajax({
         type:"POST",
         url: "the_php_that_checks_cookie.php",
         data:"check=true",
         success:function(data){
               //alert the message sent by the php file
               alert(data);
         }
    });
},5000);

Send ajax request every 5 seconds and In your PHP file,

if(isset($_POST['check']){

    $cookie1= $_COOKIE['PBCBD2A0PBP3D31B'];
    $cookie2= $_COOKIE['PBC0622FPBP3D31B'];
    $cookie3= $_COOKIE['PBC669C9PBP3D31B'];
    $count= "1";

  while ($count >0 )
    {
      if (isset($cookie1) || isset($cookie2) || isset($cookie3))
      {$count= "0"; echo "You clicked on an AD";} else{$count= "1";}
    }
}

Upvotes: 1

Palladium
Palladium

Reputation: 3763

This is a basic issue of PHP vs JavaScript.

If you want to check for a cookie every minute using PHP, you'll have to refresh the page every minute. This is obviously very uneconomical for a great variety of reasons, the least of which is that you're checking for something on the client side using server-side code - by compulsory page-refreshing every minute, no less.

Based on your description of what you want to do, I suggest using JavaScript instead to check for cookies (the page in the link shows how to make and check for a test cookie; modify to suit your needs) and running this function using setInterval().

Upvotes: 2

Lawrence Barsanti
Lawrence Barsanti

Reputation: 33302

Your approach will not work. PHP is used to generate a response (i.e. the html, javascript, etc... that makes up your page). Once it is generated, the response is sent to the user and your PHP script is done. It is up to the client (think JavaScript) to notify you of any user actions. You could add an onclick event that sends a new request to your sever which allows you to detect ad clicks.

Upvotes: 1

Matt
Matt

Reputation: 7040

Use the javascript onclick event to run an AJAX call which checks for the existence of the cookie.

Upvotes: 3

Related Questions