Jump to content

php / people online at moment question!


Fsoft

Recommended Posts

Hey,

 

How you doing all? well Again, Like before I am up with a new php problem (question)..

 

This time, I tried the answer on Google! but wasn't able to find what was searching for so though you might help me...

 

The qestion is MySQL and php based.

 

Now, I make a script which uses sessions. Now once script is browsed for the first time, as it uses session, it will make a query like this;

 

<?php
session_start();

if($_SESSION["this_people"] == "already_counted")
{
//do nothing...........
//Perform the script task...........
}else
{
$query1 = mysql_query("select * from sessions");
$query1_fetch = mysql_fetch_array($query1);
$current_people_view = $query1_fetch["people_number"];
//The people number is the number written by same script before, however, once you see more code you will understand...

$new_user_entry = $current_people_view + 1;

$query2 = mysql_query("insert into sessions (people_number) values ('$new_user_entry');
$query3 = mysql_query("select * from sessions");
$query3_fetch = mysql_fetch_array($query3);
$_SESSION["this_people"] = "already_counted";
}
echo "Currently $query3_fetch[people_number] are seeing this page";

?>

 

Hope this code makes a sense to you, In this manner we will able to show , how much people are active...

 

This can be used for a registration script also, I mean for registered members also , using this manner of code, we can know how much registered users are logged in and after we can echo their usernames and do that kind of stuff.. And using a log-out link we can just use a new MySQL query fetching the exiting number of online users and making $user_at_moment = $fetching_array["people_count"] -1;

And write back in database...

 

But the problem is that if some one don't click on log-out and just close their browser sheet, they will be shown as online until they don't log in and log-out..

 

Now I need help with this, As you know Script will use session it means after every 25 mins session will expire, and once it's expired there should be a way in which it will auto-matically decreses the count of online people once a session is expired, so lets say 10 people are online and after 5 mins for 4 of them , then session is expired, so once we refresh the script, it will show up 6 people online, not 10...

 

HOpe it makes a sense to you guys, Please help me how can we do it, or any kind of help will be helpful,

 

Thanks a lot, waiting for answer.

 

FAISAL!

Link to comment
Share on other sites

http://www.daniweb.com/forums/thread137990.html

 

this thread here might help you

 

Yes,

 

1st of all thanks, But I know about session_destroy(); and this is what I mentioned in text, but that will be only possible if some one will click on Log-out link, it will destroy session and write a query in database with a user offline details,

 

But problem is that if user don't click on Log-out so session will not destroy "that don't matter" but the query will also be not written in database with offline details if user just closes his / her browser

 

My question is, using a way, when a user logins, his username is taken in session and session id, and when that session expires, no matter if user click on logout or not, or close browser or not, it will write a query in MySQL giving users offline info.........

 

Hope it makes clear, what I am trying to say, Thanks, I am waiting for help..

 

FAISAL!

Link to comment
Share on other sites

Web servers only know when browsers make http requests for web pages. You can try, but it won't work in all browsers, to get an onUnload even to generate a http request.

 

So, to do this reliably you need to store/update the last requested time for each visitor in a database table (or a flat file database) on every page request. Then to display who or how many are online, you query the table for last request times that are newer than some limit you pick (typically 10-20 minutes is used, you can use AJAX to shorten this down to a minute or less depending on how frequently you want to hit your server with requests.) To automatically log someone out when their last request time is older than some limit, just query for the records that match what you want and then set the value in your user table that says they are logged out/not logged in.

 

There are literally 10's of thousands of existing "online/loggout if not online" scripts that work this way. If you search, you should be able to find an existing script that shows how to do this.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.