I have a logging system on my site, but I don't know how to get a user's status (online/offline). I've read more themes, but I don't understand them... can someone give me an example?
You don't know if the page is close from the server, you can't check that with PHP.
You can check with javascript on client side and call your server in ajax on each events.
For exemple with Jquery :
<script>
$(window).unload( function () { $.get('http://exemple.com/user.php?offline=true'); } );
</script>
Just add an column status in your user_info table in database and when user logs-in then update it with 'loged-in' and when it logs out then update it with 'Logs-out'
I'd keep track of the user's actions; this is what Stack Overflow does (along with "last seen" instead of "online" or "offline") and it works very nicely.
Pick a criterion:
Requested a page
Posted something
Whatever else applies to your website in particular
If the user hasn't done that in the last n minutes, then you can assume the user is offline. I'd say a good threshold is probably 30 minutes.
Related
I am currently using laravel 5.4, i get the list of online users, and i want from my admin-dashboard to make a specific user from my list to disconnect,
is there a way to do so ?
Set a flag in the database, for example, to mark the user as needing to logout. Then on their next request, in say a middleware, you can do:
if (Auth::user()->should_logout) {
Auth::logout();
}
There is a workaround to do this but I think it's
not optimum!
In users table we should have a field, say conected, then call an ajax request every 10 seconds (example), this ajax request is checking for connected field and sending user id, stating that user still is online, now when you change this field manualy, any time user click on a link or refreshes the page, they are logged out!
update:
#btl's idea to use window.reload() is good and does not need refreshing !
I'm working on a Doctor-Patient project(Web+Android) where when patient books a token, it first checks if doctor is online or not; if it is online patient can book.
For this purpose I have used ping user IP approach i.e. when patient submits token, it pings at Doctor's IP and if he's online(has active internet connection) it registers patient token. But that seems not to be working as received data from ping indicates 100% packet loss.
Can't use flag setting which is triggered on doctor's last action which is not real time.
Please suggestion if you have solution for this issue.
<?php
// unset variables first to avoid mixing the results from previous calls
// 0 for success, 1 for packet loss, 2 for other error.
$retval=-1;
$output=array();
exec("ping 127.0.0.1 -c2 -w2 2>&1",$output,$retval);
echo "Return code: ".$retval."<br>\n";
echo implode("<br>\n",$output);
?>
Many sites ignore ICMP (ping). If you have the doctor have his machine turn off the ICMP filtering, that may do it.
insert a flag in user table means add extra column in your user table. when user is login make a flag on . when a user can logout off that flag. e.g
|is_login|
1
or
|is_login|
0
then it is easy any where you can query from database and can check that the doctor is online or offline.
Okay, so how I have understood your question, you want to know check if the doctor is using your website/app or not at the moment. So, this can be done by adding one more field to the database. Let's call this User_activity. Now, When a user logs in you can store the time in this field then you can make a query to see who all have this time in last 5-10 minutes(as per your requirement). Now, once you have done this you can ping the user in every 60 seconds to check whether the user is still there or not and from here you can update your database field as well. So, by this, you can check who is online and who is not.
var checkstatus = setInterval(function () {
/* jQuery - back to server*/
$.get("checkstatus.php");
}, 60000);
Problem is solved through 'EventSource' Method in JavaScript.
Just include var source = new EventSource("set_online.php");
Include above line in each page.
Then on set_online.php set online-status flag which will be updated in database.
That's it! as long as you are on that page user is set online thus indicating he is connected to internet.
I'm currently trying to display all online users on my SITE'S userpage using the php session variables. To do this, whenever a user logs in or out, a column in a database gets set to "1" or "0".. However this doesn't entirely work since the database doesn't get updated when the user closes their browser (and therefor destroys the session). So is there another way of checking if a certain sessionid is set??
I also want to know how the twitter and facebook handle this ?
You almost have it. The way that the vast majority of sites deal with the issue is to have a table like you do, but they add a timestamp to it and update the timestamp when a new page is loaded.
When querying the table, you simply look for say the last five minutes of active users and they are the "live" users on the site.
Technically, you don't even need to keep the "logged in/out" value in that table. If they have been logged in within the lat five minutes, they are probably still about.
There is no guaranteed, sure-fire, totally bullet-proof way of checking if a user is there or not. You can do some tricky JS to ping on and off, you can add even more JS that will try to alert the db when the user navigates away from the page - but at the end of the day, you cannot do anything if a browser is closed unexpectedly, or if that user loses power, or network.
On top of that web browsing is by default stateless and doesn't maintain a connection to the user after the server has finished sending code. The best we can efficiently do is update a table when the user does something new and assume they will be around for a few minutes at least.
I haven't checked but Twitter and Facebook most likely have Javascript code which notifies the server when somebody closes the page, probably coupled with a periodic heartbeat and timeout.
Check the onunload event and XMLHTTPRequest to see how you can make a request to your PHP application notifying of an user leaving (a library like jQuery might help you do this much more easily).
Add a field "last_visit" to user's Table and update it every time when user visit your site
When user login to your site find "last_visit" time and current time,after that use this function
$time_period = floor(round(abs($current_time - $last_visit)/60,2));
if ($time_period <= 10)
$online_offline_status = 1;
else
$online_offline_status = 0;
}
and then print your final result
<?php if (
$online_offline_status == 0){ ?>
<span style="color:#FF0000;">Offline</span>
<?php } else if ($online_offline_status == 1) {?>
<span style="color:#669900;">Online</span>
<?php }?>
I've binged a lot for this stuff, but couldn't find direct ANSWER,
I've searched for this here,
But
I am still beating my head against the wall trying to implement,
How do I do update query after session expired?
I'm not talking about explicit clicking "Logout" button
Here's basic SQL structure:
CREATE TABLE auth_users (
email varchar(40) NOT NULL,
password varchar(40) NOT NULL,
online ENUM('1') DEFAULT NULL <-- HERE, it updates to 1 when user logged in, it updates back to NULL when user explicitly clicks on LOGOUT
) type=MyISAM;
Class Hierarchy:
interface {
function login();
function logout();
//calls after succes authorization
function set_as_online();
//calls from within logout() method
function set_as_offline();
}
BUT IT DOES NOT UPDATES BACK TO NULL WHEN USER CLOSES HIS BROWSER,
For example,
Assume we have two users: User-A, User-B
User A logged successfully, now User-B can see User-A as ONLINE user.
If User-A forget to click "LOGOUT" and would close his browser,
user-B still can see User-A as ONLINE. That's the problem.
For example,
Facebook handles this very well,
Assume your friend just closed the browser (i.e his session does not exists anymore),
then somehow you can see him as OFFLINE
What am I doing wrong? Incorrect approach of handling offline/online users?
Another approach is needed here i think. Don't set a online/offline flag, but a 'last_seen' timestamp. Ie. with every request update the record to the current timestamp. If you want to know if the user is online, just do:
if($current_time - $last_seen_time < $session_expire_limit) {
// online
} else {
// offline
}
Otherwise you'd need a cronjob of some sort to automatically reset the online flag in your database after a certain time, but then still you'd need a last_seen column.
// edit
i don't know exactly how facebook does it, but it could be one of the following; for the chat and notify functionality facebook opens up a 'stream', which is in fact a little ajax call which is kept alive by the server (btw, this ajax call is refreshed every 40 seconds). Possibly this stream is used to track online users. Another option, but less likely, is that an event is attached to the window.unload event. This is less likely because a page refresh, a clicked link to another facebook page etc. is also triggering the event. This would mean that every time an internal facebook link is clicked the event should be unbinded from the browser.
Can't think of another way atm, just some suggestions. Unfortunately those are quite labor-heavy to implement, I assume my suggestion above (before the edit) should be suitable for a common website.
I am not sure how facebook controls this stuff but i can suggest you from the top of my head how i would approach this matter.
I would add a new field on your auth_users table of type Date that will represent the session_expiry_time .
Then inside your html pages you should implement some silent ajax code that will call a dummy php page on the server (the interval is something very important because you have to balance performance and functionality). This dummy page will update the session_expiry_time of the user in
the auth_users table.
Therefore, in any given time, checking a user's session_expiry_time against current time will determine if the user is online or not.
http://de2.php.net/manual/en/features.connection-handling.php
you can with register_shutdown_function() und connection_aborted() your intend achieve
Use a simple JavaScript on the page
In the body tag
The callLogoff() should be replaced with the JavaScript function that calls log off.
Try it.
Environment : PHP/MySQL/Jquery
I would like to refresh the page (ex: index.php) when new data stores in to the table (ex : new_entry_table) checking for every one minute. The same page will be opened in many machines(pc-browsers) at a time all should get refresh when new data arises.
using jquery and database check i tried the following:
setInterval(function(){
$.post("new_data_check.php", function(data) {
if(data > 0){
$("#container").fadeOut('fast').load(location.reload());
}
});
return false;
},60000);
new_data_check.php: : checks
for new data in the test_db where
newdata_field=0 ; : If any new
data arises echo "1"; And update the
newdata_field=1; to stop constantly
refreshing the page. : else echo
"0"; : Just for a trigger
So the above jquery code checks new_data_check.php every 1 min for the trigger , if any trigger arises it will refresh the container.But its getting refresh only one opened session other opened sessions(in other browser or other pc) not getting refreshed.
Suggest a better way to do this. Thanks.
Your new_data_check.php file will need to keep track of all the users that are independently viewing the page.
If not, this will happen:
User 1 and User 2 are both logged into the site.
User 1 does the check 5 seconds before User 1, there is new data.
User 1 completes his request. The server clears the new_data bit.
User 2's request arrives 5 second later. Hedoes his request, but at this point, User 1 has set the new_data field to 0. His data is stale, but User 1 has effectively co-opted him.
By the looks of it, you're not supplying it with any information that would identify the user - unless the server is determining that using the IP address. If that's the case, remember that it's likely everyone behind a small network is going to have the same external ip address. So it can't tell people at an office apart.
What you could do is generate some kind of unique hash for each session and pass that off to the browser. The javascript can then provide the php script that same token.
Another solution without any sort of special bit, would be to pass along a timestamp with the request. Essentially, the client says "my latest bit of data arrived at 9:21:53 PM" The server then checks and responds "My most recent data was created before that, so you're still good. " or "I've got something that was created or modified on 9:22:53PM, you should download it."
With that method, you don't need to worry about who's who. Instead, you need to have modification/creation times on the data you're checking for staleness. You also need to make sure that the clients clock is synchronized correctly - better yet, don't trust the clients date and time information. Maybe utilize your new_data_check.php to provide the script with a timestamp it can pass along on subsequent requests.