How to log visitors using PHP?

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

    #16
    Re: How to log visitors using PHP?

    Uzytkownik "CountScubu la" <me@scantek.hot mail.com> napisal w wiadomosci
    news:CLU5c.1117 4$7K2.5116@news svr27.news.prod igy.com...[color=blue]
    > "deko" <dje422@hotmail .com> wrote in message
    > news:U7U5c.3954 6$3T6.6442@news svr25.news.prod igy.com...
    > Sometimes on a shared server, you can still look at the logs, generaly[/color]
    they[color=blue]
    > are readable by all system accounts, try something like this:
    >
    > test.php
    > ----------------------
    > <?php
    > $info =`tail /var/log/httpd/access_log`;
    > print $info;
    > ?>[/color]

    On our server the Apache logs have more entries for Red Alert and various
    other IIS worms than real user requests. I have completely given up on them
    :-(


    Comment

    • CountScubula

      #17
      Re: How to log visitors using PHP?

      "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message
      news:1vednXcOkM jvZsXd4p2dnA@co mcast.com...
      [color=blue]
      > On our server the Apache logs have more entries for Red Alert and various
      > other IIS worms than real user requests. I have completely given up on[/color]
      them[color=blue]
      > :-(
      >
      >[/color]

      Hmm, Chung, you strike me as someone who has thier own T1 and a few servers
      lying around :)


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


      Comment

      • deko

        #18
        Re: How to log visitors using PHP?

        Here's the latest - the stats I am still working on are hostname, referring
        page, and ISP

        suggestions welcome!

        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
        <html>
        <head>
        <title>stats</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        </head>
        <body style="padding: 5%">
        <?php
        /*
        This code to go in every page on my site

        stats to get:

        * IP address - $visip
        hostname - $vishost
        * browser - $visbrow
        - operating - system $visos
        ? referring page - $visref
        ? ISP - $visisp
        * date/time of visit - $visdate
        * number of page views

        files updated:

        visdata - visitor information
        viscount - number of visits

        */
        // identify IP
        $visip = $_SERVER['REMOTE_ADDR'];
        ?>
        <br>
        $visip =
        <?php print $visip;
        // identify browser
        if((ereg("Nav", getenv("HTTP_US ER_AGENT"))) || (ereg("Gold",
        getenv("HTTP_US ER_AGENT"))) || (ereg("X11", getenv("HTTP_US ER_AGENT"))) ||
        (ereg("Mozilla" , getenv("HTTP_US ER_AGENT"))) || (ereg("Netscape ",
        getenv("HTTP_US ER_AGENT"))) AND (!ereg("MSIE", getenv("HTTP_US ER_AGENT"))) AND
        (!ereg("Konquer or", getenv("HTTP_US ER_AGENT"))))
        $visbrow = "Netscape";
        elseif(ereg("Op era", getenv("HTTP_US ER_AGENT")))
        $visbrow = "Opera";
        elseif(ereg("MS IE", getenv("HTTP_US ER_AGENT")))
        $visbrow = "MSIE";
        elseif(ereg("Ly nx", getenv("HTTP_US ER_AGENT")))
        $visbrow = "Lynx";
        elseif(ereg("We bTV", getenv("HTTP_US ER_AGENT")))
        $visbrow = "WebTV";
        elseif(ereg("Ko nqueror", getenv("HTTP_US ER_AGENT")))
        $visbrow = "Konqueror" ;
        elseif((eregi(" bot", getenv("HTTP_US ER_AGENT"))) || (ereg("Google",
        getenv("HTTP_US ER_AGENT"))) || (ereg("Slurp", getenv("HTTP_US ER_AGENT"))) ||
        (ereg("Scooter" , getenv("HTTP_US ER_AGENT"))) || (eregi("Spider" ,
        getenv("HTTP_US ER_AGENT"))) || (eregi("Infosee k", getenv("HTTP_US ER_AGENT"))))
        $visbrow = "Bot";
        else $visbrow = "Other";
        ?>
        <br>
        $visbrow =
        <?php
        print $visbrow;
        // identify OS
        if(ereg("Win", getenv("HTTP_US ER_AGENT")))
        $visos = "Windows"; // needs to be expanded to get version - e.g.
        NT,XP,2000, etc.
        elseif((ereg("M ac", getenv("HTTP_US ER_AGENT"))) || (ereg("PPC",
        getenv("HTTP_US ER_AGENT"))))
        $visos = "Mac";
        elseif(ereg("Li nux", getenv("HTTP_US ER_AGENT")))
        $visos = "Linux";
        elseif(ereg("Fr eeBSD", getenv("HTTP_US ER_AGENT")))
        $visos = "FreeBSD";
        elseif(ereg("Su nOS", getenv("HTTP_US ER_AGENT")))
        $visos = "SunOS";
        elseif(ereg("IR IX", getenv("HTTP_US ER_AGENT")))
        $visos = "IRIX";
        elseif(ereg("Be OS", getenv("HTTP_US ER_AGENT")))
        $visos = "BeOS";
        elseif(ereg("OS/2", getenv("HTTP_US ER_AGENT")))
        $visos = "OS/2";
        elseif(ereg("AI X", getenv("HTTP_US ER_AGENT")))
        $visos = "AIX";
        else $visos = "Other";
        ?>
        <br>
        $visos =
        <?php
        print $visos;
        // visit date and time
        $hourdiff = "0"; // hours difference between server time and local time.
        $timeadjust = ($hourdiff * 60 * 60);
        $visdate = date("l, F d Y h:i a",time() + $timeadjust);
        ?>
        <br>
        $visdate =
        <?php
        print $visdate;
        // identlfy ISP
        /*
        if ($visip=='') $visip = $_SERVER['REMOTE_ADDR'];
        $longisp = @gethostbyaddr( $ip);
        $visisp = explode('.', $longisp);
        $visisp = array_reverse($ isp);
        $tmp = $isp[1];
        if (preg_match("/\<(org?|com?|ne t)\>/i", $tmp)) {
        $visisp = $isp[2].'.'.$isp[1].'.'.$isp[0];
        } else {
        $visisp = $isp[1].'.'.$isp[0];
        }
        preg_match("/[0-9]{1,3}\.[0-9]{1,3}/", $myisp) ? return 'unknown' : return
        $visisp;
        ?>
        <br>
        $visisp =
        <?php
        print $visisp;

        // identify referring page
        //function storereferer () {
        // global $HTTP_REFERER, $baseurl;
        // /*no referer?*/
        // if (!isset ($HTTP_REFERER) )
        // return;
        // /*empty referer?*/
        // if ($HTTP_REFERER == "")
        // return;
        // /*referer is from this site?*/
        // if (strstr ($HTTP_REFERER, $baseurl))
        // return;
        // /*Encode quotes and HTML characters.*/
        // $referer = htmlspecialchar s ($HTTP_REFERER, ENT_QUOTES);




        // page views
        // viscount will store number of visits - visdata stores visitor info
        $file = '/home/post/public_html/viscount';
        if ($counter = @file ($file)) {
        $line = each($counter);
        if (!$counter) {
        $counter = 1;
        } else {
        $counter = $line[value]+1;
        }
        }
        $fp = fopen ($file, "w");
        fwrite ($fp, "$counter") ;
        fclose($fp);
        ?>

        </body>
        </html>


        Comment

        • CountScubula

          #19
          Re: How to log visitors using PHP?

          "deko" <dje422@hotmail .com> wrote in message
          news:MUa6c.1153 0$cS1.4516@news svr27.news.prod igy.com...[color=blue]
          > Here's the latest - the stats I am still working on are hostname,[/color]
          referring[color=blue]
          > page, and ISP
          > suggestions welcome!
          > This code to go in every page on my site
          > ...snip...
          >[/color]

          You have some good ideas, but the code is really hard to follow, try to
          structure it a bit more.
          also, just put it in one file, and do an include() on it from your other
          pages.

          Also, if I may, don't log the host name, this lookup can add considerable
          delay, proccess that when you spawn a bg proccess.

          If I may also, don't keep diving into functions getenv, use a simple $var =
          .... and reference the $var instead, this keeps it easy to modify your code.

          just my 2 cents
          --
          Mike Bradley
          http://www.gzentools.com -- free online php tools


          Comment

          • deko

            #20
            Re: How to log visitors using PHP?

            no doubt, the code is a mess - this is just a beta hack - will create functions
            and clean up when I can first get everything to print to screen...

            You say this will result in a delay:

            <?php
            function gethost($visip) {
            $host = `host $visip`;
            return (($host ? end ( explode (' ', $host)) : $visip));
            }
            print gethost($visip) ;
            ?>

            Do you suggest doing this for all visitor IPs at the end of the day (2:00am) in
            a batch process?
            [color=blue]
            > If I may also, don't keep diving into functions getenv, use a simple $var =
            > ... and reference the $var instead, this keeps it easy to modify your code.[/color]

            You mean like this:

            $var = getenv("HTTP_US ER_AGENT")

            <?php print $visip;
            // identify browser
            if((ereg("Nav", $var)) || (ereg("Gold", $var)) || (ereg("X11", $var)) ||
            (ereg("Mozilla" , $var))

            and so on throughout the script?
            ....
            $visbrow = "Netscape";
            elseif(ereg("Op era", getenv("HTTP_US ER_AGENT")))
            $visbrow = "Opera";
            elseif(ereg("MS IE", getenv("HTTP_US ER_AGENT")))
            $visbrow = "MSIE";
            elseif(ereg("Ly nx", getenv("HTTP_US ER_AGENT")))
            $visbrow = "Lynx";
            elseif(ereg("We bTV", getenv("HTTP_US ER_AGENT")))
            $visbrow = "WebTV";
            elseif(ereg("Ko nqueror", getenv("HTTP_US ER_AGENT")))
            $visbrow = "Konqueror" ;
            ...
            ?>


            Comment

            • deko

              #21
              Re: How to log visitors using PHP?

              This prints all the data I'm after to the screen. One improvement I'd like to
              make is to identify the Windows OS version - so it will report "Windows XP" or
              "Windows 2000", or whatever, instead of just "Windows".

              Note - assigning getenv("HTTP_US ER_AGENT") to $var did not work.

              The next step is to get this to write everything nicely to a file that can be
              easily batch uploaded into MySql - and prior to doing so, run a batch process to
              get the host name of all the logged IPs. Once issue with that idea, however, is
              that most folks are using DHCP so the IP may be stale when the process is run -
              but I suppose it will return the same info regardless - unless the IP is now
              unassigned...


              <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
              "http://www.w3.org/TR/html4/loose.dtd">
              <html>
              <head>
              <title>stats</title>
              <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
              </head>
              <body style="padding: 5%">
              <?php

              /*
              This code to go in every page on site

              stats to get:

              * IP address - $visip
              * hostname - $vishost
              * browser - $visbrow
              * operating - system $visos
              * referring page - $visref
              * date/time of visit - $visdate
              * number of page views

              files updated:

              * visdata - visitor information
              * viscount - number of visits

              */

              // identify IP
              $visip = $_SERVER['REMOTE_ADDR'];
              ?>
              <br>
              $visip =
              <?php print $visip;
              // identify browser and OS
              // this did not work:
              //$var = getenv("HTTP_US ER_AGENT")
              if((ereg("Nav", getenv("HTTP_US ER_AGENT"))) || (ereg("Gold",
              getenv("HTTP_US ER_AGENT"))) || (ereg("X11", getenv("HTTP_US ER_AGENT"))) ||
              (ereg("Mozilla" , getenv("HTTP_US ER_AGENT"))) || (ereg("Netscape ",
              getenv("HTTP_US ER_AGENT"))) AND (!ereg("MSIE", getenv("HTTP_US ER_AGENT"))) AND
              (!ereg("Konquer or", getenv("HTTP_US ER_AGENT"))))
              $visbrow = "Netscape";
              elseif(ereg("Op era", getenv("HTTP_US ER_AGENT")))
              $visbrow = "Opera";
              elseif(ereg("MS IE", getenv("HTTP_US ER_AGENT")))
              $visbrow = "MSIE";
              elseif(ereg("Ly nx", getenv("HTTP_US ER_AGENT")))
              $visbrow = "Lynx";
              elseif(ereg("We bTV", getenv("HTTP_US ER_AGENT")))
              $visbrow = "WebTV";
              elseif(ereg("Ko nqueror", getenv("HTTP_US ER_AGENT")))
              $visbrow = "Konqueror" ;
              elseif((eregi(" bot", getenv("HTTP_US ER_AGENT"))) || (ereg("Google",
              getenv("HTTP_US ER_AGENT"))) || (ereg("Slurp", getenv("HTTP_US ER_AGENT"))) ||
              (ereg("Scooter" , getenv("HTTP_US ER_AGENT"))) || (eregi("Spider" ,
              getenv("HTTP_US ER_AGENT"))) || (eregi("Infosee k", getenv("HTTP_US ER_AGENT"))))
              $visbrow = "Bot";
              else $visbrow = "unknown";
              ?>
              <br>
              $visbrow =
              <?php
              print $visbrow;
              // identify OS
              if(ereg("Win", getenv("HTTP_US ER_AGENT")))
              $visos = "Windows"; // needs to be expanded to get version - e.g.
              NT,XP,2000, etc.
              elseif((ereg("M ac", getenv("HTTP_US ER_AGENT"))) || (ereg("PPC",
              getenv("HTTP_US ER_AGENT"))))
              $visos = "Mac";
              elseif(ereg("Li nux", getenv("HTTP_US ER_AGENT")))
              $visos = "Linux";
              elseif(ereg("Fr eeBSD", getenv("HTTP_US ER_AGENT")))
              $visos = "FreeBSD";
              elseif(ereg("Su nOS", getenv("HTTP_US ER_AGENT")))
              $visos = "SunOS";
              elseif(ereg("IR IX", getenv("HTTP_US ER_AGENT")))
              $visos = "IRIX";
              elseif(ereg("Be OS", getenv("HTTP_US ER_AGENT")))
              $visos = "BeOS";
              elseif(ereg("OS/2", getenv("HTTP_US ER_AGENT")))
              $visos = "OS/2";
              elseif(ereg("AI X", getenv("HTTP_US ER_AGENT")))
              $visos = "AIX";
              else $visos = "unknown";
              ?>
              <br>
              $visos =
              <?php
              print $visos;
              // visit date and time
              $hourdiff = "1"; // hours difference between server time and local time.
              $timeadjust = ($hourdiff * 60 * 60);
              $visdate = date("l, F d Y h:i a",time() + $timeadjust);
              ?>
              <br>
              $visdate =
              <?php
              print $visdate;
              ?>
              <br>
              gethost($visip) =
              <?php
              //identify hostname - run this as a batch process once a day for all visitor IPs
              //otherwise will result in delay in loading page while info is sought over the
              net
              //note backticks can cause error when php is run in safe mode
              function gethost($visip) {
              $host = `host $visip`;
              return (($host ? end ( explode (' ', $host)) : $visip));
              }
              print gethost($visip) ;
              //identify referring page
              $referer = getenv("HTTP_RE FERER")
              ?>
              <br>
              $referer =
              <?php
              print $referer;
              ?>
              </body>
              </html>


              Comment

              • CountScubula

                #22
                Re: How to log visitors using PHP?

                http://www.gzentools.com -- free online php tools
                "deko" <dje422@hotmail .com> wrote in message
                news:L0c6c.1156 1$jC2.1002@news svr27.news.prod igy.com...[color=blue]
                > no doubt, the code is a mess - this is just a beta hack - will create[/color]
                functions[color=blue]
                > and clean up when I can first get everything to print to screen...
                >
                > You say this will result in a delay:
                >
                > <?php
                > function gethost($visip) {
                > $host = `host $visip`;
                > return (($host ? end ( explode (' ', $host)) : $visip));
                > }
                > print gethost($visip) ;
                > ?>
                >
                > Do you suggest doing this for all visitor IPs at the end of the day[/color]
                (2:00am) in[color=blue]
                > a batch process?
                >[color=green]
                > > If I may also, don't keep diving into functions getenv, use a simple[/color][/color]
                $var =[color=blue][color=green]
                > > ... and reference the $var instead, this keeps it easy to modify your[/color][/color]
                code.[color=blue]
                >
                > You mean like this:
                >
                > $var = getenv("HTTP_US ER_AGENT")
                >
                > <?php print $visip;
                > // identify browser
                > if((ereg("Nav", $var)) || (ereg("Gold", $var)) || (ereg("X11",[/color]
                $var)) ||[color=blue]
                > (ereg("Mozilla" , $var))
                >
                > and so on throughout the script?
                > ...
                > $visbrow = "Netscape";
                > elseif(ereg("Op era", getenv("HTTP_US ER_AGENT")))
                > $visbrow = "Opera";
                > elseif(ereg("MS IE", getenv("HTTP_US ER_AGENT")))
                > $visbrow = "MSIE";
                > elseif(ereg("Ly nx", getenv("HTTP_US ER_AGENT")))
                > $visbrow = "Lynx";
                > elseif(ereg("We bTV", getenv("HTTP_US ER_AGENT")))
                > $visbrow = "WebTV";
                > elseif(ereg("Ko nqueror", getenv("HTTP_US ER_AGENT")))
                > $visbrow = "Konqueror" ;
                > ...
                > ?>
                >
                >[/color]
                yes do it in a batch instead of live

                $host = gethostbyaddr($ visp); // built in php function

                --
                Mike Bradley



                Comment

                • deko

                  #23
                  Re: How to log visitors using PHP?

                  > also, just put it in one file, and do an include() on it from your other[color=blue]
                  > pages.[/color]

                  Do you mean that I don't need to embed the code in an html page? So, I can have
                  a file in public_html named "visistats. php? and then just call a function from
                  that file into index.php, or whatever.php? What does teh syntax look like?
                  What if someone trys to access http://www.domain.com/visistats.php?


                  Comment

                  • Chung Leong

                    #24
                    Re: How to log visitors using PHP?

                    Worse, I work at a large university. The server is connected via a gigabit
                    link to a network with several thousands poorly protected Windows boxes.

                    Uzytkownik "CountScubu la" <me@scantek.hot mail.com> napisal w wiadomosci
                    news:8n76c.2553 9$xp.14172@news svr29.news.prod igy.com...[color=blue]
                    > "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message
                    > news:1vednXcOkM jvZsXd4p2dnA@co mcast.com...
                    >[color=green]
                    > > On our server the Apache logs have more entries for Red Alert and[/color][/color]
                    various[color=blue][color=green]
                    > > other IIS worms than real user requests. I have completely given up on[/color]
                    > them[color=green]
                    > > :-(
                    > >
                    > >[/color]
                    >
                    > Hmm, Chung, you strike me as someone who has thier own T1 and a few[/color]
                    servers[color=blue]
                    > lying around :)
                    >
                    >
                    > --
                    > Mike Bradley
                    > http://www.gzentools.com -- free online php tools
                    >
                    >[/color]


                    Comment

                    Working...