your opinion on this type of counter
Posted: Sun Jun 12, 2005 2:30 am
I have build a page traffic analizer/counter and I would like to know what you think of it. It is good? Does it have buggs? Can the script make fals entries into the db? How can I optimeze it?
Code: Select all
<?php
session_start();
mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());
$page = $_GET['page'];
$ip = $_SERVER['REMOTE_ADDR'];
$host = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$user = $_SESSION['user'];
$last= date("Y-m-d");
if (empty($_SESSION['user'])) //for index page or any other that doesnt need
//you to be authenticated
{
$select = "SELECT * FROM `counter` WHERE ip='".$ip."' LIMIT 1"; //check for ip
$result = mysql_query($select);
if (mysql_affected_rows()<1) //if the ip is not there it means
//this persone is for the first time here
{
$insert = "INSERT INTO counter (ip, host, nr, last, ".$page.") VALUES ('".$ip."', '".$host."',1,'".$last."',1 )";
$result = mysql_query($insert);
}
elseif (mysql_affected_rows()>0) //if ip is there, check for the last view of the page
{
$row = mysql_fetch_assoc($result);
if ($row['last'] != $last)
{
$update = "UPDATE counter SET nr=nr+1, last='".$last."' WHERE ip='".$ip."'";
$resultU = mysql_query($update);
}
elseif ($row['last'] == $last)
{
$update1 = "UPDATE counter SET ".$page."=".$page."+1 WHERE ip='".$ip."'";
$resultU1 = mysql_query($update1);
}
}
}
elseif (!empty($_SESSION['user'])) //user is logged in
{
$select = "SELECT * FROM `counter` WHERE `user` ='".$user."' LIMIT 1";
$result = mysql_query($select);
if(mysql_affected_rows()<1)
{
$update2 = "UPDATE `counter` SET user='".$user."' WHERE ip='".$ip."'";
$resulU2 = mysql_query($update2);
}
elseif(mysql_affected_rows()>0)
{
$select = "SELECT * FROM `counter` WHERE `user` ='".$user."' LIMIT 1";
$result = mysql_query($select);
$row = mysql_fetch_assoc($result);
if ($row['last'] != $last)
{
$update = "UPDATE `counter` SET ip='".$ip."', host='".$host."', nr=nr+1, last='".$last."', ".$page."=".$page."+1 WHERE user='".$user."'";
$resulU = mysql_query($update);
}
elseif ($row['last'] == $last )
{
$update1 = "UPDATE counter SET ".$page."=".$page."+1 WHERE user='".$user."'";
$resultU1 = mysql_query($update1);
}
}
}
?>