CMX Ads Webmaster Resources for Success

Websites, Advertising, Scripts Tips n Snippets

News: How Hackers Inject Data

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_lineFILE_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_fileLOCK_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:
?>
 

Click here for the script in a text file.

Please help me I am handicapped and support myself

Donate With PayPal

 

Donate Bitcoin

Please donate and help the handicapped.

1KtuX6N6xM2YxijE7JfPeHZmtGKGQwgfpc



name:Arty Date:12.1.22 @ 16:45pm Country:
Thanks for sharing.


name:Jordan Date:12.1.22 @ 16:30pm Country:
This worked out great for me too. I sent you 82 cents.


name:Page Date:12.1.22 @ 11:31am Country:
Worked out great for me I sent you 29 cents.


name:Arty Date:12.1.22 @ 05:11am Country:
Thank you . I Sent you 5 cents.


name:Hilton Date:12.1.22 @ 04:56am Country:
Thanks I sent you 89 cents.


name:Marcelene Date:08.7.22 @ 06:38am IP:9470.112.3.811
Thanks your the greatest.


name:Oswaldo Date:08.7.22 @ 06:38am IP:...41239171081
Thanks I admire you I sent you 67 cents.


name:Abbie Date:08.7.22 @ 06:38am IP:378124.101.19.
I am using this worked out great I sent you 9 cents.


name:Summer Date:08.7.22 @ 06:19am IP:.4.8193112.017
Thanks you are awesome I sent you 6 cents.


name:Lucretia Date:08.7.22 @ 06:19am IP:1..1.728114390
Thanks I sent you 89 cents.


name:Ulrike Date:08.7.22 @ 06:19am IP:9270.1813.114.
A great help thanks I sent you 35 cents.


name:Kathrine Date:08.7.22 @ 04:23am IP:2137194..80.11
Thanks you are cool I sent you 7 cents.


name:Valorie Date:08.7.22 @ 04:23am IP:174.12.108.391
Thanks I love you I sent you 58 cents.


name:Lucretia Date:08.7.22 @ 04:23am IP:20497311.1..18
Tyvm.


name:Teddy Date:08.7.22 @ 04:23am IP:.384.79211011.
This worked out great for me too. I sent you 82 cents.


name:Hope Date:08.7.22 @ 04:23am IP:4.197011138..2
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: