Tuesday, December 25, 2007

Simple Clickthru PHP

MySQL Create Table Statement:

CREATE TABLE `count_stuff` (
  `ip_number` varchar(16) NOT NULL default '',
  `time` datetime NOT NULL default '0000-00-00 00:00:00',
  `referer` varchar(128) NOT NULL default '',
  `target_file` varchar(128) NOT NULL default ''
) TYPE=MyISAM;
(redirect1.php) The redirect page that enters the clickthru info into database:
<html>
<head>
<meta http-equiv="REFRESH" content="0; 
URL=http://www.example.com/PRODUCTS.php">
</head>
</html>
<?php
$target_file
="Manual_Download";
$dbh=mysql_connect ("localhost"
                    
"db_username"
            
"password"
            or die 
            (
'I cannot connect to the database because: '
            
mysql_error());
mysql_select_db ("count_clicks");
$sql "INSERT INTO 
        `count_stuff` 
    ( `ip_number` ,
    `time` ,
    `referer`, 
    `target_file` ) "
;
$sql.= " VALUES ( '".$REMOTE_ADDR."',
         now() ,
     '"
.$_SERVER['HTTP_REFERER']."',
     '"
.$target_file."' );";
mysql_query($sql);
?>

This PHP will show how many have accessed the PRODUCT.pdf:
<?php
$dbh
=mysql_connect ("localhost"
                   
"db_user"
           
"password"
or die (
'I cannot connect to the database because: ' 
mysql_error());
mysql_select_db ("count_clicks");
$sql="SELECT COUNT(  `target_file` ) 
FROM  `count_stuff` 
WHERE  `target_file`='Manual_Download'"
;
$results=mysql_query($sql);
while(
$row mysql_fetch_row($results)) {
echo 
"This many people downloaded the manual:".$row[0];
}
?>

No comments: