2

In our intranet, each user can log in and cookies are created when the user is sucessfully connected

setcookie('id_user', $res['id_user'], time()+600, '/');
setcookie('email', $res['mail'], time()+600, '/');
setcookie('firstname', $res['firstname'], time()+600, '/');
setcookie('lastname', $res['name'], time()+600, '/');

They will expire after 10 min.

I have many pages where js function are using the $_COOKIE variables. I do not want to check if $_COOKIE is null or not for every function.

Is there a way to trigger a js function to check if the cookie is still available ?

I tried this

var user = '<?php echo $_COOKIE['id_user']; ?>';

function check()
{
    if(user === null)
    {
        console.log('logged');
    }
    else
    {
        console.log('disconnected');
    }
}

check();
setInterval(check, 1000);

But it did not worked. When I'm connected before accessing to this page. The console is always showing 'connected' even when I disconnect from another page. I think the cookie is still present in the page and did not expire.

And if I am not connected before accessing the page, an js error tell me

SyntaxError: unterminated string literal


var user = '<br />
Atnaize
  • 1,766
  • 5
  • 25
  • 54
  • The “unterminated string literal” is a result of PHP spitting out an error message first – check the page source code. Apart from that, rather unclear what you’re asking. – CBroe Jan 13 '16 at 13:29
  • 5
    Javascript has direct access to cookies in the browser, no need to use php (which only runs once before the page loads, hence the problem). Also, you have 2 other SO questions from the last 24hrs which you have not responded to any comments or answers. Thats not how SO works, if you want decent answers you need to engage with people – Steve Jan 13 '16 at 13:30
  • @CBroe It is because the PHP variable does not exists – Atnaize Jan 13 '16 at 13:30
  • 1
    @Raccoon: I know that – it is your job to _check_ that before you try to output it. – CBroe Jan 13 '16 at 13:38
  • 1
    @Steve: _“Javascript has direct access to cookies in the browser”_ – unless they were set by the server with `httpOnly`. – CBroe Jan 13 '16 at 13:39
  • @CBroe Ah, yes i hadn't thought about that, but i doubt its an issue here. The OP would have to clarify that though... – Steve Jan 13 '16 at 13:43

1 Answers1

2

I managed this using the following js. This code will check every sec if the cookie is still available.

function check()
{
    var user = getCookie('firstname');

    if(user == '')
    {
        console.log('disconnected');
    }
    else
    {
        console.log('connected');
    }
}

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
    }
    return "";
} 

check();
setInterval(check, 1000);
Atnaize
  • 1,766
  • 5
  • 25
  • 54
  • 1
    Why check every second? It's more practical to check it after a new request. – schellingerht Jan 13 '16 at 14:02
  • @Schellingerht What if the user do nothing until the cookie expire and then use a request ? You should check this before every function for being right. If you only <=10 functions is still ok but when you have to use this function for <1000 js function. It's way easier to use a timer. – Atnaize Jan 13 '16 at 15:15