Creating a simple hitcounter for PHP web pages. (saves to files)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tony19760619@gmail.com

    Creating a simple hitcounter for PHP web pages. (saves to files)

    To make this work on a single page you will need to create/modify 4
    files.
    So here goes...

    Create a file called counter.php and put following code in it:
    <?php
    //Counter
    $count_my_page = ("hitcounter.tx t");
    $hits = file($count_my_ page);
    $hits[0] ++;
    $hit_count = $hits[0];
    $fp = fopen($count_my _page , "w");
    fputs($fp , "$hits[0]");
    fclose($fp);
    //echo $hits[0];

    //Details to add to list
    $count_my_page = ("hitcounter.cs v");
    $fp = fopen($count_my _page , "a");
    $ip=$_SERVER[REMOTE_ADDR];
    $dnsname = gethostbyaddr($ ip);
    $referer=$_SERV ER[HTTP_REFERER];
    $page=$_SERVER[PHP_SELF];
    $now = date( "Y/m/d H:i:s", time() );
    fputs($fp , "$now,$hit_coun t,$page,$ip,$dn sname,$referer\ r\n");
    fclose($fp);
    ?>

    Then create a file called hitcounter.txt and put value 0 in it.

    Then create a file called hitcounter.csv and put following text in it:
    TimeStamp,Count ,Page,IP-Adress,DNS,Refe rer

    Remember to give write permitions to the hitcounter files for your
    local internet account and you done.

    Now on every PHP web page you have add the following code between the
    <header></headeror <body></bodytags:
    <?php include ("counter.php") ; ?>

    If you link .csv file to excel when you click on the csv file you will
    be promted to open it with excel.
    If you do you get a nice grid layout of the data.
  • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

    #2
    Re: Creating a simple hitcounter for PHP web pages. (saves to files)

    tony19760619@gm ail.com wrote:
    $fp = fopen($count_my _page , "w");
    fputs($fp , "$hits[0]");
    fclose($fp);
    The problem wiht this is that the write operations are not atomic, and
    you're not using locks of any kind, so you *will* have concurrency
    problems.

    In other words: your data files will be corrupted on a high visitor load.

    I suggest you change all that to either use flock() or any kind of RDBMS
    (e.g. sqlite).

    --
    ----------------------------------
    Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

    Un ordenador no es un televisor ni un microondas, es una herramienta
    compleja.

    Comment

    • Michael Fesser

      #3
      Re: Creating a simple hitcounter for PHP web pages. (saves to files)

      ..oO(tony197606 19@gmail.com)
      >To make this work on a single page you will need to create/modify 4
      >files.
      >[...]
      Please don't multipost! One posting is more than enough. Crosspost if
      you think it's necessary.

      Micha

      Comment

      • tony19760619@gmail.com

        #4
        Re: Creating a simple hitcounter for PHP web pages. (saves to files)

        The idea was to make a simple counter using files...
        To implement your request complicates things a lot.
        But here is contents for the counter.php file for writing to csv file

        <?php

        //------------------------------
        //-- Values you can set
        //------------------------------

        //This is number of seconds to try and open and lock
        //the files needed for the counter to work
        $seconds = 10; //10 is one second, so 20 is two

        $counter_for_si te = ("hitcounter.tx t");
        $list_for_site = ("hitcounter.cs v");

        //------------------------------
        //-- Open and lock counter file
        //------------------------------
        $counter_file_o pen = False;
        $counter_file_l ock = False;
        $list_file_open = False;
        $list_file_lock = False;
        for($counter=0; $counter <= $seconds;$count er+=1) {
        //Open files
        if($counter_fil e_open == False){
        $fp_counter_fil e = fopen($counter_ for_site , "r+");
        if ($fp_counter_fi le != False){
        $counter_file_o pen = True;
        }
        }
        if($list_file_o pen == False){
        $fp_list_file = fopen($list_for _site , "a");
        if ($fp_counter_fi le != False){
        $counter_file_o pen = True;
        }
        }
        //Now lock the files
        if($counter_fil e_lock == False){
        $lock_state = flock($fp_list_ file,LOCK_EX);
        if ($lock_state == True){
        $counter_file_l ock = 1;
        }
        }
        if($list_file_l ock == False){
        $lock_state = flock($fp_list_ file,LOCK_EX);
        if ($lock_state == True){
        $list_file_lock = True;
        }
        }

        if (($counter_file _lock == True) and ($list_file_loc k == True)){
        break;
        }

        sleep(0.1);
        }

        if (($counter_file _lock == False) or ($list_file_loc k == False)){
        fclose($fp_coun ter_file);
        fclose($fp_list _file);
        exit("Could not lock counter files");
        }

        //-- Info
        //-- We need to suppress messages from file functions since PHP
        //-- is not sure we using a valid file pointer

        //------------------------------
        //-- Do counter stuff
        //------------------------------
        //$hits = file($counter_f or_site);
        //$hits[0] ++;
        //$hit_count = $hits[0];
        $hits = @fgets($fp_coun ter_file);
        $hits_i = (int)$hits;
        $hits_i++;
        $hits = (string)$hits_i ;
        @rewind($fp_cou nter_file);
        @fputs($fp_coun ter_file , "$hits");
        @fclose($fp_cou nter_file);

        //------------------------------
        //-- Do list details stuff
        //------------------------------
        $ip=$_SERVER[REMOTE_ADDR];
        $dnsname = gethostbyaddr($ ip);
        $referer=$_SERV ER[HTTP_REFERER];
        $page=$_SERVER[PHP_SELF];
        $now = date( "Y/m/d H:i:s", time() );
        @fputs($fp_list _file , "$now,$hits,$pa ge,$ip,$dnsname ,$referer\r\n") ;
        @fclose($fp_lis t_file);
        ?>

        Comment

        • Michael Fesser

          #5
          Re: Creating a simple hitcounter for PHP web pages. (saves to files)

          ..oO(tony197606 19@gmail.com)
          >//------------------------------
          >//-- Do list details stuff
          >//------------------------------
          >$ip=$_SERVER[REMOTE_ADDR];
          >$dnsname = gethostbyaddr($ ip);
          >$referer=$_SER VER[HTTP_REFERER];
          >$page=$_SERV ER[PHP_SELF];
          >$now = date( "Y/m/d H:i:s", time() );
          >@fputs($fp_lis t_file , "$now,$hits,$pa ge,$ip,$dnsname ,$referer\r\n") ;
          >@fclose($fp_li st_file);
          >?>
          This code will still throw at least three notices. You should fix your
          error_reporting , as already suggested in alt.php.

          Micha

          Comment

          • tony19760619@gmail.com

            #6
            Re: Creating a simple hitcounter for PHP web pages. (saves to files)

            This code will still throw at least three notices. You should fix your
            error_reporting , as already suggested in alt.php.
            >
            My PHP environment is set to ignore Notices.
            I tested the code, and it does work.
            So if you want to use it...you can.
            I will leave the fixing up of the notices..and the other stuff
            mentioned in "alt.php" to someone else.
            If you feel up to it, you can do it..then post it back here if you
            like :)

            Comment

            • Michael Fesser

              #7
              Re: Creating a simple hitcounter for PHP web pages. (saves to files)

              ..oO(tony197606 19@gmail.com)
              >This code will still throw at least three notices. You should fix your
              >error_reportin g, as already suggested in alt.php.
              >
              >My PHP environment is set to ignore Notices.
              Really bad idea. No serious developer does this.
              >I tested the code, and it does work.
              Notices are caused by bugs. Not severe bugs, but still bugs. And
              sometimes a notice might even indicate a really big problem. Without
              them you might have a hard time to find the reason for some problems.

              In short: Code that throws notices is bad code.
              >So if you want to use it...you can.
              I don't need hit counters.
              >I will leave the fixing up of the notices..and the other stuff
              >mentioned in "alt.php" to someone else.
              You're offering buggy and unreliable code, which is why I responded.
              >If you feel up to it, you can do it..then post it back here if you
              >like :)
              Why should I? No one needs hit counters except as a programming
              exercise. You should learn how to do things properly right from the
              beginning, which means to fix E_NOTICE errors and some other things.

              Micha

              Comment

              • tony19760619@gmail.com

                #8
                Re: Creating a simple hitcounter for PHP web pages. (saves to files)

                Micha
                You make more noise than booing taking off.

                I did not claim to be the worlds best programmer, not even a good one.
                The orignal post was about a simple hitcounter..(No t flawless code)
                I did a bit of the coding on it, but about half of it I found on the
                net.
                I did not bother to change the code from the net..(where 90% of the
                "problems" came from)
                Thought I would share it. That is all.

                I was even COURTEOUS enough to fix the "bug" of locking files.(from
                the code on the net)
                But you ignored that and started bitting on other parts of it.
                So I am now officially "crying wolf" on YOU!

                Please excuse me for ignoring your next post/s...


                Comment

                • Michael Fesser

                  #9
                  Re: Creating a simple hitcounter for PHP web pages. (saves to files)

                  ..oO(tony197606 19@gmail.com)
                  >You make more noise than booing taking off.
                  I'm just calling for good code, not a collection of bugs.
                  >I did not claim to be the worlds best programmer, not even a good one.
                  Ignoring notices is even less than amateurish.
                  >The orignal post was about a simple hitcounter..(No t flawless code)
                  It was buggy and you were given many hints how to fix it.
                  >I did a bit of the coding on it, but about half of it I found on the
                  >net.
                  >I did not bother to change the code from the net..(where 90% of the
                  >"problems" came from)
                  >Thought I would share it. That is all.
                  You didn't say it was "from the net". You posted it under your name, so
                  you're the one to blame and you're the one responsible for fixing bugs.
                  >I was even COURTEOUS enough to fix the "bug" of locking files.(from
                  >the code on the net)
                  Leaving many other bugs open. Good code is not measured just on parts of
                  it, but on the entire thing. And your code as a whole is still buggy,
                  regardless of where it came from.
                  >But you ignored that and started bitting on other parts of it.
                  >So I am now officially "crying wolf" on YOU!
                  Whatever you want.
                  >Please excuse me for ignoring your next post/s...
                  You're welcome.

                  BTW: Since you're a Google user - do you know how to use a killfile or
                  should I do a drawing?

                  Micha

                  Comment

                  • Jerry Stuckle

                    #10
                    Re: Creating a simple hitcounter for PHP web pages. (saves to files)

                    tony19760619@gm ail.com wrote:
                    Micha
                    You make more noise than booing taking off.
                    >
                    I did not claim to be the worlds best programmer, not even a good one.
                    The orignal post was about a simple hitcounter..(No t flawless code)
                    I did a bit of the coding on it, but about half of it I found on the
                    net.
                    I did not bother to change the code from the net..(where 90% of the
                    "problems" came from)
                    Thought I would share it. That is all.
                    >
                    I was even COURTEOUS enough to fix the "bug" of locking files.(from
                    the code on the net)
                    But you ignored that and started bitting on other parts of it.
                    So I am now officially "crying wolf" on YOU!
                    >
                    Please excuse me for ignoring your next post/s...
                    >
                    >
                    Micha is correct. It doesn't matter if you're the world's worst
                    programmer. Your code is buggy and needs to be fixed. Sharing such
                    buggy code reflects very poorly on you. Refusing to fix the problems
                    when told about them reflects even worse.

                    So go ahead and use your buggy code on your site. But don't release it
                    to the world - it's guys like you who give programmers a bad name.

                    --
                    =============== ===
                    Remove the "x" from my email address
                    Jerry Stuckle
                    JDS Computer Training Corp.
                    jstucklex@attgl obal.net
                    =============== ===

                    Comment

                    • JackM

                      #11
                      Re: Creating a simple hitcounter for PHP web pages. (saves to files)

                      Jerry Stuckle wrote:
                      tony19760619@gm ail.com wrote:
                      >Micha
                      >You make more noise than booing taking off.
                      >>
                      >I did not claim to be the worlds best programmer, not even a good one.
                      >The orignal post was about a simple hitcounter..(No t flawless code)
                      >I did a bit of the coding on it, but about half of it I found on the
                      >net.
                      >I did not bother to change the code from the net..(where 90% of the
                      >"problems" came from)
                      >Thought I would share it. That is all.
                      >>
                      >I was even COURTEOUS enough to fix the "bug" of locking files.(from
                      >the code on the net)
                      >But you ignored that and started bitting on other parts of it.
                      >So I am now officially "crying wolf" on YOU!
                      >>
                      >Please excuse me for ignoring your next post/s...
                      >>
                      >>
                      >
                      Micha is correct. It doesn't matter if you're the world's worst
                      programmer. Your code is buggy and needs to be fixed. Sharing such
                      buggy code reflects very poorly on you. Refusing to fix the problems
                      when told about them reflects even worse.
                      >
                      So go ahead and use your buggy code on your site. But don't release it
                      to the world - it's guys like you who give programmers a bad name.
                      Since he got the code from the net instead of writing it himself, the
                      chances are greatest that he doesn't know how to fix it properly. That
                      in itself is nothing to be ashamed of but is something that he really
                      should have owned up to instead of disparaging Micha.

                      Comment

                      • Jerry Stuckle

                        #12
                        Re: Creating a simple hitcounter for PHP web pages. (saves to files)

                        JackM wrote:
                        Jerry Stuckle wrote:
                        >tony19760619@gm ail.com wrote:
                        >>Micha
                        >>You make more noise than booing taking off.
                        >>>
                        >>I did not claim to be the worlds best programmer, not even a good one.
                        >>The orignal post was about a simple hitcounter..(No t flawless code)
                        >>I did a bit of the coding on it, but about half of it I found on the
                        >>net.
                        >>I did not bother to change the code from the net..(where 90% of the
                        >>"problems" came from)
                        >>Thought I would share it. That is all.
                        >>>
                        >>I was even COURTEOUS enough to fix the "bug" of locking files.(from
                        >>the code on the net)
                        >>But you ignored that and started bitting on other parts of it.
                        >>So I am now officially "crying wolf" on YOU!
                        >>>
                        >>Please excuse me for ignoring your next post/s...
                        >>>
                        >>>
                        >>
                        >Micha is correct. It doesn't matter if you're the world's worst
                        >programmer. Your code is buggy and needs to be fixed. Sharing such
                        >buggy code reflects very poorly on you. Refusing to fix the problems
                        >when told about them reflects even worse.
                        >>
                        >So go ahead and use your buggy code on your site. But don't release
                        >it to the world - it's guys like you who give programmers a bad name.
                        >
                        Since he got the code from the net instead of writing it himself, the
                        chances are greatest that he doesn't know how to fix it properly. That
                        in itself is nothing to be ashamed of but is something that he really
                        should have owned up to instead of disparaging Micha.
                        Very good point.

                        --
                        =============== ===
                        Remove the "x" from my email address
                        Jerry Stuckle
                        JDS Computer Training Corp.
                        jstucklex@attgl obal.net
                        =============== ===

                        Comment

                        Working...