How to log visitors using PHP?

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

    How to log visitors using PHP?

    Can someone point me to a sample script that will log all visitors to my web
    site?

    I'm looking for a simple script that will capture IP address, Host Name,
    Browser, OS, and referring page - and write that info to MySql.

    I've created a MySql database - visitors - with the columns mentioned above, but
    am fumbling trying to get started with PHP... any help is appreciated!


  • deko

    #2
    Re: How to log visitors using PHP?

    okay, here's what I've got so far:

    <?php
    //get visitors ip address
    $ipaddress = $REMOTE_ADDR;
    //get visit date
    $vdate = date("m-d-y");
    //connect to database
    $conn = @mysql_connect( "localhost","us ername","passwo rd") or die("cannot connect
    to MySQL");
    //select the database
    @mysql_select_d b("ppvisits") or die("cannot connect to the database");
    //execute the query
    @mysql_query("I NSERT INTO guests(ip,date) VALUES ('$ipaddress',' $vdate')");
    //close the connection
    mysql_close($co nn);
    ?>

    What I still need is a way to get hostname, browser, OS, and referring page...


    "deko" <dje422@hotmail .com> wrote in message
    news:56T5c.3952 9$Dm6.12602@new ssvr25.news.pro digy.com...[color=blue]
    > Can someone point me to a sample script that will log all visitors to my web
    > site?
    >
    > I'm looking for a simple script that will capture IP address, Host Name,
    > Browser, OS, and referring page - and write that info to MySql.
    >
    > I've created a MySql database - visitors - with the columns mentioned above,[/color]
    but[color=blue]
    > am fumbling trying to get started with PHP... any help is appreciated!
    >
    >[/color]


    Comment

    • MyOdd

      #3
      Re: How to log visitors using PHP?

      [color=blue]
      >[/color]

      <snip code >
      [color=blue]
      >
      > What I still need is a way to get hostname, browser, OS, and referring[/color]
      page...[color=blue]
      >
      >[/color]

      Try and have a look at http://www.hotscripts.com/, quite a few scripts
      approach that subject.
      Also, have a look at getenv("HTTP_US ER_AGENT"); that contains some of the
      info you need.

      Your main problem will be the OS and Browser, but if you look at the php
      scripts they provide you will be well on your way.

      And why keep the IP BTW?, just curious really.

      Sims


      Comment

      • deko

        #4
        Re: How to log visitors using PHP?

        I've looked at hotscripts, but mostly an ocean of way too complicated scripts...
        hard to navigate. But I have found a few helpful things just by doing a web
        search.

        As for getenv("HTTP_US R_AGENT");

        I tried this, but it did not work...

        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
        <html>
        <head>
        <title>Get Stats</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        </head>
        <body>
        <?php
        print getenv("HTTP_US R_AGENT");
        ?>
        </body>
        </html>


        Comment

        • CountScubula

          #5
          Re: How to log visitors using PHP?

          "deko" <dje422@hotmail .com> wrote in message
          news:56T5c.3952 9$Dm6.12602@new ssvr25.news.pro digy.com...[color=blue]
          > Can someone point me to a sample script that will log all visitors to my[/color]
          web[color=blue]
          > site?
          >
          > I'm looking for a simple script that will capture IP address, Host Name,
          > Browser, OS, and referring page - and write that info to MySql.
          >
          > I've created a MySql database - visitors - with the columns mentioned[/color]
          above, but[color=blue]
          > am fumbling trying to get started with PHP... any help is appreciated!
          >
          >[/color]

          Forgive me, I may be missing something, but just parse the server logs. its
          all there.
          less load on server instead of making db connection/writting data/closing
          connection for every page

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


          Comment

          • deko

            #6
            Re: How to log visitors using PHP?

            > Forgive me, I may be missing something, but just parse the server logs. its[color=blue]
            > all there.
            > less load on server instead of making db connection/writting data/closing
            > connection for every page[/color]

            I agree, but I don't have access to the server logs - it's a shared hosting
            environemnt...


            Comment

            • deko

              #7
              Re: How to log visitors using PHP?

              > Forgive me, I may be missing something, but just parse the server logs. its[color=blue]
              > all there.
              > less load on server instead of making db connection/writting data/closing
              > connection for every page[/color]

              perhpas the thing to do would be to write it all to a flat file and then batch
              update the db once a day?


              Comment

              • CountScubula

                #8
                Re: How to log visitors using PHP?

                "deko" <dje422@hotmail .com> wrote in message
                news:U7U5c.3954 6$3T6.6442@news svr25.news.prod igy.com...[color=blue][color=green]
                > > Forgive me, I may be missing something, but just parse the server logs.[/color][/color]
                its[color=blue][color=green]
                > > all there.
                > > less load on server instead of making db connection/writting[/color][/color]
                data/closing[color=blue][color=green]
                > > connection for every page[/color]
                >
                > I agree, but I don't have access to the server logs - it's a shared[/color]
                hosting[color=blue]
                > environemnt...
                >
                >[/color]

                Gotcha,

                Sometimes on a shared server, you can still look at the logs, generaly they
                are readable by all system accounts, try something like this:

                test.php
                ----------------------
                <?php
                $info =`tail /var/log/httpd/access_log`;
                print $info;
                ?>


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


                Comment

                • CountScubula

                  #9
                  Re: How to log visitors using PHP?

                  If they do not offer up a log to you, then there is a chance they will not
                  offer up a cron job to schedule daily tasks.

                  I can think of may workarounds, but they are all more work than just parsing
                  the logs.

                  examples

                  * call a page that does the batching
                  * make call to another server
                  * write to a flat file, and if its over xxxx amount of bytes, spawn a
                  background process to batch

                  those are just a few.

                  --
                  Mike Bradley
                  http://www.gzentools.com -- free online php tools
                  "deko" <dje422@hotmail .com> wrote in message
                  news:meU5c.3954 7$gV6.19515@new ssvr25.news.pro digy.com...[color=blue][color=green]
                  > > Forgive me, I may be missing something, but just parse the server logs.[/color][/color]
                  its[color=blue][color=green]
                  > > all there.
                  > > less load on server instead of making db connection/writting[/color][/color]
                  data/closing[color=blue][color=green]
                  > > connection for every page[/color]
                  >
                  > perhpas the thing to do would be to write it all to a flat file and then[/color]
                  batch[color=blue]
                  > update the db once a day?
                  >
                  >[/color]


                  Comment

                  • Geoff Berrow

                    #10
                    Re: How to log visitors using PHP?

                    I noticed that Message-ID:
                    <56T5c.39529$Dm 6.12602@newssvr 25.news.prodigy .com> from deko contained
                    the following:
                    [color=blue]
                    >Can someone point me to a sample script that will log all visitors to my web
                    >site?
                    >
                    >I'm looking for a simple script that will capture IP address, Host Name,
                    >Browser, OS, and referring page - and write that info to MySql.
                    >
                    >I've created a MySql database - visitors - with the columns mentioned above, but
                    >am fumbling trying to get started with PHP... any help is appreciated![/color]

                    Check out $_SERVER In fact you are already using part of it. You
                    should be using $_SERVER['REMOTE_ADDR']


                    foreach ($_SERVER as $key => $value) {
                    $fullheaders .= $key . ": " . $value . "<br>\n";
                    }


                    $_SERVER will contain whatever headers are sent. Here is an example of
                    what you might get (I have removed identifying information)

                    ArrayKey: DOCUMENT_ROOT; Value: /usr/local/www/data
                    Key: HTTP_ACCEPT; Value: image/gif, image/x-xbitmap, image/jpeg,
                    image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel,
                    application/msword, */*
                    Key: HTTP_ACCEPT_ENC ODING; Value: gzip, deflate
                    Key: HTTP_ACCEPT_LAN GUAGE; Value: ie-ee,en-gb;q=0.5
                    Key: HTTP_CONNECTION ; Value: keep-alive
                    Key: HTTP_HOST; Value: <removed>
                    Key: HTTP_USER_AGENT ; Value: Mozilla/4.0 (compatible; MSIE 6.0;
                    Windows 98; generic_01_01)
                    Key: HTTP_VIA; Value: 1.1 webcacheB02 (NetCache NetApp/5.2.1R3)
                    Key: HTTP_X_FORWARDE D_FOR; Value: <removed>
                    Key: PATH; Value:
                    /sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/X11R6/bin
                    Key: REMOTE_ADDR; Value: 195.92.168.164
                    Key: REMOTE_PORT; Value: 8008
                    Key: SCRIPT_FILENAME ; Value: <removed>
                    Key: SCRIPT_URI; Value: <removed>
                    Key: SCRIPT_URL; Value: <removed>
                    Key: SERVER_ADDR; Value: <removed>
                    Key: SERVER_ADMIN; Value: <removed>
                    Key: SERVER_NAME; Value: <removed>
                    Key: SERVER_PORT; Value: 80
                    Key: SERVER_SIGNATUR E; Value: <ADDRESS>Apac he/1.3.26 Server at
                    <removed> Port 80</ADDRESS>

                    Key: SERVER_SOFTWARE ; Value: Apache/1.3.26 (Unix) mod_fastcgi/2.2.12
                    mod_perl/1.27 Resin/2.0.1 PHP/3.0.18 PHP/4.3.2-dev
                    Key: UNIQUE_ID; Value: QBJd4cFvyAUAAXq gd3k
                    Key: GATEWAY_INTERFA CE; Value: CGI/1.1
                    Key: SERVER_PROTOCOL ; Value: HTTP/1.1
                    Key: REQUEST_METHOD; Value: GET
                    Key: QUERY_STRING; Value: 2
                    Key: REQUEST_URI; Value: <removed>
                    Key: SCRIPT_NAME; Value: <removed>
                    Key: PATH_TRANSLATED ; Value: <removed>
                    Key: PHP_SELF; Value: <removed>
                    Key: argv; Value: Array
                    Key: argc; Value: 1
                    --
                    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

                    • Sandman

                      #11
                      Re: How to log visitors using PHP?

                      In article <k_T5c.39543$kN 6.31395@newssvr 25.news.prodigy .com>,
                      "deko" <dje422@hotmail .com> wrote:
                      [color=blue]
                      > I've looked at hotscripts, but mostly an ocean of way too complicated
                      > scripts...
                      > hard to navigate. But I have found a few helpful things just by doing a web
                      > search.
                      >
                      > As for getenv("HTTP_US R_AGENT");
                      >
                      > I tried this, but it did not work...
                      >
                      > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                      > "http://www.w3.org/TR/html4/loose.dtd">
                      > <html>
                      > <head>
                      > <title>Get Stats</title>
                      > <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
                      > </head>
                      > <body>
                      > <?php
                      > print getenv("HTTP_US R_AGENT");
                      > ?>
                      > </body>
                      > </html>[/color]


                      HTTP_USER_AGENT
                      ^

                      --
                      Sandman[.net]

                      Comment

                      • Sandman

                        #12
                        Re: How to log visitors using PHP?

                        In article <56T5c.39529$Dm 6.12602@newssvr 25.news.prodigy .com>, "deko"
                        <dje422@hotmail .com> wrote:
                        [color=blue]
                        > Can someone point me to a sample script that will log all visitors to
                        > my web site?
                        >
                        > I'm looking for a simple script that will capture IP address, Host
                        > Name, Browser, OS, and referring page - and write that info to MySql.
                        >
                        > I've created a MySql database - visitors - with the columns mentioned
                        > above, but am fumbling trying to get started with PHP... any help is
                        > appreciated![/color]

                        Do this:

                        <?
                        foreach ($_SERVER as $key => $value){
                        print "\$_SERVER[$key] = $value<br>\n";
                        }
                        ?>

                        You'll end up with a long list of variables and what their value is.

                        The variables you should keep an eye on are:

                        $_SERVER["REMOTE_ADD R"]
                        $_SERVER["HTTP_REFER ER"]
                        $_SERVER["HTTP_USER_AGEN T"]

                        --
                        Sandman[.net]

                        Comment

                        • deko

                          #13
                          Re: How to log visitors using PHP?

                          > * write to a flat file, and if its over xxxx amount of bytes, spawn a[color=blue]
                          > background process to batch[/color]

                          Updating the database everytime someone visists the site is kind of silly, so
                          I'm thinking writing to a file is better. Then I can display the file in a
                          table (the last 20 or so visitors) on my "stats" page and batch update the
                          database twice a day. This will keep the stats as current as they need to be.
                          I could also provide a link on the page that will generate a larger list of past
                          visitors.

                          I need a good learning project, so this might as well be it. If you could point
                          me in the right
                          direction on a few things that would be great - for example: How do I write the
                          data to a file so that it will be formatted for batch processing? It's easy to
                          write to a file, but how to format it so it can be imported into the
                          database? Should I have more than one file? Also can I use PHP (instead of
                          cron) to kick off the database update? How do I kick it off as a background
                          process?

                          Thanks!



                          Comment

                          • CountScubula

                            #14
                            Re: How to log visitors using PHP?

                            "deko" <dje422@hotmail .com> wrote in message
                            news:28%5c.1123 5$y64.8186@news svr27.news.prod igy.com...[color=blue][color=green]
                            > > * write to a flat file, and if its over xxxx amount of bytes, spawn a
                            > > background process to batch[/color]
                            >
                            > Updating the database everytime someone visists the site is kind of silly,[/color]
                            so[color=blue]
                            > I'm thinking writing to a file is better. Then I can display the file in[/color]
                            a[color=blue]
                            > table (the last 20 or so visitors) on my "stats" page and batch update the
                            > database twice a day. This will keep the stats as current as they need to[/color]
                            be.[color=blue]
                            > I could also provide a link on the page that will generate a larger list[/color]
                            of past[color=blue]
                            > visitors.
                            >
                            > I need a good learning project, so this might as well be it. If you could[/color]
                            point[color=blue]
                            > me in the right
                            > direction on a few things that would be great - for example: How do I[/color]
                            write the[color=blue]
                            > data to a file so that it will be formatted for batch processing? It's[/color]
                            easy to[color=blue]
                            > write to a file, but how to format it so it can be imported into the
                            > database? Should I have more than one file? Also can I use PHP (instead[/color]
                            of[color=blue]
                            > cron) to kick off the database update? How do I kick it off as a[/color]
                            background[color=blue]
                            > process?
                            >
                            > Thanks!
                            >
                            >
                            >[/color]

                            One of the things I like to do is have a directory that has write
                            permisions, so I done have to change permisions on a file by file basis. so
                            you could create a directory called _wfiles or something similar, and chmod
                            it 777

                            then to write data to you log you just need to do this:

                            $logData = "data to write to log";
                            $fp = fopen("_wfiles/mylog.txt","a") ;
                            fwrite($fp,"$lo gData\n");
                            fclose($fp);


                            now you also asked what format to save the file in, well thats up to you,
                            since it is your file, and you will be the onlyone reading/parsing it. I
                            would suggest the | pipe chr as a seperator for items. thus you can do
                            something like:

                            $logData = $_SERVER['REMOTE_ADDR'] . "|" .
                            $_SERVER['REQUEST_METHOD '] . "|" .
                            $_SERVER['REQUEST_URI'] . "|" .
                            $_SERVER['HTTP_REFERER'] . "|" .
                            $_SERVER['HTTP_USER_AGEN T'];

                            (look at the php manual under Predefined Variables for more info)

                            to spawn a background proccess, try something like:
                            exec("php -q myscript.php > /dev/null");

                            As far as scheduling the background proccess, you can't but we can do some
                            trickery and get to to run if someone clicks a page and it hasnt run yet.
                            sorta like this:

                            we write the time we update the log to a file like this:
                            $fp = fopen("_wfiles/logtime.txt","w ");
                            fwrite($fp,time ());
                            fclose($fp);

                            we check the time in the file before we update it, if it is older than a set
                            time, the spawn background
                            the whole thing might look like:


                            $logData = $_SERVER['REMOTE_ADDR'] . "|" .
                            $_SERVER['REQUEST_METHOD '] . "|" .
                            $_SERVER['REQUEST_URI'] . "|" .
                            $_SERVER['HTTP_REFERER'] . "|" .
                            $_SERVER['HTTP_USER_AGEN T'];

                            $fp = fopen("_wfiles/mylog.txt","a") ;
                            fwrite($fp,"$lo gData\n");
                            fclose($fp);

                            $logTime = @implode("",@fi le("_wfiles/logtime.txt"));
                            $day = 3600 * 24 // seconds in hour time 24 hours

                            if ( ($logTime + $day) < time() ) // is it over 24 hours old?
                            {
                            // yes, so do some stuff
                            $fp = fopen("_wfiles/logtime.txt","w ");
                            fwrite($fp,time ());
                            fclose($fp);

                            exec("php -q myscript.php > /dev/null");
                            }


                            Also, as a side note, if you are going to parse the file, rename it to a new
                            file name before you do so, this way the file will not be updated by another
                            script as you are trying to parse it.


                            Ok, sorry for the long post, I might have gotten carried away. Also, this
                            might not be the best way, it was off the top of my head.

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


                            Comment

                            • deko

                              #15
                              Re: How to log visitors using PHP?

                              wow! thanks for the snippets and advice. I'll go to work with this and post
                              back.

                              Also - I just bought a book - PHP and MySql Web Development (Welling, Thompson)
                              2nd Ed. - which should help...
                              [color=blue]
                              > One of the things I like to do is have a directory that has write
                              > permisions, so I done have to change permisions on a file by file basis. so
                              > you could create a directory called _wfiles or something similar, and chmod
                              > it 777
                              >
                              > then to write data to you log you just need to do this:
                              >
                              > $logData = "data to write to log";
                              > $fp = fopen("_wfiles/mylog.txt","a") ;
                              > fwrite($fp,"$lo gData\n");
                              > fclose($fp);
                              >
                              >
                              > now you also asked what format to save the file in, well thats up to you,
                              > since it is your file, and you will be the onlyone reading/parsing it. I
                              > would suggest the | pipe chr as a seperator for items. thus you can do
                              > something like:
                              >
                              > $logData = $_SERVER['REMOTE_ADDR'] . "|" .
                              > $_SERVER['REQUEST_METHOD '] . "|" .
                              > $_SERVER['REQUEST_URI'] . "|" .
                              > $_SERVER['HTTP_REFERER'] . "|" .
                              > $_SERVER['HTTP_USER_AGEN T'];
                              >
                              > (look at the php manual under Predefined Variables for more info)
                              >
                              > to spawn a background proccess, try something like:
                              > exec("php -q myscript.php > /dev/null");
                              >
                              > As far as scheduling the background proccess, you can't but we can do some
                              > trickery and get to to run if someone clicks a page and it hasnt run yet.
                              > sorta like this:
                              >
                              > we write the time we update the log to a file like this:
                              > $fp = fopen("_wfiles/logtime.txt","w ");
                              > fwrite($fp,time ());
                              > fclose($fp);
                              >
                              > we check the time in the file before we update it, if it is older than a set
                              > time, the spawn background
                              > the whole thing might look like:
                              >
                              >
                              > $logData = $_SERVER['REMOTE_ADDR'] . "|" .
                              > $_SERVER['REQUEST_METHOD '] . "|" .
                              > $_SERVER['REQUEST_URI'] . "|" .
                              > $_SERVER['HTTP_REFERER'] . "|" .
                              > $_SERVER['HTTP_USER_AGEN T'];
                              >
                              > $fp = fopen("_wfiles/mylog.txt","a") ;
                              > fwrite($fp,"$lo gData\n");
                              > fclose($fp);
                              >
                              > $logTime = @implode("",@fi le("_wfiles/logtime.txt"));
                              > $day = 3600 * 24 // seconds in hour time 24 hours
                              >
                              > if ( ($logTime + $day) < time() ) // is it over 24 hours old?
                              > {
                              > // yes, so do some stuff
                              > $fp = fopen("_wfiles/logtime.txt","w ");
                              > fwrite($fp,time ());
                              > fclose($fp);
                              >
                              > exec("php -q myscript.php > /dev/null");
                              > }
                              >
                              >
                              > Also, as a side note, if you are going to parse the file, rename it to a new
                              > file name before you do so, this way the file will not be updated by another
                              > script as you are trying to parse it.
                              >
                              >
                              > Ok, sorry for the long post, I might have gotten carried away. Also, this
                              > might not be the best way, it was off the top of my head.
                              >
                              > --
                              > Mike Bradley
                              > http://www.gzentools.com -- free online php tools
                              >
                              >[/color]


                              Comment

                              Working...