Hi,
In this post I will demonstrate how you can advertise on other website and keep track how many times the advertisement has been loaded in that website. This is again an example from GD library. For this I need a database to store the various images for advertisement. The database schema is as follows:
(Click here to download the entire source code with the full database)
1. Table for storing the various advertisement details
CREATE TABLE IF NOT EXISTS `advertisements` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`img` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
2. Table for keeping track of impressionsCREATE TABLE IF NOT EXISTS `adsense_track` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`ip` varchar(30) DEFAULT NULL,
`dt` datetime DEFAULT NULL,
`imgid` int(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
For you to advertise your image you need to supply a simple <img> to the external webpage or blog. In this it will look like:
<img src="http://localhost/php/adsense/advertisement.php?ad_id=1">
Now the source of the image is a PHP file with a parameter ad_id. Now in advertisement.php I have the following:
$ad_details=@mysql_fetch_array(mysql_query("select * from advertisements where id=".$_GET['ad_id']));
if(!empty($ad_details)){
mysql_query("insert into adsense_track set ip='".$_SERVER['REMOTE_ADDR']."', dt='".date("Y-m-d H:i:s",time())."', imgid='".$_GET['ad_id']."'");
$im=imagecreatefromjpeg($ad_details['img']);
header("content-type: image/jpeg");
imagejpeg($im,null,100);
}else{
$im=imagecreatefromjpeg("noimage.jpg");
header("content-type: image/jpeg");
imagejpeg($im,null,100);
}
Explanation:
First I am checking whether the ad_id is a valid ID or not by:
$ad_details=@mysql_fetch_array(mysql_query("select * from advertisements where id=".$_GET['ad_id']));
if(!empty($ad_details))
{
// code to generate the advertisement image
}
else{
$im=imagecreatefromjpeg("noimage.jpg"); // loading an error image
header("content-type: image/jpeg"); // setting the header to image type
imagejpeg($im,null,100); // generating the image
}
If the ID is valid this is what goes in the if(){} part:
mysql_query("insert into adsense_track set ip='".$_SERVER['REMOTE_ADDR']."', dt='".date("Y-m-d H:i:s",time())."', imgid='".$_GET['ad_id']."'");
$im=imagecreatefromjpeg($ad_details['img']);
header("content-type: image/jpeg");
imagejpeg($im,null,100);
First I am updating the impression tracking table for that particular advertisement ID and next it loads the image based on the image name stored in the database with respect to the given advertisement ID.
Initially the data in the database looks like:
Now I create a simple HTML file and place the advertisement script, "<img src="http://localhost/php/adsense/advertisement.php?ad_id=1">". This sample HTML file is basically the website where you are promoting your advertisements.The sample HTML page looks like:
As soon the advertisement loads on the right side of the HTML page, the impression table gets updated as follows:
Thus my impression tracking advertisement is ready. Download adsense.zip
You just need to make 1 change in the HTML in line <img src="http://localhost/php/adsense/advertisement.php?ad_id=1">. Change the path of the advertisement.php file with respect to your website.
:)
No comments:
Post a Comment