Why does this insert twice?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

PHP and MYSql

I have the code below which logs a visitor to my site via logging the  
session id and other details in a table.  I only want to count a visit  
once so I have a check to see if the session id is in the table already  
then don't log it.  I don't want an entry in the event of a surfer  
hitting refresh on the browser or revisitng the page during a session.

However, not only is it logging the first initial visit during the  
session but it is also logging the second.  It does not log the third  
visit on.

Any ideas?

thanks in advance,

rg  www.pocketpcheaven.com

$valid = "yes";
$_SESSION[valid] = $valid;

include ('Connections/local.php');

// Log the visit first

$sessionid = session_id();
$ipaddress = GetHostByName($REMOTE_ADDR);
$agent = getenv("HTTP_USER_AGENT");
    // check to see if running ie
        if (preg_match("/PPC/i", "$agent"))  {
        $browser = "PPCIE";
    //check to see if running Firefox
        else if (preg_match("/Firefox/i", "$agent")) {
        $browser = "Firefox";
    // check to see if running opera
        else if (preg_match("/Opera/i", "$agent")) {
        $browser = "Opera";
    // check to see if running pocket pc browser
        else if (preg_match("/MSIE/i", "$agent")) {
    $browser = "IE";
    // if anything else just display the browser string
    else {
    $browser = $agent;

// but.. b4 that have a look to see if session id exist already, we only  
want to log new users
mysql_select_db($database_local, $local);
$sqlchecksession = "SELECT sessionid from visitors where sessionid =  
$result = @mysql_query($sqlchecksession, $local) or die(mysql_error());
$thenum = mysql_num_rows($result);
if ($thenum <=1 ) {

// if ok and not exist already then log the visit
mysql_select_db($database_local, $local);
$query_rslogvisit = "INSERT INTO visitors (sessionid, ipaddress,  
browser, visitdate, visittime) VALUES ('$sessionid', '$ipaddress',  
'$browser', current_date(), current_time())";
$result = mysql_query($query_rslogvisit) or die(mysql_error());

Re: Why does this insert twice?

On Sun, 04 Sep 2005 12:17:17 +0000, toedipper wrote:

Quoted text here. Click to load it
<snip code>
Quoted text here. Click to load it

You are checking if 1 or fewer rows exists. The comparison should be
less than, not less than or equal to.
Why not just create a unique index on the sessionid column? You would
then not need to bother checking for existence at all - MySQL will
simply refuse to insert a row if one with the same sessionid
already exists.

(Remove REMOVE for email address)  

Re: Why does this insert twice?

toedipper wrote:
Quoted text here. Click to load it

Maybe the problem is in the following line:

if ($thenum <=1 )

Just a wild shot.


Re: Why does this insert twice?

from toedipper contained the following:

Quoted text here. Click to load it

We've just had this.  Why are you storing the session ID?  Simply set a
session variable to true at the beginning of the session, check for it
before you write to the database, then set the session variable to


//write to database
//visitor already logged so do other stuff

Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/

Site Timeline