Posted: Sun Dec 30, 2007 1:51 pm Post subject: Users Online
In this tutorial, you will learn the basic way of showing how many users are currently browsing a webpage. This is useful because it can be a good "bragging" feature, and also tell you the time when most users are online. It is just one simple code that I will be explaining, but it does involve sql, so Ill give the table code below (It's only one table ).
Code:
CREATE TABLE `users_online` (
`last_visit` int(11) NOT NULL default '0',
`ip` text NOT NULL
)
Just enter that into phpmyadmin or something and we are ready to start coding!
-----------------
The Code
Since this feature only needs one file/script to run, I will just simple give the code, then explain it. So, the code is below. Just place this on any file, because filename doesn't matter.
-----------------
The Explanation
Now that you have the code in front of you, I will explain it step-by-step.
Step 1- The first step is simple. We just include the connection file that connect to the mysql database.
Step 2- In this step we declare some variables. The $time variable has the value of the seconds passed since January 1, 1970. The $offset variable can be any number you like. The default is 60 seconds. This variable is how long the user can be unactive before they are considered offline. The $most_time variable is the current time, minus the offset. This variable is used to determine whether or not to delete the user from the users_online table, or update them on that table with the current time. The $ip variable is simple the user's IP.
Step 3- In this step, we select the ip column from the users_online table where the ip column equals the user's IP.
Step 4- In this step, we check if the query returned any results. If it didn't, we insert the time and the user's IP into the users_online table. If the query did return results, instead of inserting the user into the table again or doing nothing, we update the last_visit column in the users_online table to the current time where the ip column equals the user's IP.
Step 5- In this step, we delete all rows in the users_online table where the last_visit column is less than or equal to the $most_time variable. In other words, we delete all rows where the IP column hasn't been updated in $offset seconds.
Step 6- In this step, we select all rows from the users_online table where the last_visit column is greater than the $most_time variable. In other words, we rows that have been updated in the past $offset seconds.
Step 7- In this step, we declare a variable called $users_online, and give it the value of the number of results returned when we selected rows from the users_online table. We then output that variable onto the page.
Step 8- In this step, we close the connection to the database for security reasons.
-----------------
There you have it! A complete script that can show you how many users are online. If you wish, you can put this code on all your web pages, to show you how many users are currently browsing your site. And, this tutorial doesn't require crons, the code itself deletes inactive users. Please post here questions, comments, or corrections.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum