| Welcome to Chibiwolf. We hope you enjoy your visit. You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free. Join our community! If you're already a member please log in to your account to access all of our features: |
| Online User Counter - Php Tutorial | |
|---|---|
| Topic Started: Jun 15 2008, 05:41 PM (50 Views) | |
| MusicUploader | Jun 15 2008, 05:41 PM Post #1 |
![]()
The Ownager
![]() ![]() ![]() ![]() ![]() ![]()
|
1. The idea Okay, so what exactly are we going to be doing here? Well the aim is to be able to count how many users are currently on your web site so in order for us to do this we need to be tracking them somehow. By using a MySQL database and just a single table we can do exactly that. For this to work you will need access to a MySQL database - if you do not have this then you should contact your host and find out if they can provide this for you. (recommended Free hosts Quotaless, X10-Hosting, Neoshares Post To Host) 2. Set up the database Once you've got your database created and ready (whether you or your host does it) it's now time to get to work on our table. Before creating any tables you should ask yourself exactly what data you need it to store. In the case of this script you will need to store just two things: the visitors IP address and the time they last visited. In order to create this table you can either do it manually using the table creation section of phpMyAdmin or alternatively you can just run the following SQL query on your database.
The above query will create a table called "user_online" which has two columns - ipaddress and lastactive. The ipaddress column has been give the VARCHAR data type which means it accepts a string of all characters of up to 15 in length. This is needed because a typical IP address in the form of xxx.xxx.xxx.xxx is 15 characters long. The lastactive colum has been given the INT data type which basically means it can accept integers with a maximum length of 10. Now that we have created our table now is a good time to take note of a few things. Firstly you will need to know the name of your database - for this tutorial mine is called "mydatabase" but yours will most likely be different. You will also then need the username and password for that database. These are needed so that our PHP script can connect to the MySQL database. And then finally you will need to know your server name but this isn't vital - usually you can substitute it with "localhost" and it should work just fine. 3. The main code Now that the database is ready we can now start looking at the PHP code which will make up our user tracking script. Take a look at the following piece of code which should be saved as "online.php" or something similar.
The above code is our entire user tracking script. Now let me take you through it. In line number 2 we use the mysql_connect() function. This function allows PHP to connect to the MySQL server so that it can then access its databases. This function requires three parameters which are the servers address (although "localhost" usually does the trick), the servers username (in this case "root") and the servers password (in this example it's been left blank). All three of these details can be obtained by speaking to your host. We then on line 3 have the mysql_select_db() function. Similarly to mysql_connect(), this function is required if you wish your script to connect to a MySQL database. Where the previous function connects to the server this function takes care of selecting which database to use. It requires only one parameter (although you can provide others for slightly more advanced use) which is the name of the database which you would like to connect to (my database is called "mydatabase"). Again if you are unsure of the names or if you do not even have access to any MySQL databases get in touch with your host. Now that our script has connected to the database we can now start working on the main section of our script. Now the logic behind this script can be explained like this. Has the user visited before? > If yes Update users last active time in database > If no Insert new users details into database Remove any old records which are now unnecessary Output number of online users So as you can see the first thing to do (apart from connecting to the database) is to use an if statement to determine if the person visiting is new or if they have visited before. This is done by querying our table using an SQL query and the mysql_query() and pull all rows where the ipaddress column is equal to the current users IP address. If a row exists then we need to do another query to update that rows lastactive column. If however no rows are returned then we need to insert the current visitors IP address and the time into the table as a new row. Our initial query and the if-else statement is as follows.
In the code above we have used the mysql_num_rows() function - this function counts the number of rows which the supplied query returned which we can then use in our if-else statement. That's the 'tracking' part over and done with. We could just go straight ahead and count all the rows in the table to determine how many people have visited - but that wouldn't necessarily show how many are actually on our site. To do this we need to remove any rows which are too old (more than 5 minutes old). To do this we simply perform another query on our user_online table and delete all rows where the lastactive column < NOW - 300. The number 300 is used because there are 300 seconds in 5 minutes. This is done in this line.
Brilliant. Now we can move on and find out how many people have visited our script in the last 5 minutes, which is probably the easiest way to determine how many people are currently on your site. To do this we do a simple query and pull all rows from our table. The number of rows returned equals the number of users online.
4. Implementing it As far as our tracking script goes we're all done. All that's left to do is to implement it into your web site so you can see how many users are currently online. There are two possible ways of doing this. Firstly we could use the PHP include() function and include our online.php script into all of our PHP enabled web pages. This can be done with just one line like this.
Alternatively, if your web page is not PHP enabled then you can include it using an iframe. This can be done by using the following HTML code.
That's it - you should now be able to keep a track on how many people are currently on your site. Enjoy! |
|
I Loved Her So Much I Saw A Chance And Missed It Now Im Broken ┏┫ | | ┣┓ ┏┓ ┗┫━━ ┃ ━━┣┛ ┣┫ ┃ ━━━━━ ┃ ┏┳┫┣┳┓ ┗━━┳━┳━━┛ ┃ ┃ ━━━━┃ ┃ ┗━┳┳━┛ ━━━━┃ ┗━━━━━━┛┃
| |
![]() |
|
| Chibi | Jun 15 2008, 07:13 PM Post #2 |
![]()
Shmell mah finger
![]()
|
Pretty good. I may try it some other time. |
![]() "I'd kill the Jews." -glare- "I'd not kill the Jews, no. I'd toss in a penny and watch them fight to the death. -snickering- I did the same with two Catholic priests but I tossed in a small boy!! And the winner, had to fight Michael Jackson!"%mh%-100%mh% | |
![]() |
|
| DarkCannon | Jun 15 2008, 07:16 PM Post #3 |
![]()
Willy Wolf
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
|
0.o HOW DO YOU HAVE TIME TO WRITE THIS! Very good XD |
| [size=14]I AM A BUNNY! FEAR ME! I SHALL NIBBLE ON YOUR CARROTS! RAWR![/size] | |
![]() |
|
| <span style=salamander | Jun 16 2008, 08:13 AM Post #4 |
|
Petty Possum
![]() ![]() ![]() ![]() ![]() ![]() ![]()
|
Pretty good tutorial. Kinda long but sweet. |
|
A word a word, A life for a word. Damn those romans were stupid! | |
![]() |
|
| MusicUploader | Jun 17 2008, 12:06 AM Post #5 |
![]()
The Ownager
![]() ![]() ![]() ![]() ![]() ![]()
|
lol my job is webhosting support on a warez/webhosting site which i also do music uploads for so i pretty much get paid to do stuff like this
|
|
I Loved Her So Much I Saw A Chance And Missed It Now Im Broken ┏┫ | | ┣┓ ┏┓ ┗┫━━ ┃ ━━┣┛ ┣┫ ┃ ━━━━━ ┃ ┏┳┫┣┳┓ ┗━━┳━┳━━┛ ┃ ┃ ━━━━┃ ┃ ┗━┳┳━┛ ━━━━┃ ┗━━━━━━┛┃
| |
![]() |
|
| <span style=Zyenet | Jun 27 2008, 02:08 AM Post #6 |
|
Stone Skipper
![]() ![]() ![]() ![]()
|
Nice job. Very simple script, but good quality. Thought I should point out, you need two other things for this to work. In your code snippets, you are missing the PHP tags, which are necessary for the PHP parser to know you have code needing to be parsed. Before your code, add this:
And at the end, add this:
Then everything will work.
|
![]() Clicking the image above makes you happier, and healthier. ![]() Play classic video games online at the Zyetendo Arcade. Registering to use it takes under a minute, and you get access to tons of classic games like Pacman, Donkey Kong, Zelda, etc. | |
![]() |
|
| Chibi | Jun 29 2008, 01:56 AM Post #7 |
![]()
Shmell mah finger
![]()
|
i thought that but i was saying that i have no idea what i was talking about. Guess half way on both ends. XD |
![]() "I'd kill the Jews." -glare- "I'd not kill the Jews, no. I'd toss in a penny and watch them fight to the death. -snickering- I did the same with two Catholic priests but I tossed in a small boy!! And the winner, had to fight Michael Jackson!"%mh%-100%mh% | |
![]() |
|
| « Previous Topic · Computer Talk · Next Topic » |






![]](http://209.85.48.18/static/1/pip_r.png)









2:38 AM Nov 26