Создал Класс Сессию
<?php
namespace Modules;
class Session
{
private $logged_in = false;
public $user_id;
//Session start and check logged in
public function __construct()
{
session_start();
$this->checkLogin();
if ($this->logged_in) {
// actions to take right away if user is logged in
} else {
// actions to take right away if user is not logged in
}
}
//session destroy
public function __destruct()
{
session_destroy();
}
// Check if User log in
public function isLoggedIn()
{
return $this->logged_in;
}//true
//check if some one log in from page
public function login($user)//true
{
// database should find user based on username/password
if ($user) {
$this->user_id = $_SESSION['user_id']=$user->id ;
$this->logged_in = true;
}
}
//unset out user_id
public function logout()//true
{
unset($_SESSION['user_id']);
unset($this->user_id);
$this->logged_in = false;
}
// check for user_id accept
private function checkLogin()
{
if (isset($_SESSION['user_id'])) {
$this->user_id = $_SESSION['user_id'];
$this->logged_in = true;
} else {
unset($this->user_id);
$this->logged_in = false;
}
}//true
}
Страница логина
<?php
require_once('../../vendor/autoload.php');
require_once('../../includes/functions.php');
$session = new \Modules\Session();
$database = new \Modules\MySqlDatabase();
$object = new \Modules\User();
if ($session->isLoggedIn()) {
redirect_to("index.php");
}
// Remember to give your form's submit tag a name="submit" attribute!
if (isset($_POST['submit'])) { // Form has been submitted.
$username = cleanStr($_POST['username']);
$password = cleanStr($_POST['password']);
// Check database to see if username/password exist.
$found_user = $object->authenticate($username, $password);
if ($found_user) {
var_dump($session->login($found_user));
//redirect_to("index.php");
} else {
// username/password combo was not found in the database
$message = "Username password are incorrect.";
}
} else { // Form has not been submitted.
$username = '';
$password = "";
}
Собственно , с бд все нормально проходит. Перехожу на страницу но выводит NULL:
var_dump($session->login($found_user));
И если я не закоментирую обратный редирект с закрытой страницы, меня вечно редиректит назад на логин.
Стр. куда иду после ввода логина и после чего реконектит назад:
require_once ("../../vendor/autoload.php");
require_once ("../../includes/functions.php");
$session = new \Modules\Session();
var_dump($session->isLoggedIn());
if (!$session->isLoggedIn()) { redirect_to("login.php"); }
?>