Log Visitor Class


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.


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!


   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);
         $query =      MySQL_Query("SELECT * FROM visitors WHERE ip='{$this->ipAddress}' LIMIT 1");
           // the ip exists so lets update the info stored.
             $tempvalue = $row->visits + 1;
             $this->timesVisited = $row->visits;
             MySQL_Query("UPDATE visitors SET ua='{$this->userAgent}', visits='{$tempvalue}' WHERE ip='{$this->ipAddress}' LIMIT 1");
           // 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')");
         // 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);
        $query =      MySQL_Query("SELECT * FROM visitors ORDER BY visits DESC LIMIT 10");
          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";
        echo "</ul>n";
      } // 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";



To create a DAILY randomization of WHATEVER (i.e. MOTD)
First create a file (perhaps random.txt) pass4sure ccna 200-120

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


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


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