+ Reply to Thread
Page 1 of 2
1 2 LastLast
Results 1 to 10 of 17

Thread: How many users online

  1. #1
    Moderator Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan's Avatar
    Join Date
    Dec 2006
    Location
    Estonia
    Age
    18
    Posts
    2,052
    Blog Entries
    9

    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.

    [highlight="sql"]
    CREATE TABLE `uonline` (
    `session` varchar(100) NOT NULL,
    `time` int(5) NOT NULL default '0'
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
    [/highlight]

    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
    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:

    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.

    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

    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:

    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:

    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.

    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:

    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.
    Trill Hosting - Cheap Web Hosting, Register Cheap Domains, Cheap Blog Hosting
    www.trillhosting.com | support@trillhosting.com
    Hosting Plans | Write To Us | Support | Client Area | About Us
    CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | Freelance

  2. #2
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    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

  3. #3
    Moderator Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan's Avatar
    Join Date
    Dec 2006
    Location
    Estonia
    Age
    18
    Posts
    2,052
    Blog Entries
    9

    Re: How many users online

    yes there are.. BUT when you're making your own..then it's feel much better..
    Trill Hosting - Cheap Web Hosting, Register Cheap Domains, Cheap Blog Hosting
    www.trillhosting.com | support@trillhosting.com
    Hosting Plans | Write To Us | Support | Client Area | About Us
    CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | Freelance

  4. #4
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    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

  5. #5
    Moderator Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan's Avatar
    Join Date
    Dec 2006
    Location
    Estonia
    Age
    18
    Posts
    2,052
    Blog Entries
    9

    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..
    Trill Hosting - Cheap Web Hosting, Register Cheap Domains, Cheap Blog Hosting
    www.trillhosting.com | support@trillhosting.com
    Hosting Plans | Write To Us | Support | Client Area | About Us
    CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | Freelance

  6. #6
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    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

  7. #7
    Speaks fluent binary phpforfun has a spectacular aura about phpforfun has a spectacular aura about phpforfun's Avatar
    Join Date
    Feb 2008
    Posts
    1,204
    Blog Entries
    17

    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

  8. #8
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    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

  9. #9
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,883
    Blog Entries
    25

    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.

  10. #10
    Newbie seyz4all is an unknown quantity at this point
    Join Date
    Aug 2008
    Posts
    14

    Re: How many users online

    this can be helpful

+ Reply to Thread
Page 1 of 2
1 2 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Using JS to see if a site is online?
    By phpforfun in forum JavaScript and CSS
    Replies: 4
    Last Post: 03-02-2008, 11:14 PM
  2. Netiquette - Are you behaving online?
    By TcM in forum The Lounge
    Replies: 8
    Last Post: 11-26-2007, 05:15 PM
  3. Online RPG For Sale - Predicted PR 5
    By phb50530 in forum Site Reviews
    Replies: 10
    Last Post: 01-10-2007, 05:59 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts