Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

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 ?

share|improve this question

marked as duplicate by Loz Cherone ツ May 28 at 21:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4  
You need to be doing this with AJAX. The PHP here is just rendered once (on page load). –  AMorrise May 28 at 20:55
    
sidenote, better to always use <?php also you might as well just use <?=$this->session->userdata('user_logged_in') ? '1' : '0'; ?> –  Loz Cherone ツ May 28 at 21:03
    
@Alex to be doing it with AJAX; how can i achieve that pls ? –  getapay May 29 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 at 5:39
    
The link is at the top of the page. –  AMorrise May 29 at 15:03

1 Answer 1

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?

share|improve this answer
    
how can i perform an AJAX call to the server pls? so, it will be run at interval, with the code ?? –  getapay May 29 at 5:34

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