Question: there is a way to check wordpress cookie **wordpress\_logged\_in** using jquery?

I'm using cloudflare full caching, so there is no way to check if user is logged or not than this cookie, I tried to read it using jquery but seems protected and not visible, I can access it only using PHP.

Here is how I can read it right now :

```
$logged_in = 'no'; 
if (count($_COOKIE)) {
    foreach ($_COOKIE as $key => $val) {
        if (preg_match("/wordpress_logged_in/i", $key)) {
            $logged_in = 'yes';
        }       
    }
}

```

Maybe there is a possiblity to check it simply by jquery, but I didn't find how.

Thanks for helping

Answer: 
You can't access to wordpress cookies named `wordpress_logged_in` using `js/jquery` just because it's flagged as [`HttpOnly`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#Secure_and_HttpOnly_cookies):

> 
> A secure cookie is only sent to the server with a encrypted request
>  over the HTTPS protocol... To prevent cross-site scripting (XSS)
>  attacks, HttpOnly cookies are inaccessible to JavaScript's
>  Document.cookie API; they are only sent to the server.
> 
> 
> 

But since 3.0 wordpress add one more cookie named [wp-settings-{time}-[UID]](https://codex.wordpress.org/WordPress_Cookies#WordPress_.3E_3.0) when user log in:

> 
> WordPress also sets a few wp-settings-{time}-[UID] cookies. The number
>  on the end is your individual user ID from the users database table.
>  This is used to customize your view of admin interface, and possibly
>  also the main site interface.
> 
> 
> 

And you can get this cookie using `js/jquery`. So, you can even get user ID from cookies.

Here is working js function for getting cookies and checking, if user is logged in:

```
function getLoggedInCookie() {
    var cookie = document.cookie.indexOf('wp-settings-time') !== -1;

    if(cookie){
        alert('Logged in');
    }else{
        alert('Not User');
    }
}
getLoggedInCookie();

```

jQuery solution will be to include [Cookie plugin](http://plugins.jquery.com/cookie/) to your wordpress theme/plugin and try to use it( may need in some modifications ).

**NOTE:**

Provided code will work with login forms, which use standart WordPress functions and endpoints, such as `http://{website-name}/wp-admin/` or `http://{website-name}/wp-login.php`. Third party plugins could not set cookies named `wp-settings-{time}-[UID]`.
As mentioned in another answer, `wp_localize_script()` is a useful function. I use this, specifically:

```
wp_localize_script(
    'site-js',
    'siteVars',
    array(
        'ajaxurl' => admin_url( 'admin-ajax.php' ),
        'isMobile' => (wp_is_mobile() ? true : false),
        'isHome' => (is_home() || is_front_page() ? true : false),
        'isSingle' => (is_single() ? true : false),
        'isLogged' => (is_user_logged_in() ? true : false),
    )
);

```

Then you can access it in your site script like `siteVars.isLogged` which will return the correct boolean value. Place this in your `functions.php` file. I prefer to place this in a function and execute it right after you enqueue your scripts.

You *might* be able to use a script such as [js-cookie](https://github.com/js-cookie/js-cookie) -- I've used that script to set/read cookie data through WP several times. Though the first method with `wp_localize_script()` is probably more reliable. If the cookie data is protected then the JS solution might not work. The PHP solution is a for-sure thing. You could even extend it to get the users role and user information. 

<https://developer.wordpress.org/reference/functions/is_user_logged_in/>

<https://codex.wordpress.org/Function_Reference/wp_localize_script>