Adam
Adam

Reputation: 20882

jquery & php + if session is dead an error is returned

I've noticed if the session is dead (on an Jquery AJAX PHP Request) the data returned is preceeded with an error message if the session is needed in the request.

How do other sites deal with this?

Similar code is shown below - eg: its using a SESSION variable in the code which doesn't exist as the session is dead.

public function internal($variable) {
    if($_SESSION['data'] == $variable) {
           echo TRUE;        
        }else{
           echo FALSE;
        }
}

Should I use isset to check if the variable exists?

Should I be coding for dead sessions?

thx

Upvotes: 1

Views: 238

Answers (3)

Manish Chauhan
Manish Chauhan

Reputation: 570

You can also do like this,

public function internal($variable) {
  if(!empty($_SESSION['data']) && $_SESSION['data'] == $variable) { // modify here
       echo TRUE;        
  }else{
       echo FALSE;
  }
}

Upvotes: 1

Padmanathan J
Padmanathan J

Reputation: 4620

Try this

public function internal($variable) {
    if($_SESSION['data']!="" && $_SESSION['data'] == $variable) { //add here
           echo TRUE;        
    }else{
           echo FALSE;
    }
}

Upvotes: 2

Code Lღver
Code Lღver

Reputation: 15603

You need to add the isset also:

public function internal($variable) {
    if(isset($_SESSION['data']) && $_SESSION['data'] == $variable) { //add here
           echo TRUE;        
    }else{
           echo FALSE;
    }
}

Upvotes: 3

Related Questions