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
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();
?>

Link to comment
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.

 

<?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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.