Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aleksov's avatar

Check is user loged in with javascript

Maybe is stupid question but is possible to check is user loged in with javascript...

How to check is user loged in with javascript? I try:

$(document).ready(function() {

$('#facebook').click(function(){
    window.open(this.href, "myWindowName", "width=800, height=600");
    return false;
    setInterval(function() {
        @if {{Auth::check()}};

alert('loged') @else alert('not loged') @endif

    }, 5000);
});

}); offcource this dont work... SO I need when I click on #facebook to check every 5 seconds is user logedin...

0 likes
12 replies
d3xt3r's avatar

How do you expect the user to login ? Why constantly checking if he is logged in ? If you are using sessions/cookie, you could just refresh the page at fixed interval, if he is logged in from different tab in same browser, this would pick that up.

aleksov's avatar

hm, no. I need to check is user loged via facebook (different window) and if yes to redirect them...

d3xt3r's avatar

i don't understand the part where you say different window.

d3xt3r's avatar

As i said you could just reload the page or check the same with ajax. Ofcourse, this would work only if same session id is set for both the tabs (highly likely scenario unless something is screwed up).

aleksov's avatar

Yes, but my app is into iframe at other website, so I try first to open facebooklogin in new smaller window and after that to chech on iframe window when is user loged to redirect them... so iframe is my problme

jlrdw's avatar

Can you add a submit button to your iframe that the user can click once logged in. If app is running in iframe authentication should also work in the iframe, just like a browser window.

jekinney's avatar

In the auth.php file under conf you can allow access the session data for use with JavaScript. Then with JavaScript you can access session data.

Qlic's avatar

Using javascript is nice and all, but you should not rely on that, and always use php to double check wether or not the person is actually logged in. Since all Auth::check() does is return a true or false, that should be quite easy to manipulate browserside.

aleksov's avatar

I do this: $(document).ready(function() { var myWindow; $('#facebook').click(function(){ myWindow = window.open(this.href, "myWindowName", "width=400, height=300"); return false; });

$('.facebook').click(function(){

setInterval(function() { $.ajax({ url: 'http://roomtobid.com/isloged',

success: function(data) { if (data === 'yes'){ console.log('loged'); myWindow.close(); }else { console.log('not loged'); } }, type: 'GET' });

    }, 2000);
});

});

but does it problem for server when I check every 2 seconds is user loged ?

Please or to participate in this conversation.