Show DevBest [PHP] Site view counter script

griimnak

You're a slave to the money then you die
Jul 20, 2013
955
794
Hello. Quick cool script, designed to count users based on number of ip's in database.
(tested on php 5.6)

Database table:
Code:
CREATE TABLE `views_log` (
  `ip` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `views_log` (`ip`) VALUES
('127.0.0.1');


PHP Code:
PHP:
class Database {
    public static function connect() {
        try {

            /**
            * Database settings for use of MySQLPDO
            * dbh - Hostname of database server
            * dbu - Username of database user
            * dbp - Password of database user
            * dbd - Desired database for use
            */
                $dbh = 'localhost';
                $dbu = 'root';
                $dbp = 'secret';
                $dbd = 'yourdb';
            /**
            * <-- END CONFIG -->
            */

                $conn = new pdo("mysql:host=$dbh;dbname=$dbd;", $dbu, $dbp);
                $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            return $conn;

        } catch(PDOException $e) {
            die('Failed to query: '.$e);

        }
    }

    public static function verifyView() {

       if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
          /**
          * Check if from shared internet
          */
            $view = $_SERVER['HTTP_CLIENT_IP'];

       } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
          /**
          * Check if from proxy
          */
            $view = $_SERVER['HTTP_X_FORWARDED_FOR'];

       } else {
          /**
          * Return unmasked ip if all else fails
          */
            $view = $_SERVER['REMOTE_ADDR'];
       }
       return $view;
    }

    public static function queryView() {
        $viewer = Database::verifyView();

        $query = Database::connect()->prepare("SELECT ip FROM views_log WHERE ip=:viewer");
        $query->bindParam(':viewer', $viewer);
        $query->execute();
        $try = $query->fetchAll();

        if (!$try) {
            /**
            * Viewer doesnt exist, let's tally them.
            */
            $insert = Database::connect()->prepare("INSERT INTO views_log (ip) VALUES (:viewer)");
            $insert->bindParam(':viewer', $viewer);
            $insert->execute();
         
        }
    }
}

Now you can go on any page you wish to count, and put this on it;
PHP:
Database::queryView();

To count the views, simply count the database rows:
PHP:
$count = Database::connect()->query('select count(*) from views_log')->fetchColumn();
echo $count;

Cheers, if you can make this script better post some code below and i may modify the OP :rasta:
 

Weasel

👄 I'd intercept me
Nov 25, 2011
4,132
2,456
The code is really clean, nice job. However the downside of this script, is that you don't have a correct amount of current visitors. Besides that you have spiders, possible Layer-7 attacks (this could flood your database really badly and cause problems), and so forth.
 

Users who are viewing this thread

Top