Connect with Facebook Lost Password?


Go Back   CodeCall Programming Forum > Software Development > Tutorials > PHP Tutorials

PHP Tutorials PHP Tutorials

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-07-2008, 05:16 PM
Jaan's Avatar   
Moderator
 
Join Date: Dec 2006
Location: Estonia
Age: 18
Posts: 1,517
Blog Entries: 4
Rep Power: 24
Jaan is just really niceJaan is just really niceJaan is just really niceJaan is just really niceJaan is just really nice
Send a message via MSN to Jaan
Default How many users online

Hey!

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:
  1. CREATE TABLE `uonline` (
  2.   `session` varchar(100) NOT NULL,
  3.   `time` int(5) NOT NULL DEFAULT '0'
  4. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

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:
<?php
session_start
();
$ses session_id();
$time time();
$timech=$time-300;
session_start(); - This wills start a session
$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:
// Declare SQL login info
$host "localhost";
$username "";
$password "";
$dbname "";

// Now we are going to connect to our database
mysql_connect("$host""$username""$password")or die("<font style='color:red'><b>Can not connect:</b> ".mysql_error()."</font>");
mysql_select_db("$db_name")or die("<font style='color:red'><b>Can not select the database:</b> ".mysql_error()."</font>"); 
$host = "localhost"; - Your SQL's host name, usually it's "localhost"
$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:
$result mysql_query("SELECT * FROM uonline WHERE session='$ses'");
$num mysql_num_rows($result); 
$result = mysql_query("SELECT * FROM uonline WHERE session='$ses'"); - This will select all info from session column where it's value is "$ses"
$num = mysql_num_rows($result); - It will tell us how many active records are in session column

PHP Code:
if($num == "0"){
$result1 mysql_query("INSERT INTO uonline (session, time)VALUES('$ses', '$time')");
}else{
$result2 mysql_query("UPDATE uonline SET time='$time' WHERE session = '$ses'");

if($num=="0"){ - If there's no records in session column then we must insert some records
$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:
$result3 mysql_query("SELECT * FROM uonline"); 
$result3 = mysql_query("SELECT * FROM uonline"); - This will get all info from uonline table

It's time for showing how many users are looking your site:

PHP Code:
$usersonline mysql_num_rows($result3);
echo 
"There are: <b>".$usersonline."</b> users online"
$usersonline = mysql_num_rows($result3); - It will get the number of records in columns
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:
mysql_query("DELETE FROM uonline WHERE time<$timech");
?> 
mysql_query("DELETE FROM uonline WHERE time<$timech"); - Deletes records from database when 5min has been thru.

And here's the full code:

PHP Code:
<?php
session_start
();
$ses session_id();
$time time();
$timech=$time-300

$host "localhost";
$username "";
$password "";
$dbname "";

mysql_connect("$host""$username""$password")or die("<font style='color:red'><b>Can not connect:</b> ".mysql_error()."</font>");
mysql_select_db("$db_name")or die("<font style='color:red'><b>Can not select the database:</b> ".mysql_error()."</font>");  

$result mysql_query("SELECT * FROM uonline WHERE session='$ses'");
$num mysql_num_rows($result); 

if(
$num == "0"){
$result1 mysql_query("INSERT INTO uonline (session, time)VALUES('$ses', '$time')");
}else{
$result2 mysql_query("UPDATE uonline SET time='$time' WHERE session = '$ses'");


$result3 mysql_query("SELECT * FROM uonline"); 

$usersonline mysql_num_rows($result3);
echo 
"There are: <b>".$usersonline."</b> users online";  

mysql_query("DELETE FROM uonline WHERE time<$timech");
?>

Last edited by Jaan; 04-08-2008 at 11:56 PM..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-08-2008, 07:27 AM
Xav's Avatar   
Xav Xav is offline
Code Slinger
 
Join Date: Mar 2008
Location: The North Pole
Posts: 13,210
Blog Entries: 13
Rep Power: 105
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default Re: How many users online

Thanks for the advice. If you can't be bothered to write the code, there are lots of ready-made scripts available on the internet.
__________________

Quote:
Originally Posted by Jordan View Post
Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-08-2008, 10:42 AM
Jaan's Avatar   
Moderator
 
Join Date: Dec 2006
Location: Estonia
Age: 18
Posts: 1,517
Blog Entries: 4
Rep Power: 24
Jaan is just really niceJaan is just really niceJaan is just really niceJaan is just really niceJaan is just really nice
Send a message via MSN to Jaan
Default Re: How many users online

yes there are.. BUT when you're making your own..then it's feel much better..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-08-2008, 01:18 PM
Xav's Avatar   
Xav Xav is offline
Code Slinger
 
Join Date: Mar 2008
Location: The North Pole
Posts: 13,210
Blog Entries: 13
Rep Power: 105
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default Re: How many users online

Good point. Now you think about it - what's the point of having a programming forum if no-one bothers to program? I'll never forget that first moment of joy when my very first application worked. It was just a button that popped up with a message, then exited, but hey - everyone's gotta start somewhere.
__________________

Quote:
Originally Posted by Jordan View Post
Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-08-2008, 11:55 PM
Jaan's Avatar   
Moderator
 
Join Date: Dec 2006
Location: Estonia
Age: 18
Posts: 1,517
Blog Entries: 4
Rep Power: 24
Jaan is just really niceJaan is just really niceJaan is just really niceJaan is just really niceJaan is just really nice
Send a message via MSN to Jaan
Default Re: How many users online

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..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-09-2008, 12:07 PM
Xav's Avatar   
Xav Xav is offline
Code Slinger
 
Join Date: Mar 2008
Location: The North Pole
Posts: 13,210
Blog Entries: 13
Rep Power: 105
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default Re: How many users online

Very true, very true.
__________________

Quote:
Originally Posted by Jordan View Post
Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-16-2008, 10:50 AM
phpforfun's Avatar   
Speaks fluent binary
 
Join Date: Feb 2008
Posts: 1,164
Rep Power: 14
phpforfun has a spectacular aura aboutphpforfun has a spectacular aura about
Default Re: How many users online

is it possible to add the usernames to the database, when they exit out of the browser, it will remove them from the list? this way we could make a chat of some sort
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-16-2008, 12:29 PM
Xav's Avatar   
Xav Xav is offline
Code Slinger
 
Join Date: Mar 2008
Location: The North Pole
Posts: 13,210
Blog Entries: 13
Rep Power: 105
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default Re: How many users online

Oh, just get one off the internet. You can always customise it later!
__________________

Quote:
Originally Posted by Jordan View Post
Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-16-2008, 08:54 PM
John's Avatar   
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 5,305
Blog Entries: 24
Rep Power: 20
John is just really niceJohn is just really niceJohn is just really niceJohn is just really niceJohn is just really nice
Send a message via AIM to John Send a message via MSN to John
Default Re: How many users online

Quote:
Originally Posted by phpforfun View Post
is it possible to add the usernames to the database, when they exit out of the browser, it will remove them from the list? this way we could make a chat of some sort
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 01-05-2009, 03:28 AM
Newbie
 
Join Date: Aug 2008
Posts: 14
Rep Power: 0
seyz4all is an unknown quantity at this point
Default Re: How many users online

this can be helpful
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT -5. The time now is 01:35 AM.

Freelance Jobs

XML/XSL: Need code for Book with Chapers using XML
Create an XML file for a book of your creation, and a basic CSS file that will format it to display ...
Earn: $40.00


C++/C: Simple firework cue sequencer
What I require is a rework of a simple cue sequencer. I have a piece of hardware (an Arduino boar...
Earn: $50.00


HTML/XHTML: Menu Rework - ASCIIBin
I'm placing this in the HTML/XHTML section of the Freelance site but you are not limited to HTML. Wh...
Earn: $20.00



CodeCall Goal

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%

Ads