Problem with code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cornishcat
Forum Newbie
Posts: 2
Joined: Thu Feb 19, 2009 3:32 am

Problem with code

Post by cornishcat »

Hi,

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();
//} 
?>
 
Can someone help, as this is driving me mad
expvice
Forum Newbie
Posts: 1
Joined: Thu Feb 19, 2009 10:44 am

Re: Problem with code

Post by expvice »

You don't have to spent hours, our expert has experienced it before and you can view the code by clicking here

http://www.expvice.com

İf you want to have online conversation with our expert click here

http://www.expvice.com/Contactus.aspx : online expert advice.
Post Reply