Make Your Own Cool Drop Down Ad's - Tracking the number of click-thrus
(Page 6 of 7 )
Remember back to the admin.php script where the number of impressions, click thru’s and click-thru rate is listed for each banner. As we already know, the number of impressions is incremented whenever the viewBanner function is called. Track.php on the other hand increments the number of click-thru's for each banner and redirects the user to the destination link for that banner, depending on whether or not they clicked on the top or bottom banner.
Track.php starts by connecting to our MySQL database and incrementing the numClicks field for the selected banner:
@mysql_query("update banners set numClicks = numClicks + 1 where bannerId = " . $_GET["bannerId"]); Remember that the banners ID is passed as the bannerId query string value, so that's why $_GET["bannerId"] is used in the SQL query. After the number of clicks has been incremented, it's a simple matter of determining if the user clicked on the top or bottom banner. This is accomplished by checking the img query string variable:
// Redirect the user to the link
if($_GET["img"] == "top")
{
// Get the link for the top banner
$result = @mysql_query("select link1 from banners where bannerId = " . $_GET["bannerId"]);
}
else
{
// Get the link for the bottom banner
$result = @mysql_query("select link2 from banners where bannerId = " . $_GET["bannerId"]);
} Depending on which banner was clicked, we retrieve either the link1 or link2 field from the banners table. Once retrieved, we make sure that field was indeed returned and use the header function to redirect the user to that link:
if($row = mysql_fetch_row($result))
{
header("Location: " . $row[0]);
} Sending the binary image data As we saw earlier, the viewBanner function sets the src of each banner image to something like getbanner.php?bannerId=2&img=top. Getbanner.php connects to the database and retrieves the blob data for the banners image as well as its MIME type, depending on whether img is top or bottom:
if($_GET["img"] == "top")
{
// Get the top image for the banner
$result = mysql_query("select data1, type1 from banners where bannerId = " . $_GET["bannerId"]);
}
else
{
// Get the bottom image for the banner
$result = mysql_query("select data2, type2 from banners where bannerId = " . $_GET["bannerId"]);
} Once the image and MIME type are retrieved, it's simply a matter of calling the header function to set the type of output and then echoing the binary data to the browser, like this:
if($row = mysql_fetch_row($result))
{
header("Content-type: " . $row[1]);
echo $row[0];
} If you want to test getbanner.php then you can fire up your browser and visit http://localhost/getbanner.php?bannerId=1&img=top, assuming that you have added a banner with admin.php and that the scripts are installed on your local machine.
Next: Conclusion >>
More HTML Articles
More By Tim Pabst