我想知道为什么我们的会话在“login()”函数中没有被设置。如果我在构造函数或find()函数中设置会话,则它们已正确设置,但如果我将它们放在login()函数中,则它们不会被设置。谁能回答我为什么?由于自动加载,会在所有文件中加载会话启动(),因此这是一个问题。
的login.php:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once '/home/1/u/someplace/www/Core/init.php';
if(Input::exists()){
if(Token::validate(Input::get('token'))){
$validate = new validator;
$passed = $validate->validate($_POST, array('email' => array('required' => 'true'), 'password' => array('required' => 'true')));
if ($passed) {
$user = new users(Input::get('username'));
if($user->login(Input::get('password')));{
redirect::to("http://www.someplace.info/Includes/index.php");
}
}else{
echo "not passed";
}
}
}
?>
<html>
<header></header>
<body>
<form action="" method="post">
<input name="username" value="<?php echo Input::get('email');?>">
<input name="password" value="<?php echo Input::get('password')?>">
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>" >
<input type="submit" value="Login">
</form>
</body>
</html>
Users.php:
<?php
class users{
private $_db;
private $_data = array();
private $_sessionName;
private $_cookieName;
private $_isLoggedIn;
private $_link;
function __construct($user = null){
$this->_db = Database::getDBI();
$this->_cookieName = Config::get('cookie:cookie_name');
$this->_sessionName = Config::get('session:session_name');
if (Session::exists($this->_sessionName) && $user == null) {
$user = Session::get($this->_sessionName); //session = name[user], value = user_id
//sessions can be put here.
if($this->find($user)){
$this->_isLoggedIn = true;
} elseif(!$this->_link == "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]") {
redirect::to("http://www.ulrikbf.info/includes/login.php");
}
} else {
$this->find($user);
}
}
public function create($table,$field,$values = array()){
if (!$this->_db->insert($table,$field,$values)) {
return false;
}
}
public function find($user = null){
//sessions can be put here.
switch ($user) {
case is_numeric($user):
$this->_data = $this->_db->get('users', array('user_id','=',$user));
break;
case $user == null:
$this->_data = $this->_db->get('users', array('email','=',session::get(config::get('session::session_name'))));
break;
default:
$data = $this->_data = $this->_db->get('users', array('email','=', $user));
$datafirst = $data->first();
if ($user == $datafirst->email) {
$this->_data = $datafirst;
}
break;
return $this->_data;
}
}
public function login($user_password = null){
$password = hash::make($user_password, $this->_data->salt);
$passwordHash = $this->_data->password;
if ($passwordHash == $password ) {
$hashSession = hash::unique();
session::put('hash', $hashSession); //not working
session::put($this->_sessionName,$this->data()->user_id); //not working.
$this->_db->insert('sessions','user_id, hash', array(
$this->_data->user_id, $hashSession));
return true;
}
return false;
}
public function data(){
return $this->_data;
}
public function isLoggedIn(){
return $this->_isLoggedIn;
}
}
的index.php:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once '/home/1/u/someplace/www/Core/init.php';
$user = new users();
print_r($_SESSION);
if($user->isLoggedIn()){
echo "Great";
} else {
echo "Not so great";
}
?>
的init.php:
session_start();
//Standard PHP Library(spl)..
spl_autoload_register(function($class) {
require_once '/home/1/u/someplace/www/Classes/' . $class . '.php';
});
session.php文件:
<?php
class session {
public static function put($name,$value){
return $_SESSION[$name] = $value;
}
}
答案 0 :(得分:0)
您需要在每个文件中启动会话