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

Thread: PHP: Currently Active Users

  1. #1
    Code Warrior
    /////////|||||\\\\\\\\\
    amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama's Avatar
    Join Date
    Aug 2007
    Location
    Pyramids st, Giza, Egypt
    Age
    21
    Posts
    8,181
    Blog Entries
    12

    PHP: Currently Active Users

    hi all,
    in this tutorial ill show you how to show the users that are logged on to your website like the one in codecall home page.theres several techniques that you can use to do so, i choose to use this one for simplicity.

    Overview:
    it will work by storing the last time a user accessed a page on your website in a table called "navigation" by placing a simple query on the top/beginning of all pages on your website, then to show the currently active users we make a query to show the records in table "navigation" in the last <X> minutes.

    Navigation table:
    the table that will contain a list of usernames with last time the user accessed a page on the website:
    Code:
    CREATE TABLE  `navigation` (
      `username` varchar(50) NOT NULL,
      `time` datetime NOT NULL,
      PRIMARY KEY (`username`)
    )
    as you can see it only has 2 fields Username, time. you can add more fields if you want to like IP address or page URL the user accessed.
    becareful this table can get huge if you dont specify a primary key (user), and insead of using regular insert statements we will use replace into or update.

    Navigation script: (navigate.php)
    Code:
    if(isset($_SESSION['username'])){
    	$user=$_SESSION['username'];
    	$date=date('c');
    	mysql_query("replace into navigation (username,time) values('$user','$date')");
    }
    pretty simple, eh? all it does is checking if theres a username key/variable in the $_SESSION super global array, if theres it means the user is logged in so we can update the navigation table with last time he accessed the website. i used "replace into" because its shorter than update statement and can be helpful if you have more than one primary key (in this case i dont).
    i placed this in a seperate script to call it (require/include) from other pages on the website like the index page we will see below

    Showing Results: (index.php)
    now when a user logs in to your website and open any page with the above script will insert/update the navigation table like this:
    Code:
    | Username  |  Time               |
    ----------------------------------
    | amrosama  |2009-09-02 19:28:13  |
    | someuse   |2009-09-02 15:27:36  |
    __________________________________
    no user will have more than one record, so the query will be really simple:
    Code:
    select username from navigation where time>'$d'
    where $d will be the datetime value used to get active users, the whole DIV should look like this:
    Code:
     <?php 
                 $d=date('c',time()-5*60);//last 5 minutes
                 $q=mysql_query("select username from navigation where time>'$d'");
                 if(mysql_affected_rows()>0){
    	             print "<ul>";
    	             while($users=mysql_fetch_array($q)){
    	             	print "<li>{$users[0]}</li>";
    	             }
    	             print "</ul>";
                 }
    ?>
    to make a date in mysql format i used "date('c')" and it takes a second parameter with the UNIX timestamp. you can get the current time stamp by calling the function "time()" to get the timestamp of 5 minutes ago we do this "time()-5*60" (5minutes X 60 seconds foreach minute)
    -the rest should be easy , the index.php ,navigation.php and some screens are attached
    Attached Thumbnails PHP: Currently Active Users-1.jpg   PHP: Currently Active Users-2.jpg  
    Attached Files

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: PHP: Currently Active Users

    Very helpful! The use of replace make it so easy. You've inspired me to write a tutorial on the one I just finished which differs from yours because it logs guests but basically the same.

    +rep

  3. #3
    Code Warrior BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch's Avatar
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Age
    19
    Posts
    2,221
    Blog Entries
    8

    Re: PHP: Currently Active Users

    Good Job, sir!

  4. #4
    Code Warrior
    /////////|||||\\\\\\\\\
    amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama's Avatar
    Join Date
    Aug 2007
    Location
    Pyramids st, Giza, Egypt
    Age
    21
    Posts
    8,181
    Blog Entries
    12

    Re: PHP: Currently Active Users

    thnx
    Quote Originally Posted by Jordan View Post
    Very helpful! The use of replace make it so easy. You've inspired me to write a tutorial on the one I just finished which differs from yours because it logs guests but basically the same.

    +rep
    please post it

  5. #5
    Guru debtboy is just really nice debtboy is just really nice debtboy is just really nice debtboy is just really nice debtboy is just really nice debtboy's Avatar
    Join Date
    Aug 2009
    Location
    I'm in the... Black Lodge
    Posts
    908

    Re: PHP: Currently Active Users

    Good Tutorial +rep
    The owls are not what they seem...

  6. #6
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,602
    Blog Entries
    57

    Re: PHP: Currently Active Users

    Very nice +rep
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  7. #7
    Newbie Overload will become famous soon enough
    Join Date
    Sep 2009
    Posts
    15

    Re: PHP: Currently Active Users

    Nice bit of code there.

  8. #8
    Newbie reddem0n is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    11

    Re: PHP: Currently Active Users

    This is extremely useful as I am in the middle of building a CMS with a few friends.

  9. #9
    Newbie Overload will become famous soon enough
    Join Date
    Sep 2009
    Posts
    15

    Re: PHP: Currently Active Users

    Glad to hear it.

  10. #10
    Code Warrior
    /////////|||||\\\\\\\\\
    amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama's Avatar
    Join Date
    Aug 2007
    Location
    Pyramids st, Giza, Egypt
    Age
    21
    Posts
    8,181
    Blog Entries
    12

    Re: PHP: Currently Active Users

    enjoy

+ 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. PHP 5 and OOP
    By Jordan in forum PHP Tutorials
    Replies: 11
    Last Post: 09-22-2008, 02:58 AM
  2. PHP 4 end of life announcement
    By Jordan in forum News
    Replies: 4
    Last Post: 08-30-2007, 10:55 AM
  3. PHP: Log Out Users
    By Ronin in forum PHP Forum
    Replies: 3
    Last Post: 04-26-2007, 10:42 AM

Bookmarks

Posting Permissions

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