Help on click tracking code

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

    Help on click tracking code

    I write a piece of code to tracking the number of clicks on a link
    each day. The link usu. gets about 3000-6000 clicks per day. I just
    store today's click count in a one-line text file, like,

    01/07/2004 1234

    The first field is the date and the second field is number of clicks.
    I just read the current count, increase by 1 and then write back. When
    another day comes, I write today's count to mysql DB.

    The problem I am having is that sometimes during a single day, the
    click count seems to be reset to zero and starts counting from zero
    again. Normally I'd get at least 3000 clicks a day. But sometimes, the
    clicks I've got are only around 600. I am sure there is something
    wrong with my code. I've pasted my code. I'd appreciate if sombody can
    help me with some tips.

    $hitcount=0;
    $today=date("m/d/Y");
    $hitdate=$today ;

    $fdaycount="day count.txt";

    if (file_exists($f daycount))
    {
    //read out today's count from file
    $handle=fopen($ fdaycount,"r");
    if (!$handle) exit(0);
    if (!feof($handle) )
    {
    $s=fgets($handl e);
    $p=strpos($s," ");
    $hitdate=substr ($s,0,$p);
    $hitcount=(inte ger)substr($s,$ p+1);
    }
    fclose($handle) ;
    }

    if ($hitdate==$tod ay)
    {
    //increase todays' count by 1
    $hitcount++;
    $fp=fopen($fday count,"w");
    if (!$fp) exit(0);
    $s=$today." ".$hitcount ;
    fputs($fp,$s);
    fclose($fp);
    }
    else
    {
    //write today's data into database
    .....
    //

    //reset today's count to 1
    $fp=fopen($fday count,"w");
    if (!$fp) exit(0);
    $s=$today." 1";
    fputs($fp,$s);
    fclose($fp);
    }
  • CountScubula

    #2
    Re: Help on click tracking code

    "Shanfeng Cheng" <shanfengcheng@ yahoo.com> wrote in message
    news:c8638372.0 401080048.24fb7 516@posting.goo gle.com...[color=blue]
    > I write a piece of code to tracking the number of clicks on a link
    > each day. The link usu. gets about 3000-6000 clicks per day. I just
    > store today's click count in a one-line text file, like,
    >
    > 01/07/2004 1234
    >
    > The first field is the date and the second field is number of clicks.
    > I just read the current count, increase by 1 and then write back. When
    > another day comes, I write today's count to mysql DB.
    >
    > The problem I am having is that sometimes during a single day, the
    > click count seems to be reset to zero and starts counting from zero
    > again. Normally I'd get at least 3000 clicks a day. But sometimes, the
    > clicks I've got are only around 600. I am sure there is something
    > wrong with my code. I've pasted my code. I'd appreciate if sombody can
    > help me with some tips.
    >
    > $hitcount=0;
    > $today=date("m/d/Y");
    > $hitdate=$today ;
    >
    > $fdaycount="day count.txt";
    >
    > if (file_exists($f daycount))
    > {
    > //read out today's count from file
    > $handle=fopen($ fdaycount,"r");
    > if (!$handle) exit(0);
    > if (!feof($handle) )
    > {
    > $s=fgets($handl e);
    > $p=strpos($s," ");
    > $hitdate=substr ($s,0,$p);
    > $hitcount=(inte ger)substr($s,$ p+1);
    > }
    > fclose($handle) ;
    > }
    >
    > if ($hitdate==$tod ay)
    > {
    > //increase todays' count by 1
    > $hitcount++;
    > $fp=fopen($fday count,"w");
    > if (!$fp) exit(0);
    > $s=$today." ".$hitcount ;
    > fputs($fp,$s);
    > fclose($fp);
    > }
    > else
    > {
    > //write today's data into database
    > .....
    > //
    >
    > //reset today's count to 1
    > $fp=fopen($fday count,"w");
    > if (!$fp) exit(0);
    > $s=$today." 1";
    > fputs($fp,$s);
    > fclose($fp);
    > }[/color]

    Proccess the logs, its cleaner,

    run a cron job, and place the results in your dbase


    --
    Mike Bradley
    http://www.gzentools.com -- free online php tools


    Comment

    • Herb Kauhry

      #3
      Re: Help on click tracking code

      Look what happens in the code when the file doesn't exist. Now consider
      what happens if two (or two hundred!) sessions try to open the file at the
      same time. What happens when you use file_exists() on a file that someone
      else has already opened for write? Does file_exist return false? Out of
      200 simultaneous attempts, how many actually would succeed in updating the
      counter?

      You've got a database - use that and manage the count like this: UPDATE
      hitcountertable SET hitcount = hitcount +1. The DBMS will serialize access
      to the hitcount, be faster and work better.
      --

      "Shanfeng Cheng" <shanfengcheng@ yahoo.com> wrote in message
      news:c8638372.0 401080048.24fb7 516@posting.goo gle.com...[color=blue]
      > I write a piece of code to tracking the number of clicks on a link
      > each day. The link usu. gets about 3000-6000 clicks per day. I just
      > store today's click count in a one-line text file, like,
      >
      > 01/07/2004 1234
      >
      > The first field is the date and the second field is number of clicks.
      > I just read the current count, increase by 1 and then write back. When
      > another day comes, I write today's count to mysql DB.
      >
      > The problem I am having is that sometimes during a single day, the
      > click count seems to be reset to zero and starts counting from zero
      > again. Normally I'd get at least 3000 clicks a day. But sometimes, the
      > clicks I've got are only around 600. I am sure there is something
      > wrong with my code. I've pasted my code. I'd appreciate if sombody can
      > help me with some tips.
      >
      > $hitcount=0;
      > $today=date("m/d/Y");
      > $hitdate=$today ;
      >
      > $fdaycount="day count.txt";
      >
      > if (file_exists($f daycount))
      > {
      > //read out today's count from file
      > $handle=fopen($ fdaycount,"r");
      > if (!$handle) exit(0);
      > if (!feof($handle) )
      > {
      > $s=fgets($handl e);
      > $p=strpos($s," ");
      > $hitdate=substr ($s,0,$p);
      > $hitcount=(inte ger)substr($s,$ p+1);
      > }
      > fclose($handle) ;
      > }
      >
      > if ($hitdate==$tod ay)
      > {
      > //increase todays' count by 1
      > $hitcount++;
      > $fp=fopen($fday count,"w");
      > if (!$fp) exit(0);
      > $s=$today." ".$hitcount ;
      > fputs($fp,$s);
      > fclose($fp);
      > }
      > else
      > {
      > //write today's data into database
      > .....
      > //
      >
      > //reset today's count to 1
      > $fp=fopen($fday count,"w");
      > if (!$fp) exit(0);
      > $s=$today." 1";
      > fputs($fp,$s);
      > fclose($fp);
      > }[/color]


      Comment

      • Shanfeng Cheng

        #4
        Re: Help on click tracking code

        Thanks. I did find problem with the file_exists() function call. It
        does return false sometimes.

        But I don't know why updating a database field can be faster. You have
        to take time making the database connection each time. And somehow my
        ISP limits the number of simultaneous MySQL connections.


        "Herb Kauhry" <tiptoe@tulips. me> wrote in message news:<POCdnW1O4 41Fc2CiRVn-sw@comcast.com> ...[color=blue]
        > Look what happens in the code when the file doesn't exist. Now consider
        > what happens if two (or two hundred!) sessions try to open the file at the
        > same time. What happens when you use file_exists() on a file that someone
        > else has already opened for write? Does file_exist return false? Out of
        > 200 simultaneous attempts, how many actually would succeed in updating the
        > counter?
        >
        > You've got a database - use that and manage the count like this: UPDATE
        > hitcountertable SET hitcount = hitcount +1. The DBMS will serialize access
        > to the hitcount, be faster and work better.
        > --
        >
        > "Shanfeng Cheng" <shanfengcheng@ yahoo.com> wrote in message
        > news:c8638372.0 401080048.24fb7 516@posting.goo gle.com...[color=green]
        > > I write a piece of code to tracking the number of clicks on a link
        > > each day. The link usu. gets about 3000-6000 clicks per day. I just
        > > store today's click count in a one-line text file, like,
        > >
        > > 01/07/2004 1234
        > >
        > > The first field is the date and the second field is number of clicks.
        > > I just read the current count, increase by 1 and then write back. When
        > > another day comes, I write today's count to mysql DB.
        > >
        > > The problem I am having is that sometimes during a single day, the
        > > click count seems to be reset to zero and starts counting from zero
        > > again. Normally I'd get at least 3000 clicks a day. But sometimes, the
        > > clicks I've got are only around 600. I am sure there is something
        > > wrong with my code. I've pasted my code. I'd appreciate if sombody can
        > > help me with some tips.
        > >
        > > $hitcount=0;
        > > $today=date("m/d/Y");
        > > $hitdate=$today ;
        > >
        > > $fdaycount="day count.txt";
        > >
        > > if (file_exists($f daycount))
        > > {
        > > //read out today's count from file
        > > $handle=fopen($ fdaycount,"r");
        > > if (!$handle) exit(0);
        > > if (!feof($handle) )
        > > {
        > > $s=fgets($handl e);
        > > $p=strpos($s," ");
        > > $hitdate=substr ($s,0,$p);
        > > $hitcount=(inte ger)substr($s,$ p+1);
        > > }[/color]
        > fclose($handle) ;[color=green]
        > > }
        > >
        > > if ($hitdate==$tod ay)
        > > {
        > > //increase todays' count by 1
        > > $hitcount++;
        > > $fp=fopen($fday count,"w");
        > > if (!$fp) exit(0);
        > > $s=$today." ".$hitcount ;
        > > fputs($fp,$s);
        > > fclose($fp);
        > > }
        > > else
        > > {
        > > //write today's data into database
        > > .....
        > > //
        > >
        > > //reset today's count to 1
        > > $fp=fopen($fday count,"w");
        > > if (!$fp) exit(0);
        > > $s=$today." 1";
        > > fputs($fp,$s);
        > > fclose($fp);
        > > }[/color][/color]

        Comment

        Working...