I am trying to check the current user logged in or not using long polling.
What is the best way to achieve this in terms of performance and bandwidth ?
I am not asking about long polling, but the best way to check user logged in or not.
I tried to use GET
request to check the login status.
var logged = $.ajax( {url:url,'type':'get'} );
logged.done(function(response, textStatus, jqXHR){ // success });
logged.fail(function((jqXHR, textStatus, errorThrown){ // sometimes it failed });
and in PHP
if( is_logged() ){ echo 1; }else{ echo 0; }
So depending up on the response i can detect the status. This works well. However i am concerning about the bandwidth usage and ended up with type HEAD
, so that it will only fetch header contents.
so i modified the php part to ..
if( !is_logged() ){
header('HTTP/1.0 401 Unauthorized');
}
in a quick view i feel that there are no problem with approach. But comparatively i am new to PHP and feels that there may be some problems with this approach.
So is this the best approach to check login status ? or what is the best way to do this ?
Thanks
Your GET request is fine and I don't see anything wrong with your approach. Not sold on the idea of returning 1/0 saving you anything tangible or returning headers/status instead of a proper response.
Instead, echo a JSON response. { success: true, response: {logged_in: true}}
I would also change your php script from displaying a 401 header and instead returning {success: true, response: {logged_in: false}}