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: