Bots Online Today Php Utility Script Tutorial
How to write a Php utility script for your website example tutorial
Hi!. This is one example of how to go about writing your first Php utility script.
This particular little gem anylizes the user agent string and looks for the words bot and spider.
if a bot is detected we extract the bot name and record the timestamp of the bots visit to your website.
We then write this data to a delimited text file.
The next step after writing the bot name to file we want to get all of the bot names
from the file and build an array for printing into your footer for display.
At the same time we are reading the file for bots and we will be
deleting bots from the file with a time stamp older than one day.
We do this by comparing the current time minus one day to the initial bot visit time.
As this script is ‘Bots Online Today’.
So we have now built an array of bot names from file entries and deleted bot entries older than 24 hour.
But the array may actually will have duplicate bot entries if the bot revisited within the 24 hours.
So what to do?
We now take the Bots array we built and using the array_unique php built in function we return a unique bot array.
after which we implode with a space and print to page.
That is it. See the script below with Logic process comments.
Please note: There is more than one way to write code to achieve the required result.
Only a coding Nazi much like grammer Nazis criticize peoples writing style or logic.
By writing code with the thought/logic steps for each code task you can think of other ways to do things.
Your coding will become more efficient with experience and knowledge of Php built in functions.
This here is not a web application just a utility script after all.
You don’t need no sql baby!
Hope you like this tutorial example and find it useful! Happy Coding!
Click here for all users and mobile users and bots tutorial
1: <?php
2: //BOTS ONLINE TODAY PHP UTILITY SCRIPT TUTORIAL
3: //by george @ CMXads.com
4: // How to write your first php utility script tutorial example
5: // write logic comments for each step of the process
6:
7:
8: //unix timestamp of script access
9: $bot_time = time();
10: //set bot variable so we do not get a php notice error
11: $bot ='';
12: //file to write bots
13: $file_name = 'botsonline.txt';
14:
15: if(isset($_SERVER['HTTP_USER_AGENT'])){// if we detect a UA
16:
17: //get UA string
18: $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
19:
20: // testing UA string. comment out for production
21: $ua = strtolower('Mozilla/5.0 (compatible; dasSpider/0.10; +https://www.mojeek.com/bot.html)');
22:
23: // look for bot in UA string. can use strpos but I like to mix it up
24: if(strpos( $ua, 'bot/') !==false || strpos( $ua, 'spider/') !==false) {
25:
26: // if found explode on space
27: $uaParts = explode(' ', trim($ua));
28:
29: // search UA parts array for bot string
30: foreach ($uaParts as $bots) {
31:
32: // found explode / and get bot name
33: if( strpos( $bots, 'bot/') !==false || strpos( $bots, 'spider/') !==false){
34: $getBot = explode('/', trim($bots));
35: }
36: }// end loop
37: $bot = $getBot[0];// no need to be in loop
38: }
39: //echo $bot;
40: // write timestamp and bot name to file
41: if($bot){
42: $new_line = $bot_time . '##' . $bot . PHP_EOL;
43: file_put_contents($file_name, $new_line, FILE_APPEND | LOCK_EX);
44: }
45: }// end get bot
46:
47: // next get bots from file
48: // if we have a file and it has bots in it
49: if(file_exists($file_name) && filesize($file_name) >5){
50:
51: $files = file($file_name);// get file lines as array
52:
53: // set new array of bot file lines after checking timestamp
54: $new_file = array();
55: //set array of bots fo8und
56: $all = array();
57:
58: foreach ($files as $line) {// read each line and explode delimiter
59: $parts = explode("##", $line);
60: //parts[0] is time stamp
61: //if time stamp is greater than current timestamp minus one day
62: if($parts[0] >= $bot_time - 86400)
63: {
64: $all[] = $parts[1];// parts[1] bots name to array
65:
66: // now while still in loop comparing time delete lines greater than 1 day old by not putting back into file
67: //minutes 86400 24hrs Bots on line today
68: $new_file[] = $line; //put lines back into file that are still within the 24 hour period
69: }
70: }// end loop
71:
72: // write updated bots back to file. can be an array as is here
73: file_put_contents($file_name, $new_file, LOCK_EX);
74:
75: // now array unique out duplicate bot names
76: $arraybots = array_unique($all);
77:
78: //now implode filtered array back to string
79: $bots = implode(" ", $arraybots);
80: if(empty($bots)){
81: $bots = " none";
82: }
83: // display in footer. style as desired
84: echo '<div id="footer"><div class="bots" style="width:200px; float:right;"><span style="font-weight:bold;">Bots online today:</span> ' . $bots . '</div></div>';
85: }
86: ?>