Forum Moderators: coopster
<?php
/*
ADSENSE CLICK LOGGER VIEWER AND RECORDER SCRIPT IN PHP
VERSION 1.0, March 20, 2004
To view your stats, open adsense_clicklogger.php in your browser.
SETUP:
1. create the table using the mysql create table command below.
2. enter the site database definitions
3. enter the clicklogger javascript code in your web pages
4. make sure that javascript code points to this script
*/
// These are the parameters for connecting to the database.
define ("SITE_DATABASE_HOSTNAME", "localhost");
define ("SITE_DATABASE_PASSWORD", "password");
define ("SITE_DATABASE_USERNAME", "account");
define ("SITE_DATABASE_DATABASE", "database");
/*
CREATE TABLE tblAdsenseClicks (
AdsenseClickID INT PRIMARY KEY AUTO_INCREMENT,
ClickDate DATETIME,
DestinationPage VARCHAR(255),
SourcePage VARCHAR(255),
RemoteAddress VARCHAR(255),
RemoteHost VARCHAR(255)
)
*/
if(isset($_GET['U'])) {
$DestinationPage = mysql_escape_string(substr(trim($_GET['U']),0,255));
} else {
$DestinationPage = "";
}
if(isset($_GET['R'])) {
$SourcePage = mysql_escape_string(substr(trim($_GET['R']),0,255));
} else {
$SourcePage = "";
}
if($DestinationPage!= "" && $SourcePage!= "")
{
$RemoteAddress = mysql_escape_string(substr(trim($_SERVER['REMOTE_ADDR']),0,255));
$RemoteHost = mysql_escape_string(substr(trim($_SERVER['REMOTE_HOST']),0,255));
$query = "INSERT INTO tblAdsenseClicks ".
" (ClickDate,DestinationPage,SourcePage,RemoteAddress,RemoteHost) ".
" VALUES (".
" '".date("Y-m-d H:i:s")."',".
" '".$DestinationPage."',".
" '".$SourcePage."',".
" '".$RemoteAddress."',".
" '".$RemoteHost."')";
run_query($query);
exit();
}
// Show the statistics page if the U and R variables are not given
?>
<html><head><title>Adsense click stats</title>
<style type="text/css">
table.clickstats {
border-collapse: collapse;
}
table.clickstats th {
color: white;
background-color: #000080;
border: 1px solid #999999;
padding: 3px;
}
table.clickstats td {
border: 1px solid #999999;
text-align: center;
}
</style>
</head>
<body><?php
$query = "SELECT ClickDate,DestinationPage,SourcePage,RemoteAddress,RemoteHost ".
" FROM tblAdsenseClicks ORDER BY ClickDate ";
print_query($query,"clickstats");
print "</body></html>";
// Connects to the site defined database and returns the connection handle.
// Uses a persistent connection and does error checking.
function connect_to_site_database()
{
// Connect to the mysql server
$link = mysql_pconnect(constant("SITE_DATABASE_HOSTNAME"),
constant("SITE_DATABASE_USERNAME"),
constant("SITE_DATABASE_PASSWORD")) or
die("Unable to connect to site database!");
// Select the database
mysql_select_db(constant("SITE_DATABASE_DATABASE"))
or die("Unable to select database: ".constant("SITE_DATABASE_DATABASE"));
return $link;
}
// Standard function to run a query, with error checking.
// Returns the resultset.
function run_query($query)
{
// Connect to the database
connect_to_site_database();
// Run the query
$result = mysql_query ($query) or die ("Invalid query: $query");
// Return the result set
return $result;
}
// prints the query results in a table with the given table style class
function print_query($query, $table_class)
{
connect_to_site_database();
// Select all the records from the addressbook and store them in $result
$result = mysql_query ($query) or die ("Invalid query: $query");
// Get the number of columns (or fields) in the results
$num_fields = mysql_num_fields ($result);
// Get the number of rows in the results
$num_rows = mysql_num_rows ($result);
// Start printing the table to show all the records
echo "<table class=\"$table_class\">\n";
echo "<tr>";
// Print out all the field names as the first row table header
$i=0;
while ($i < $num_fields) {
// Get the field name from the result
$fname = mysql_field_name ($result, $i);
echo "<th>".$fname."</th>";
$i=$i+1;
}
echo "</tr>\n";
// Now print out all the data
while ($row = mysql_fetch_row ($result)) {
echo "<tr>";
$k=0;
while ($k < $num_fields) {
echo "<td>".$row[$k]."</td>\n";
$k=$k+1;
}
echo "</tr>\n";
}
echo "</table>\n";
// Do a little clean up
mysql_free_result($result);
}
?>
I can tell you this, the create table won't work in your script as is. In order for php to execute that part of the script is should be formed a little differently, thus:
$sql = "CREATE TABLE tblAdsenseClicks (
AdsenseClickID INT PRIMARY KEY AUTO_INCREMENT,
ClickDate DATETIME,
DestinationPage VARCHAR(255),
SourcePage VARCHAR(255),
RemoteAddress VARCHAR(255),
RemoteHost VARCHAR(255)
)";
$result = mysql_query($sql);
I didn't really look over anything else past that...