Hits and Online Today Php Utility Script Example Tutorial
Display on your webpages total page views, unique visitors, mobile users and bots all in one!
Hi!. I have just added this websites 2 go total visitor analyzing flat file PhP Script. I use it on this site. I thought I would share it with you. For those who like to use flatfile based file manipulation like myself you will like this.
Ever since I had a site down for "To many database connections" I went total flatfile with all scripts. Since none of my websites will be as big as some as the mega sites out like Amazon etc. flatfile and directory read CMS work just fine works if done right. If your site gets that big you could afford a server, cloud and use sql but for the majority of us starting out with shared hosts or free hosts flatfile is the way to go. Why because it is portable and fast. You don’t have to set up a database for everthing. if I need to use SQL I just convert on a case by case basis.
Now I only get excess resource errors LOL!
Check out this script it works great and you can customize the time setting and add bots and crawlers or remove if you like. Set time for users online for minutes or hours. Scan bot useragents and display. You can even modify the crawler function to compare host to useragent to block those hackers that user a fraudulent google/bot useragent.
Hope you like it and use it and find it useful!
1: <?php
2: //Total visitors script
3: //copyright 2022 by george @ CMXads.com
4: // new updated more efficient as of Dec 1 2021
5: // Displays total page views in last 24 hours
6: //unique visitor count in last 24 hours
7: //list of bots and spiders in last 24 hours
8: //count of mobile users in last 24 hours
9:
10: date_default_timezone_set('America/New_York');
11: $user_time =time();
12:
13: if(!isset($ip)){
14: $ip = $_SERVER['REMOTE_ADDR'];
15: }
16: $user_ip = $ip;
17:
18: $bot ='';
19: $user_type ='';
20: if(isset($_SERVER['HTTP_USER_AGENT'])){// if we detect a UA
21:
22: //get UA string to lower case
23: $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
24:
25: //look for mobile users with our array
26: $mobile_array =array('android',
27: 'mobile',
28: 'ipad',
29: 'iphone',);
30: foreach($mobile_array as $mobile_user){// detect a mobile useragent
31: if(strpos( $ua, $mobile_user) !==false){
32: $user_type = "mobileuser";// set type to mobile user
33: }
34: }
35: // testing UA string. comment out for production
36: //$ua = strtolower('Mozilla/5.0 (compatible; dasSpider/0.10; +https://www.mojeek.com/bot.html)');
37:
38: // look for bot in UA string. can use strpos but I like to mix it up
39: if(strpos( $ua, 'bot/') !==false || strpos( $ua, 'spider/') !==false) {
40:
41: // if found explode on space
42: $uaParts = explode(' ', trim($ua));
43:
44: // search UA parts array for bot string
45: foreach ($uaParts as $bots) {
46:
47: // found explode / and get bot name
48: if( strpos( $bots, 'bot/') !==false || strpos( $bots, 'spider/') !==false){
49: $getBot = explode('/', trim($bots));
50: }
51: }// end loop
52: $bot = $getBot[0];// no need to be in loop
53: }
54: }
55:
56: if($bot){// if bot type is bot to file
57: $user_type = $bot;
58: }elseif(!$user_type){// if type not set from mobile check
59: $user_type = "user";// set as user
60: }
61: $bot = '';
62: $online = '';
63: $i = 0;
64: $file_name = 'onlineusers.dat';
65:
66: $new_line = $user_ip . "|" . $user_time . "|" . $user_type . "\n";
67: file_put_contents($file_name, $new_line, FILE_APPEND | LOCK_EX);
68:
69: $files = file($file_name);
70:
71: $new_file = array();
72: $all = array();
73: $bots =array();
74: $mobile = array();
75:
76: foreach ($files as $line) {
77: $users = explode("|", $line);
78: $users = array_map('trim', $users);
79: if($users[1] >= time() - 86400)// check timestamp past time less than 24 hrs
80: {
81:
82: $new_file[] = $line; // push line new array
83: }
84: if(strpos( $users[2], 'user') ===false )// get array of bots
85: {
86: $bots[] = $users[2];
87: }
88:
89: if(strpos( $users[2], 'user') !==false )// get array of all users
90: {
91: $all[] = $users[0];
92: //$non_bot++;
93: }
94: if(strpos( $users[2], 'mobileuser') !==false )// get array of mobile users only
95: {
96: $mobile[] = $users[0];
97: //$non_bot++;
98: }
99:
100:
101: //compare time and delete lines
102: //minutes 86400 24hrs 3600 1 hour expiration = 3 hours
103: $i++;
104: }
105: //write filtered lines back to file
106: file_put_contents($file_name, $new_file, LOCK_EX);
107:
108: //count page hits
109: $hits = $i;
110:
111: //check for bots
112: $bot_cnt = count((array) $bots);// count bots array
113: if($bot_cnt > 0){// if more than 0
114: $bot = array_unique($bots);// array unique it
115: $bot = implode(" ", $bot); // implode to string variable
116: }
117: if(empty($bot)){// if empty didsplay none
118: $bot = " none";
119: }
120: //now many unique users online
121: //var_dump($all);
122: //array_shift($all);
123:
124: $mobile_cnt = count((array) $mobile);// count mobile
125: if($mobile_cnt > 0){// more than 0
126: $mobile = array_unique($mobile);// array unique
127: $mobile = count((array) $mobile);// recount
128: }else{
129: $mobile =$mobile_cnt;// else display count is 0
130: }
131:
132: $all_cnt = count((array) $all);// ditto as above
133: if($all_cnt > 0){
134: $all = array_unique($all);
135: $online = count((array) $all);
136: }else{
137: $online =$all_cnt;
138: }
139: //var_dump($all);
140:
141: //display results
142: echo "Page views today $hits<br>";
143: echo "Users online today $online<br>";
144: echo "Mobile Users today $mobile<br>";
145: echo "Bots today: $bot ";
146: ?>