I have been using PHP for approx 6 months and have been asked to implement a click tracker, page detail on day and count the number of clicks, however I have created the following code, which works of a fashion, I seem to get a huge amount of page hits which are blank. All data BTW is going to a MySQL DB.
Am I missing something?
The config.inc stores username, password, hostname & dbname.
Code: Select all
<?php
include('config.inc');
// include('sites.inc'); //for implementation in future
//if (in_array($oururl, $siteslist)){ //for implementation in future
// Get Page name without params
if (isset($_SERVER['HTTP_REFERER'])){
$pagename = $_SERVER ['HTTP_REFERER'];
// Remove anything after the ?
$page = explode("?", $pagename);
$pagedetail = $page[0];
//If no referer then don't count
} else {
exit();
}
//Todays Date
$hit_date = date('Y-m-d');
//Database connection, if no connection then exit
$db = @mysql_connect($hostname, $username, $password);
if( ! ($db = @mysql_connect($hostname, $username, $password)) ) {
echo "Database Error";
exit;
}
//store the update
mysql_select_db("$dbname",$db) or die("Log error");
$query="SELECT * FROM lite_hits WHERE hit_page = '$pagedetail' AND hit_date = '$hit_date'";
$result = mysql_query( $query, $db );
$num_rows = mysql_num_rows($result);
//if new record
if (empty($num_rows)) {
$q1 = " INSERT INTO lite_hits (hit_count,hit_date,hit_page) VALUES ('1','$hit_date','$pagedetail')";
mysql_query($q1) or die("$q1 failed because ".mysql_error());
} else {
//if updating existing record
if ($num_rows) {
$q2 = "UPDATE lite_hits SET hit_count = hit_count+1 WHERE hit_page = '$pagedetail' AND hit_date = '$hit_date' ";
mysql_query($q2) or die("$q2 failed because ".mysql_error());
}
}
//} else {
//exit();
//}
?>