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:
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.Code:CREATE TABLE `navigation` ( `username` varchar(50) NOT NULL, `time` datetime NOT NULL, PRIMARY KEY (`username`) )
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)
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).Code:if(isset($_SESSION['username'])){ $user=$_SESSION['username']; $date=date('c'); mysql_query("replace into navigation (username,time) values('$user','$date')"); }
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:
no user will have more than one record, so the query will be really simple:Code:| Username | Time | ---------------------------------- | amrosama |2009-09-02 19:28:13 | | someuse |2009-09-02 15:27:36 | __________________________________
where $d will be the datetime value used to get active users, the whole DIV should look like this:Code:select username from navigation where time>'$d'
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)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>"; } ?>
-the rest should be easy, the index.php ,navigation.php and some screens are attached
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"
www.amrosama.com | the unholy methods of javascriptCode:eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
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
Good Job, sir!
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"
www.amrosama.com | the unholy methods of javascriptCode:eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
Very nice+rep
Nice bit of code there.
This is extremely useful as I am in the middle of building a CMS with a few friends.
Glad to hear it.
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"
www.amrosama.com | the unholy methods of javascriptCode:eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks