CMX Ads Webmaster Resources for Success

Websites, Advertising, Scripts Tips n Snippets

News: Hackers How They Try to Hide

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_lineFILE_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_fileLOCK_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:
?>
 
Click here for the Full download.

Rate me please.
Current Rating: 3.1 Thumbs up!
Vote 1 | Vote 2 | Vote 3 | Vote 4 | Vote 5

© 2021 CMXads.com Thumbs up rating script.


Please help me I am handicapped and support myself

Donate With PayPal

 

Donate Bitcoin

Please donate and help the handicapped.

1Ev8n7R63yqkKsFTrztufvuVah44MCbzpN



name:Melaine Date:11.23.23 @ 15:51pm Country:
Thanks your the greatest.


name:Moses Date:08.7.22 @ 06:21am IP:.711.9311.8240
I will give it a try. thanks.


name:Katerine Date:08.7.22 @ 06:21am IP:..9011811743.2
Thanks for sharing.


name:Gavin Date:08.7.22 @ 06:21am IP:1.1.7489.31201
Thanks!


name:Steve Date:04.30.20 @ 14:28pm IP:5.188.84.76
I am using this worked out great I sent you 9 cents.


name:Daphine Date:03.19.20 @ 19:32pm IP:5.188.84.76
I will give it a try. thanks.


name:Khadijah Date:03.13.20 @ 22:38pm IP:5.188.84.76
Thanks you are awesome I sent you 6 cents.


name:Abbie Date:03.6.20 @ 13:50pm IP:5.188.84.76
This worked out great I sent you 19 cents.


name:Mark Date:02.21.18 @ 18:25pm IP:146.185.223.252
Thanks I admire you I sent you 67 cents.




Name:
Click Here to Reload

My websites do not use cookies or any google spyware.

 

Quick Support: Make it short.
Email:

Message: