rajmohan Posted November 20, 2006 Share Posted November 20, 2006 hai guysi am not able to find out how many users are using my site i need the count of the user when they login please help not able to find out. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted November 20, 2006 Share Posted November 20, 2006 are you using a mysql database to store the users details? because this would make things easier. Quote Link to comment Share on other sites More sharing options...
rajmohan Posted November 20, 2006 Author Share Posted November 20, 2006 yes i am using mysql Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted November 22, 2006 Share Posted November 22, 2006 alright, well you could easily just add a couple of extra fields into your users table, which i am assuming you have setup like this:id | username | password | email | etc...----------------------------------------------------1 user1 pass1 email@email.com etc..alright, well i will show you how to do a timeout as well, so after a period of time the session is destroyed and they are signed out. can be useful. otherwise...no. :) first at 2 more fields called 'status' and 'timeout'. make timeout a varchar with 255 values and status a smallint with the value of 1 and a default of 0.we will use this rule: 0 = offline and 1 = online.ok...now...when the user signs in add this to your code, note you can take out the timeout bit if you want.[code=php:0]$timeout = time() + (30 * 60); //you can change the 30 to watever, the 30 stands for 30 minutes, but to get it we must multiply by 60.mysql_query("UPDATE `table` SET `status`='1', `timeout`='".$timeout."' WHERE `username`='".$username."'"); //remember to change the table and the username variable if you need to.[/code]now, when they logout do exactly the same thing except set status to 0 and timeout to nothing.if you are doing the timeout then you can read this otherwise skip it. if you have a log file or a file that is included in every page add this to it, remember to read it and change any values.[code=php:0]mysql_query("UPDATE `table` SET `status`='0', `timeout`='0' WHERE `timeout`<='".time()."'"); //this will remove any users who's timeout has passed...if(isset($_SESSION['user'])){ //checks to see if the user is logged in$user_timeout = mysql_result(mysql_query("SELECT `timeout` FROM `table` WHERE `username`='".$_SESSION['user']."'"),0); //just grabs the users timeout, remember to change any values.if($user_timeout <= time()){ //checks if the users timeout has passed, because we added the 30 minutes to it, if time() is greater then the user timeout we will sign them out.mysql_query("UPDATE `table` SET `status`='0', `timeout`='0' WHERE `username`<='".$_SESSION['user']."'"); //this will remove the userheader("Location: login.php"); //redirect the user to the login page again. or you can redirect them to an error page explaining why they have been signed out.}}[/code]now for the users online... simply do this:[code=php:0]$users_online = mysql_num_row(mysql_query("SELECT * FROM `table` WHERE `status`='1'"));echo "There are currently ".$users_online." users online!";[/code]you can spice it up a bit by adding a timeout in if they are inactive for to long. so whenever they refresh the page change the timeout to another 30 minutes or whatever.good luck. Quote Link to comment Share on other sites More sharing options...
jawapro Posted November 23, 2006 Share Posted November 23, 2006 Somehow you'll also need to check for any users with timeouts that have already passed, have gone away, but are still recorded as being logged in.For example, if someone clicks the CLOSE button on the browser WITHOUT logging out, the database will still think they are logged in.There would be ways around it - but they get sort of complicated. Quote Link to comment Share on other sites More sharing options...
ataria Posted November 23, 2006 Share Posted November 23, 2006 I would add a 'last_page' timer..that, whenever they go to a page.it updates that (simply enter the timestamp into the 'last_page' column..)then, for the list..do a query for all the usernames where the 'last_page' < timestamp-60.change the 60 to whatever amount of seconds you want... Quote Link to comment Share on other sites More sharing options...
jawapro Posted November 23, 2006 Share Posted November 23, 2006 That one would solve the problems I mentioned ;D Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted November 23, 2006 Share Posted November 23, 2006 if you read the code i did place this:[code=php:0]mysql_query("UPDATE `table` SET `status`='0', `timeout`='0' WHERE `timeout`<='".time()."'"); //this will remove any users who's timeout has passed...[/code]which updates the table for any users that closed the brower and there time has run out... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.