# Log Visitor Class

**URL:** <https://www.phphelp.com/t/log-visitor-class/9510>\
**Category:** Code Snippets\
**Created:** [December 25, 2005, 9:05pm UTC](https://www.phphelp.com/t/log-visitor-class/9510 "2005-12-25T21:05:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![commandergc](https://www.phphelp.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@commandergc](https://www.phphelp.com/u/commandergc)\
**Post date:** [December 25, 2005, 9:05pm UTC](https://www.phphelp.com/t/log-visitor-class/9510/1 "2005-12-25T21:05:30Z")

</div>

Well here you go folks my first attempt at using Classes. This is a method of loging unique ips that visit your site. Enjoy

The code works with php 5 and MySQL 5. this has not been tested with php 4 but i think it should work with the latest version of it.

Database:

`CREATE TABLE `visitors` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `ip` varchar(255) NOT NULL,
  `ua` varchar(255) NOT NULL,
  `visits` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `ip` (`ip`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
`  
PHP: This has been edited to remove a small security hole!

```auto
<?php

   class LogVisitor
   {
    
      // set up the public vars
      public $ipAddress;
      public $userAgent;
      public $timesVisited;

      // Database Vars
      protected $dbserv = 'localhost';
      protected $dbuser = 'root';
      protected $dbpass = NULL;
      protected $dbbase = 'log';

      function LogVisitor()
      {

         // Get the visitor's IP Address and User Agent
         $this->ipAddress = $_SERVER['REMOTE_ADDR'];
         $this->userAgent = $_SERVER['HTTP_USER_AGENT'];

         // check to see if this ip has already been logged and if it has add to it's total visits.
         // If no ip is found it will be added along with the UserAgent.
         $connection = MySQL_Connect($this->dbserv,$this->dbuser,$this->dbpass);
                       MySQL_Select_DB($this->dbbase);
         $query = MySQL_Query("SELECT * FROM visitors WHERE ip='{$this->ipAddress}' LIMIT 1");
         if(MySQL_Num_Rows($query)==1)
         {
           // the ip exists so lets update the info stored.
           while($row=MySQL_Fetch_Object($query))
           {
             $tempvalue = $row->visits + 1;
             $this->timesVisited = $row->visits;
             MySQL_Query("UPDATE visitors SET ua='{$this->userAgent}', visits='{$tempvalue}' WHERE ip='{$this->ipAddress}' LIMIT 1");
           }
         }ELSE{
           // Now the ip isn't thire so it must be new lets add it.
           $this->timesVisited = 1;
           MySQL_Query("INSERT INTO visitors VALUES(NULL,'{$this->ipAddress}','{$this->userAgent}','1')");
         }
         MySQL_Close($connection);
         // End of LogVisitor Function
      }

      // TopTen() is not very usefull but i thought it would be good to list the ips that have visited the most.
      function TopTen()
      {
        echo "<ul>n";
        echo "<b>The top 10 Visitors</b><br />n";
        $connection = MySQL_Connect($this->dbserv,$this->dbuser,$this->dbpass);
                      MySQL_Select_DB($this->dbbase);
        $query = MySQL_Query("SELECT * FROM visitors ORDER BY visits DESC LIMIT 10");
        $i=1;
        while($row=MySQL_Fetch_Object($query))
        {
          echo "<li><b>{$i}</b><br /><b>IP:</b> {$row->ip}<br /><b>User's Browser:</b> {$row->ua}<br /><b>Number of Visits:</b> {$row->visits}</li>n";
          $i++;
        }
        echo "</ul>n";
        MySQL_Close($connection);
      } // End of TopTen() Function

   }// End of LogVisitors Class
   
   $log = New LogVisitor;
   echo "Your ip is <b>".$log->ipAddress ."</b> you have visited <b>{$log->timesVisited}</b> time(s)<br />n";
   $log->TopTen();

?>
```

---

<div class="post-metadata">

**Author:** ![miloson24](https://www.phphelp.com/letter_avatar_proxy/v4/letter/m/a5b964/32.png) [@miloson24](https://www.phphelp.com/u/miloson24)\
**Post date:** [April 18, 2015, 12:10pm UTC](https://www.phphelp.com/t/log-visitor-class/9510/2 "2015-04-18T12:10:57Z")

</div>

To create a DAILY randomization of WHATEVER (i.e. MOTD)  
First create a file (perhaps random.txt) [pass4sure ccna 200-120](http://www.pass4sure.co.uk/200-120.html)

Keep 2 lines in it… The first is the the daily random number , the second is the unix timestamp (of the time last updated.)

---

<div class="post-metadata">

**Author:** ![Strider64](https://www.phphelp.com/user_avatar/www.phphelp.com/strider64/32/3161_2.png) [@Strider64](https://www.phphelp.com/u/Strider64)\
**Post date:** [April 25, 2015, 3:17pm UTC](https://www.phphelp.com/t/log-visitor-class/9510/3 "2015-04-25T15:17:18Z")

</div>

what’s up with the special characters and to a lesser extant the obsolete mysql?

---

<div class="post-metadata">

**Author:** ![astonecipher](https://www.phphelp.com/user_avatar/www.phphelp.com/astonecipher/32/2797_2.png) [@astonecipher](https://www.phphelp.com/u/astonecipher)\
**Post date:** [April 27, 2015, 11:32pm UTC](https://www.phphelp.com/t/log-visitor-class/9510/4 "2015-04-27T23:32:58Z")

</div>

Note the year of the original post… That was industry standard at the time.
