4

in php i would do

if (isset($_COOKIE['user']) && $_COOKIE['user'] == $valueFromDb)
 echo 'logged';

But in javascript how can I check it?

Thanks

4 Answers4

8

There is no reliable way to do this without making a request to the server, since the session might have expired on the server side. In one project, I do it the following, imperfect way:

checkAuth() {
    var logged = (function() {
        var isLogged = null;
        $.ajax({
            'async': false,
            'global': false,
            'url': '/user/isLogged/',
            'success': function(resp) {
                isLogged = (resp === "1");
            }
        });
        return isLogged;
    })();
    return logged;
}

I say imperfect because it makes a synchronous request which temporarily locks up the browser. It does have an advantage in that checkAuth() can be reliably called from any part of the client-side application since it forces the browser to wait for the result before continuing execution of the JS code, but that's about it.

The alternative, asynchronous (non-locking) approach would be to do something like this:

$.ajax({
    'url': '/user/isLogged/',
    'success': function(resp) {
        if(resp === "1") {
            // do your thing here, no need
            // for async: false  
        }
    }
});
karim79
  • 339,989
  • 67
  • 413
  • 406
  • can you explain it a lil better? – dynamic Mar 27 '11 at 15:15
  • The first example makes a request to `/user/isLogged/` which simply checks if the user is logged in (via `$_SESSION`) and sends back a "1" or a "0". The `async: false` option forces the browser to wait for the request to complete - so the function will always return a boolean result. The second example implies that whatever needs to happen is done within the `success` callback, which necessitates a different architecture, but eliminates the need for a synchronous request. See http://api.jquery.com/jQuery.ajax/ – karim79 Mar 27 '11 at 15:22
  • If i put the first example in $.ready why I should use the second example? – dynamic Mar 27 '11 at 15:33
  • How does it work on the backend side? How to return true or false in php so JQ will be able to read it? Sorry for this extrem-noob question... – Hexodus Sep 08 '13 at 18:39
1

When you want to know if user is logged on a page to do some type of special action, I usually use a hidden input with a special name that is checked on js. If the user is logged you put that on a master page or something like that and then in js check if it exist to know if the user is logged.

Something like:

<input type="hidden" class="UserIsLogged" />
<script type="text/javascript">
        if ($(".UserIsLogged").length > 0) {
            //User is logged
        }
</script>
0

You are mixing up server-side processing (php) with client side processing (javascript). On the client side this information does not exist unless the server has provided it in some framework-specific way.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
  • 2
    I am not mixing. I know they are separted for this I am asking this question of course – dynamic Mar 27 '11 at 15:31
  • Well, as worded, the question appears to treat them identically, and does not explain what you are doing in enough detail to see anything else. – bmargulies Mar 27 '11 at 16:00
0

Yes. check out this question: Create, read, and erase cookies with jQuery

a plugin to make it even easier for you: http://plugins.jquery.com/project/cookie

Community
  • 1
  • 1
Mohammed Swillam
  • 9,119
  • 4
  • 36
  • 47