Soheil
Soheil

Reputation: 113

is it possible to count how many times an image or flash file is viewed on my server

I want to put an advertisement of my site on some of my friends' sites and I want to see how many times these advertisements are viewed from each website. I'm searching for a solution for doing this.

Does Apache log how many times a file is requested from my server or is it possible to create a htaccess file to do this. I don't know where should I start so if you guys know about it please redirect me to right place.

Upvotes: 2

Views: 2318

Answers (3)

Radon8472
Radon8472

Reputation: 5001

The suggestions of the previus posts are quite good, to make it complete do this:

create a php file on your server, what outputs your ads and place your ads in you friends pages like this for images:

<img src="http://yourdomain.com/output.php?type=jpg&id=X">

or for flash

<embed src="http://yourdomain.com/output.php?type=swf&id=X" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash">
</embed>

in you php file you can check the id to load the matching ads (you should have a Database or a csv file with a list of you different adds.

<?php
  if(isset($_GET["id"]))
  {
    $list = load_addlist_from_db(); // <----- here you should write you own function to load your list of possible adds e.g. as array
    if(array_key_exists($_GET["id"], $list))
    {
      $filename = $list[$_GET["id"]];
      if(is_readable($filename))
      {
        header("Content-Length: ".filesize($filename));
        // set the last modification time of downloadfile or phpscript, whatever is newer
        $modtime_str = gmdate("D, d M Y H:i:s", max(filemtime($filename),getlastmod())) . " GMT";
        header("Last-Modified: ". $modtime_str);
        switch($_GET["type"])
        {
          case "jpg":
          case "jpeg": 
          {
            header("Content-Type: image/jpg"); // coul also be "image/png", "image/gif" depending on what file you like to output
          }
          case "swf":
          {
            header("Content-Type: application/x-shockwave-flash");
          }
        }
/*
 * here you can count the request to this add (or use the bbclone code below)
 * check $_SERVER['HTTP_REFERER'] to get information from what page the file was loaded 
 */
        readfile($filename);
      }
      else $file_not_found = true;
    }
    else $file_not_found = true;
  }
  else $file_not_found = true;

  if($file_not_found)
  {
    // that tells the browser, that the requestes file doas not exist
    header("Status: 404 Not Found",true,404);
  }
?>

In this file you can count you requests in an own Database / file, or you use BBClone, that counts even the Browsers and operating systems of you visitors.

You can include BBclone in your output.php like this:

define("_BBC_PAGE_NAME",basename($filename));
define("_BBCLONE_DIR", $_SERVER["DOCUMENT_ROOT"].DIRECTORY_SEPARATOR."bbclone");
define("COUNTER", _BBCLONE_DIR."mark_page.php");
include_once(COUNTER);

Upvotes: 3

Patrick
Patrick

Reputation: 922

Create a php file that shows an image like this:

    if(file_exists($path))
    { 
        header('Content-Length: '.filesize($path));
        header('Content-Type: image/jpg');
        header('Content-Disposition: inline; filename="'.$path.'";');
        echo file_get_contents($path);
        exit;
    }

Now you can add PHP code before the image gets put out and you can track the views. Just make sure you are not putting out anything before the header() call. Not even a space or HTML tag (it will break the image otherwise).

You can use .htaccess to show the file with a .jpg ending.

Upvotes: 4

Mayank Sharma
Mayank Sharma

Reputation: 829

You can put an image pixel like below, and give them to include in their file where they are displaying your flash file:

<img src="http://yourdomain.com/flashcount.php" style="display:none" />

In flashcount.php, you can get the information of the domain it was executed using $_SERVER using referrer and store its click and server info over some database table.

Upvotes: 0

Related Questions