Why does this insert twice?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • toedipper

    Why does this insert twice?

    Hello,

    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



    <?
    session_start() ;
    $valid = "yes";
    $_SESSION[valid] = $valid;

    include ('Connections/local.php');

    // Log the visit first

    $sessionid = session_id();
    $ipaddress = GetHostByName($ REMOTE_ADDR);
    $agent = getenv("HTTP_US ER_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_loca l, $local);
    $sqlchecksessio n = "SELECT sessionid from visitors where sessionid =
    '$sessionid'";
    $result = @mysql_query($s qlchecksession, $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_loca l, $local);
    $query_rslogvis it = "INSERT INTO visitors (sessionid, ipaddress,
    browser, visitdate, visittime) VALUES ('$sessionid', '$ipaddress',
    '$browser', current_date(), current_time()) ";
    $result = mysql_query($qu ery_rslogvisit) or die(mysql_error ());
    }
  • Dave

    #2
    Re: Why does this insert twice?

    On Sun, 04 Sep 2005 12:17:17 +0000, toedipper wrote:
    [color=blue]
    > Hello,
    >
    > 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.
    >[/color]
    <snip code>[color=blue]
    > $sqlchecksessio n = "SELECT sessionid from visitors where sessionid =
    > '$sessionid'";
    > $result = @mysql_query($s qlchecksession, $local) or die(mysql_error ());
    > $thenum = mysql_num_rows( $result);
    > if ($thenum <=1 ) {[/color]

    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.

    <snip>
    --
    Dave <dave@REMOVEbun dook.com>
    (Remove REMOVE for email address)

    Comment

    • Darek

      #3
      Re: Why does this insert twice?


      toedipper wrote:[color=blue]
      > Hello,
      >
      > 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
      >
      >
      >
      > <?
      > session_start() ;
      > $valid = "yes";
      > $_SESSION[valid] = $valid;
      >
      > include ('Connections/local.php');
      >
      > // Log the visit first
      >
      > $sessionid = session_id();
      > $ipaddress = GetHostByName($ REMOTE_ADDR);
      > $agent = getenv("HTTP_US ER_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_loca l, $local);
      > $sqlchecksessio n = "SELECT sessionid from visitors where sessionid =
      > '$sessionid'";
      > $result = @mysql_query($s qlchecksession, $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_loca l, $local);
      > $query_rslogvis it = "INSERT INTO visitors (sessionid, ipaddress,
      > browser, visitdate, visittime) VALUES ('$sessionid', '$ipaddress',
      > '$browser', current_date(), current_time()) ";
      > $result = mysql_query($qu ery_rslogvisit) or die(mysql_error ());
      > }[/color]

      Maybe the problem is in the following line:

      if ($thenum <=1 )

      Just a wild shot.

      Regards

      Comment

      • Geoff Berrow

        #4
        Re: Why does this insert twice?

        I noticed that Message-ID: <hBBSe.3225$7p1 .3015@newsfe7-win.ntli.net>
        from toedipper contained the following:
        [color=blue]
        >
        >I have the code below which logs a visitor to my site via logging the
        >session id and other details in a table.[/color]

        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
        false.

        e.g.
        session_start() ;
        if(!isset($_SES SION['test'])){
        $_SESSION['test']=true;
        }

        if($_SESSION['test']){
        //write to database
        $_SESSION['test']=false;
        }
        else{
        //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/

        Comment

        Working...