1

I'm using the JavaScript below to check if the user session has not expired (with PHP) at intervals of 5 seconds. After logging out from another tab (same browser) or which the current session must have expired, still the code is bringing up the alert("Logged back in."); which is supposed to work for active sessions.

setInterval(function () {
    var checklogin = "<? if ($this->session->userdata('user_logged_in')) {echo '1';} else {echo '0';} ?>";
    if (checklogin == "1" ){
        alert("Logged back in.");
    } else {
        alert("you are already logged out!");
    } 
}, 5000);

Please what am I doing wrong ?

5
  • 4
    You need to be doing this with AJAX. The PHP here is just rendered once (on page load). May 28, 2014 at 20:55
  • sidenote, better to always use <?php also you might as well just use <?=$this->session->userdata('user_logged_in') ? '1' : '0'; ?> May 28, 2014 at 21:03
  • @Alex to be doing it with AJAX; how can i achieve that pls ?
    – getapay
    May 29, 2014 at 5:37
  • @Loz noted. i've adopted that. And can you give me the link to the place where the duplicate is?
    – getapay
    May 29, 2014 at 5:39
  • The link is at the top of the page. May 29, 2014 at 15:03

1 Answer 1

0

The php section of your code will not change at run time. You could, for example, perform an AJAX call to your server to retrieve session information.

EDIT:

If you're using jQuery, take a look at: jQuery Ajax

If without jQuery, take a look at: How to make an ajax call without jquery?

1
  • how can i perform an AJAX call to the server pls? so, it will be run at interval, with the code ??
    – getapay
    May 29, 2014 at 5:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.