Version: 1.1

Type: Function

Category: HTTP

License: GNU General Public License

Description: STEP SPY is a simple yet useful PHP script for tracking the footsteps of visitors on your site. By including this script in all your pages, you can log such information as the IP of the visitor, what pages and when he/she visited, and in what order into a MySQL database. If you find this script useful, please let the author know.



<?

/*

This will count all the hits to your page and record them into a database.  Just place this at the top of your page and it will record the hit to your page and store it.  our html page must have the .php3 extention in order for this to work.

Before any of this:

create table stepspy
(id int not null,
session text not null,
url text not null,
thesequn int not null,
inpus text not null,
fecha timestamp);

*/


function make_url($url) {
        global $id,$orden;
        $orden++;
        if (empty($id)) {
                $aux =explode(" ",microtime());
                $id = md5( ($aux[1] + $aux[0])*100000 );
        }
        return $url."?id=" . $id . "&orden=". $orden ;
}

function almacenar() {
        global $id, $orden, $url, $REMOTE_ADDR;

        if (empty($REMOTE_ADDR)) $userip="desconocido";
        else $userip= $REMOTE_ADDR;

	$conn= pg_connect("server","port","database");
	$query = "INSERT INTO stepspy VALUES(max(id)+1, '$id', '$url', $orden, '$userip', current_timestamp);";
	$result = pg_exec($conn, $query);
	if(!$result)
	{
	echo("error recording the hit");
	}
pg_close($conn);
}

$url=("$PHP_SELF");
$final_url=make_url($url);
almacenar();

?>



<?

/*
this section will print the stats on the page.  Place this section where ever you want the stats to be located on the page.  I usually place this at the end of the page.

*/


$con = pg_connect("server", "port", "database");
$count = "select count(url) from stepspy where url = '$url';";
$result_count = pg_exec($con, $count);
$hit = pg_fetch_array($result_count, $counter);
$cip = "select distinct inpus from stepspy where url = '$url';";
$res_cip = pg_exec($con, $cip);
$numips = pg_numrows($res_cip);
echo("<font face=\"Arial,Helvetica,sans-serif\" size=\"2\"><div align=\"center\">This page ($url) has been hit $hit[0] times by $numips different clients.</div></font>");
pg_close($con);
?>