user2135867
user2135867

Reputation:

PHP Website Statistics

i have put this PHP code at the very top of my index.php page

<?php
session_start();
include 'includes/dbconnection.php';

$sql="INSERT into site_statistics (ipaddress, datetime, url) values ('".$_SERVER['REMOTE_ADDR']."', '".date("Y-m-d H:i:s")."', '".$_SERVER['PHP_SELF']."') ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
?>

It inserts into the database ok but it seems to always put multiple rows in the table when i just refresh the once

The session_start(); is used for my shopping cart so i cannot remove this

Upvotes: 0

Views: 167

Answers (2)

egig
egig

Reputation: 4430

If you need to update the field when page refresh, use

REPLACE INTO

in your sql query, but if not, you can check the database first using

if(...){
  ...
}

statement.

Upvotes: 1

bizzehdee
bizzehdee

Reputation: 20993

add in a column that is a varchar(32), make it the primary key, and insert the session id into it. ie.

$sql="INSERT into site_statistics (sid, ipaddress, datetime, url) values ('" . session_id() . "', '".$_SERVER['REMOTE_ADDR']."', '".date("Y-m-d H:i:s")."', '".$_SERVER['PHP_SELF']."') ";

the session id is unique to the visitor at that particular time, so it being the primary key would prevent multiple inserts of the same visitor on that particular visit.

Upvotes: 1

Related Questions