Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
[PHP] Site view counter script
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="griimnak" data-source="post: 371737" data-attributes="member: 35695"><p>Hello. Quick cool script, designed to count users based on number of ip's in database.</p><p><em><span style="font-size: 12px">(tested on php 5.6)</span></em></p><p></p><p>Database table:</p><p>[CODE]</p><p>CREATE TABLE `views_log` (</p><p> `ip` varchar(255) NOT NULL</p><p>) ENGINE=InnoDB DEFAULT CHARSET=latin1;</p><p></p><p>INSERT INTO `views_log` (`ip`) VALUES</p><p>('127.0.0.1');</p><p>[/CODE]</p><p></p><p></p><p>PHP Code:</p><p>[CODE=PHP]</p><p>class Database {</p><p> public static function connect() {</p><p> try {</p><p></p><p> /**</p><p> * Database settings for use of MySQLPDO</p><p> * dbh - Hostname of database server</p><p> * dbu - Username of database user</p><p> * dbp - Password of database user</p><p> * dbd - Desired database for use</p><p> */</p><p> $dbh = 'localhost';</p><p> $dbu = 'root';</p><p> $dbp = 'secret';</p><p> $dbd = 'yourdb';</p><p> /**</p><p> * <-- END CONFIG --></p><p> */</p><p></p><p> $conn = new pdo("mysql:host=$dbh;dbname=$dbd;", $dbu, $dbp);</p><p> $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);</p><p></p><p> return $conn;</p><p></p><p> } catch(PDOException $e) {</p><p> die('Failed to query: '.$e);</p><p></p><p> }</p><p> }</p><p></p><p> public static function verifyView() {</p><p></p><p> if (!empty($_SERVER['HTTP_CLIENT_IP'])) {</p><p> /**</p><p> * Check if from shared internet</p><p> */</p><p> $view = $_SERVER['HTTP_CLIENT_IP'];</p><p></p><p> } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {</p><p> /**</p><p> * Check if from proxy</p><p> */</p><p> $view = $_SERVER['HTTP_X_FORWARDED_FOR'];</p><p></p><p> } else {</p><p> /**</p><p> * Return unmasked ip if all else fails</p><p> */</p><p> $view = $_SERVER['REMOTE_ADDR'];</p><p> }</p><p> return $view;</p><p> }</p><p></p><p> public static function queryView() {</p><p> $viewer = Database::verifyView();</p><p></p><p> $query = Database::connect()->prepare("SELECT ip FROM views_log WHERE ip=:viewer");</p><p> $query->bindParam(':viewer', $viewer);</p><p> $query->execute();</p><p> $try = $query->fetchAll();</p><p></p><p> if (!$try) {</p><p> /**</p><p> * Viewer doesnt exist, let's tally them.</p><p> */</p><p> $insert = Database::connect()->prepare("INSERT INTO views_log (ip) VALUES (:viewer)");</p><p> $insert->bindParam(':viewer', $viewer);</p><p> $insert->execute();</p><p> </p><p> }</p><p> }</p><p>}</p><p>[/CODE]</p><p></p><p>Now you can go on any page you wish to count, and put this on it;</p><p>[CODE=PHP]</p><p>Database::queryView();</p><p>[/CODE]</p><p></p><p>To count the views, simply count the database rows:</p><p>[CODE=PHP]</p><p>$count = Database::connect()->query('select count(*) from views_log')->fetchColumn();</p><p>echo $count;</p><p>[/CODE]</p><p></p><p>Cheers, if you can make this script better post some code below and i may modify the OP <img src="/styles/default/xenforo/smilies/stasmoking.gif" class="smilie" loading="lazy" alt=":rasta:" title="Rasta :rasta:" data-shortname=":rasta:" /></p></blockquote><p></p>
[QUOTE="griimnak, post: 371737, member: 35695"] Hello. Quick cool script, designed to count users based on number of ip's in database. [I][SIZE=3](tested on php 5.6)[/SIZE][/I] 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'); [/CODE] PHP Code: [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(); } } } [/CODE] Now you can go on any page you wish to count, and put this on it; [CODE=PHP] Database::queryView(); [/CODE] To count the views, simply count the database rows: [CODE=PHP] $count = Database::connect()->query('select count(*) from views_log')->fetchColumn(); echo $count; [/CODE] Cheers, if you can make this script better post some code below and i may modify the OP :rasta: [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
[PHP] Site view counter script
Top