Chris Hick
01-17-2012, 07:51 PM
I've created the following session class for my own personal framework. I decided that the framework should automatically check if a user is logged in and then do whatever the programmer wants, in my case I'm check to see if they have chosen a character. If they aren't logged in they need to be sent to the back to the login page. But when I do this, the browser pulls and error and says there are too many redirects. I don't understand how.
<?php
require_once("functions.php");
class Session {
private $logged_in = false;
private $selected_char = false;
public $user_id;
public $char_id;
public $message;
function __construct() {
session_start();
$this->check_message();
$this->check_login();
if($this->logged_in) {
// actions to take right away if user is logged in
$this->check_char();
if($this->selected_char) {
redirect_to("profile.php");
} else {
redirect_to("select_character.php");
}
} else {
// actions to take right away if user is not logged in
redirect_to("login.php");
}
}
public function is_logged_in() {
return $this->logged_in;
}
public function has_selected_char() {
return $this->selected_char;
}
public function login($user) {
// database should find user based on username/ password
if($user) {
$this->user_id = $_SESSION['user_id'] = $user->id;
$this->logged_in = true;
}
}
public function logout() {
unset($_SESSION['user_id']);
unset($this->user_id);
$this->logged_in = false;
}
public function message($msg="") {
if(!empty($msg)) {
// then this is "set message"
// make sure you understand why $this->message=$msg wouldn't work
$_SESSION['message'] = $msg;
} else {
// then this is "get message"
return $this->message;
}
}
private function check_login() {
if (isset($_SESSION['user_id'])) {
$this->user_id = $_SESSION['user_id'];
$this->logged_in = true;
} else {
unset($this->user_id);
$this->logged_in = false;
}
}
private function check_char() {
if (isset($_SESSION['char_id'])) {
$this->char_id = $_SESSION['char_id'];
$this->selected_char = true;
} else {
unset($this->char_id);
$this->selected_char = false;
}
}
private function check_message() {
// Is there a message stored in the session
if(isset($_SESSION['message'])) {
// Add it as an attribute and erase the stored version
$this->message = $_SESSION['message'];
unset($_SESSION['message']);
} else {
$this->message = "";
}
}
}
$session = new Session();
$message = $session->message();
?>
<?php
require_once("functions.php");
class Session {
private $logged_in = false;
private $selected_char = false;
public $user_id;
public $char_id;
public $message;
function __construct() {
session_start();
$this->check_message();
$this->check_login();
if($this->logged_in) {
// actions to take right away if user is logged in
$this->check_char();
if($this->selected_char) {
redirect_to("profile.php");
} else {
redirect_to("select_character.php");
}
} else {
// actions to take right away if user is not logged in
redirect_to("login.php");
}
}
public function is_logged_in() {
return $this->logged_in;
}
public function has_selected_char() {
return $this->selected_char;
}
public function login($user) {
// database should find user based on username/ password
if($user) {
$this->user_id = $_SESSION['user_id'] = $user->id;
$this->logged_in = true;
}
}
public function logout() {
unset($_SESSION['user_id']);
unset($this->user_id);
$this->logged_in = false;
}
public function message($msg="") {
if(!empty($msg)) {
// then this is "set message"
// make sure you understand why $this->message=$msg wouldn't work
$_SESSION['message'] = $msg;
} else {
// then this is "get message"
return $this->message;
}
}
private function check_login() {
if (isset($_SESSION['user_id'])) {
$this->user_id = $_SESSION['user_id'];
$this->logged_in = true;
} else {
unset($this->user_id);
$this->logged_in = false;
}
}
private function check_char() {
if (isset($_SESSION['char_id'])) {
$this->char_id = $_SESSION['char_id'];
$this->selected_char = true;
} else {
unset($this->char_id);
$this->selected_char = false;
}
}
private function check_message() {
// Is there a message stored in the session
if(isset($_SESSION['message'])) {
// Add it as an attribute and erase the stored version
$this->message = $_SESSION['message'];
unset($_SESSION['message']);
} else {
$this->message = "";
}
}
}
$session = new Session();
$message = $session->message();
?>