|
||||||
| PHP Tutorials PHP Tutorials |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||||
|
Now I'll show you how to see how many users are browsing your website. There are many ways to display it but I'm trying to keep it simple and clean. First of all we have to create a two tables to your database. sql Code:
It will create two tables. One is session and other is time. They are going to store our information. Now it's time for PHP. Let's create our main variables PHP Code:
$ses = session_id(); - This will get our session's ID $time = time(); - This will get the time $timech=$time-300; - This will set our time to 5min Now we have to connect to the database: PHP Code:
$username = ""; - Your MySQL username $password = ""; - Your MySQL password $dbname = ""; - Your database's name Now we have to look for existing sessions and get the number of sessions. PHP Code:
$num = mysql_num_rows($result); - It will tell us how many active records are in session column PHP Code:
$result1 = mysql_query("INSERT INTO uonline (session, time)VALUES('$ses', '$time')"); - Inserts session's ID and time to database }else{ - But if there was more records than 0, let's update their records $result2 = mysql_query("UPDATE uonline SET time='$time' WHERE session = '$ses'"); - Updates existing records } - Ends If statement Now let's find our info again from the columns: PHP Code:
It's time for showing how many users are looking your site: PHP Code:
echo "There are: <b>".$usersonline."</b> users online"; - This will show how many users are on your site When the users have left, you must delete their records from database. PHP Code:
And here's the full code: PHP Code:
__________________
![]() CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | Freelance Cheap & Professional Web Design | Need help? Send a PM Last edited by Jaan; 04-08-2008 at 11:56 PM.. |
|
|||||
|
yes there are.. BUT when you're making your own..then it's feel much better.. ![]()
__________________
![]() CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | Freelance Cheap & Professional Web Design | Need help? Send a PM |
|
|||||
|
lol yes.. well.. i usually make all codes that i need myself.. because.. in my script.. i know where everything is.. but in already finished script i don't know.. where's this or that.. well.. when i want to customize it a little.. and when you make your own programs.. then you will learn very much.. ![]()
__________________
![]() CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | Freelance Cheap & Professional Web Design | Need help? Send a PM |
|
|||||
|
Yes, the method Jaan described above can be modified in several ways to meet your needs - especially since it relies on a database to store the sessions. However, if you simply want to count the amount of registered sessions, reading/writing/updating a database many times can be costly - another faster (20 times faster) method would be to just read the amount of session files that are in the servers session directory (amount of session files = amount of registered sessions). This method is more efficient for counting the amount of users, however if you want to "track" users, then using a database is a must. |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Using JS to see if a site is online? | phpforfun | JavaScript and CSS | 4 | 03-02-2008 11:14 PM |
| Netiquette - Are you behaving online? | TcM | The Lounge | 8 | 11-26-2007 05:15 PM |
| Online RPG For Sale - Predicted PR 5 | phb50530 | Site Reviews | 10 | 01-10-2007 05:59 AM |
Algorithms and Data Structures
Programming Language Popularity
Code Collaboration
Podnet IRC Network
AmpHosted
Goal #1: 1,000 Blogs
Goal #2: 1,000 Wiki Pages
Goal #3: 300,000 Posts
Goal #4: 20,000 Threads
Done: 30%, 23%, 55%, 75%