+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: PHP: Currently Active Users

  1. #1
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    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 Attached Thumbnails PHP: Currently Active Users-1.jpg   PHP: Currently Active Users-2.jpg  
    Attached Files Attached Files
    yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    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

  4. #3
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: PHP: Currently Active Users

    Good Job, sir!

  5. #4
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    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
    yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

  6. #5
    Join Date
    Aug 2009
    Location
    ~/
    Posts
    918
    Rep Power
    19

    Re: PHP: Currently Active Users

    Good Tutorial +rep

  7. #6
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143

    Re: PHP: Currently Active Users

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

  8. #7
    Overload is offline Newbie
    Join Date
    Sep 2009
    Posts
    16
    Rep Power
    9

    Re: PHP: Currently Active Users

    Nice bit of code there.

  9. #8
    reddem0n is offline Newbie
    Join Date
    Sep 2009
    Posts
    11
    Rep Power
    0

    Re: PHP: Currently Active Users

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

  10. #9
    Overload is offline Newbie
    Join Date
    Sep 2009
    Posts
    16
    Rep Power
    9

    Re: PHP: Currently Active Users

    Glad to hear it.

  11. #10
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Re: PHP: Currently Active Users

    enjoy
    yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Get active hyperlink ?
    By pokevitek in forum JavaScript and CSS
    Replies: 2
    Last Post: 09-18-2010, 12:39 AM
  2. View all users in Active Directory!
    By Sharper_Software in forum Visual Basic Programming
    Replies: 0
    Last Post: 03-10-2010, 02:38 PM
  3. How to figure out if UAC is active?
    By vbigiani in forum General Programming
    Replies: 2
    Last Post: 03-05-2008, 12:17 AM
  4. Active Transfers?
    By ptt3 in forum Linux Networking
    Replies: 0
    Last Post: 06-14-2007, 04:41 PM

Tags for this Thread

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