Hello everyone!
Today I'll try and teach you how to use files to count logged in users!
I've tried to make this script partly editable, so please change the options as you wish!
First of all, YES this IS a function, so please be nice to it ;-) I'll try to join all parts so you can copy and paste all this code straight from the box and put it together!
Feel free to edit the $config array values, try not to change the name if you can ;-)
Next, we check if the file exists, thanks to Specific for this idea :)
Of course, this would die if the file could not be created, (You probably don't have permissions to create please fix ;-)
Alright, now, lets get the IP address!
There are many ways you can do this, also, if you're worried about somebody opening up the file and reading users IP's, feel free to MD5 the getenv values just for a bit of extra security :)
Now lets get the date and time they connected!
You should leave this part alone, the script is pretty sensitive about this stuff!
Next. We're going to open up the previous log file, and get all the users in it!
But we gotta make sure its there first!
Yes $users will be set to blank if the file is empty!
Now, we add an IF to see what $users is, if it IS empty then we're the first online!
Of course, with every IF, we need an ELSE! (or atleast in this case!
Alright! Someone else is online (or my other self), now, lets get their info from the file!
First we better check if it's been updated since before today, if not, we dont want them back in our file!
Also, we want to make sure its not US that we're looking at!
Next we check if the they were online Less then the value in $config["min_online"] if its MORE, we'll say they've stopped viewing! if so, we'll add them BACK into the file with their orignal time!
Alright, now we add our information to $online_users since we're online! right? :)
Next, we empty the old file, now that we have everyting we need!
The next wile is to insert all the data back into the log file :) including our connection time!
Now, we finish everything off!
The if is to check that the file DOES have information in it, it was orignally used because the first script run returned 0 online users, this makes sure it returns atleast 1!
Then we count the number of entries in the file, (minusing 1 for the extra \n at the end of the file!)
However, if there is only 1 line, then we'll be back at 0! so we'll add a check for that and make it 1 if thats the problem,
Then we return the number of users online!
The above is just a simple useage of this script. I hope you all learnt something here!
Enjoy showing your users how many people are online! (The fast way!)
| Author Info | Comment |
|---|---|
| Specific Posted about 1 year ago. |
Thanks Jordan, works really good. I set up a little example of this script at http://www.zonehacks.com/test/Online You can find the text file at http://www.zonehacks.com/usersonline/online.txt |

