Jump to content

Session problem using boolean...!


jmahdi

Recommended Posts

I'm trying to use a boolean 'true' or 'false' to tell the page that when false, he/she is logged out and send them to loggin page..other wise display the name which is taken from the session....i made the session stuff in a class and i have it as follows:

 

<?php
include("includes/functions.php");

class Session{
    
    public $logged_in = false; // the one i'm on about
    public $key;
    
    // $_session[$key] = $value
    public function set($key, $value){//setting session
$_SESSION[$key] = $value;
if(isset($_SESSION[$key])){
    $this->logged_in = true;
}
    }
    
    public function get($key){ //getting session
if(isset($_SESSION[$key])){
    return $_SESSION[$key];
}
else{
    return false;
}
    }
    
    
    public function confirm_logged_in(){ //check if logged in
        if(!$this->logged_in)
redirect_to("login.php"); // a tailored method
    }
    
    public function logout(){ 
session_start();
session_unset();
session_destroy();
$this->logged_in = false; 
    }
}
$session = new Session();
?>

 

unfortunately when i set the session on one page (say after login) assuming that the $logged_in variable is now turned to TRUE, but when I go to another page (e.g. Index.php)  and Get the allready set session and perform the test confirm_logged_in() it does the OPPOSITE to what I would expect as for example: redirecting me to login.php even when its "supposed to be"  $logged_in=true as set in the set function above.

 

any help would be appreciated...suggestions to change syntax or any as such...thanks

Link to comment
https://forums.phpfreaks.com/topic/249332-session-problem-using-boolean/
Share on other sites

You never set $logged_in to be anything other than false.  I'm not really sure why you are creating a class variable at all given the strategy of the session class get/set relying on session variables.  You also have no login method.

 

include("includes/functions.php");

class Session{
    
    // set session key
    public function set($key, $value) {  
$_SESSION[$key] = $value;	
    }
    
    public function get($key){ //getting session
if(isset($_SESSION[$key])){
    return $_SESSION[$key];
} else {
    return false;
}
    }
        
    public function confirm_logged_in(){ //check if logged in
        if (!$this->get('logged_in'))
  redirect_to("login.php"); // a tailored method
          exit();
    }

    public function login() {
        // Do whatever you need here to validate login -- if true
        $this->set('logged_in', true);                 
    }    

    public function logout(){ 
session_start();
session_unset();
session_destroy();	
    }
}
$session = new Session();
?>

You never set $logged_in to be anything other than false.  I'm not really sure why you are creating a class variable at all given the strategy of the session class get/set relying on session variables.  You also have no login method.

 

<?php
include("includes/functions.php");

class Session{
    
    // set session key
    public function set($key, $value) {  
$_SESSION[$key] = $value;	
    }
    
    public function get($key){ //getting session
if(isset($_SESSION[$key])){
    return $_SESSION[$key];
} else {
    return false;
}
    }
        
    public function confirm_logged_in(){ //check if logged in
        if (!$this->get('logged_in'))
  redirect_to("login.php"); // a tailored method
          exit();
    }

    public function login() {
        // Do whatever you need here to validate login -- if true
        $this->set('logged_in', true);                 
    }    

    public function logout(){ 
session_start();
session_unset();
session_destroy();	
    }
}
$session = new Session();
?>

 

thanks gizmola, I'll try altering and get back with results..thanks again :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.