简体   繁体   English

PHP不会将会话设置为TRUE

[英]PHP won't set session to TRUE

I'm trying to create a login system but it doesn't seem to set "Session=true". 我正在尝试创建一个登录系统,但似乎未设置“ Session = true”。 Please help I'm stuck with this for too long now. 请帮助我太久了。

Session starts Checked all my code No PHP errors and No database Errors 会话开始检查我所有的代码没有PHP错误和数据库错误

Login Page: 登录页面:

if($session->is_logged_in()) {
  redirect_to("index.php");
}


if (isset($_POST['submit'])) { // Form has been submitted.

  $user_name = trim($_POST['user_name']);
  $password = trim($_POST['password']);

  // Check database to see if username/password exist.
    $found_user = User::authenticate($user_name, $password);

  if ($found_user) {
    $session->login($found_user);
    redirect_to("index.php");
  } else {
    // username/password combo was not found in the database
    $message = "Username/password combination incorrect.";
  }

} else { // Form has not been submitted.
  $user_name = "";
  $password = "";
}

Session Class: 会议课程:

class Session {

    private $logged_in=false;
    public $user_id;

    function __construct() {
        session_start();
        $this->check_login();
    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
    }
    }

  public function is_logged_in() {
    return $this->logged_in;
  }

    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;
  }

    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;
    }
  }

}

$session = new Session();

index page: 索引页:

if (!$session->is_logged_in()) { redirect_to("login.php");

Authorize Method: 授权方式:

public static function authenticate($user_name="", $password="") {
    global $database;
    $user_name = $database->escape_value($user_name);
    $password = $database->escape_value($password);

     $sql="SELECT * FROM users WHERE user_name='{$user_name}' AND password ='{$password}' LIMIT 1";
    $result_array = self::find_by_sql($sql);
        return !empty($result_array) ? array_shift($result_array) : false;
    }

Change your is_logged_in() function to: 将您的is_logged_in()函数更改为:

public function is_logged_in() {
  $this->check_login();
  return $this->logged_in;
}

Otherwise is_logged_in() will always return false on each new request (such as redirecting between pages). 否则,is_logged_in()将始终在每个新请求(例如页面之间的重定向)上返回false。 By calling check_login() first, it will set the logged_in variable with the value (true or false, dependent on if $_SESSION['user_id'] is set. 通过首先调用check_login(),它将根据值是否设置$ _SESSION ['user_id']来将logging_in变量设置为值(是或否)。

EDIT: 编辑:

I'm sorry, I've overlooked the line in the constructor where you already call $this->check_login(); 抱歉,我忽略了构造函数中您已调用$ this-> check_login();的那一行;

Another thing is that the authenticate function returns an Array instead of an object. 另一件事是,authenticate函数返回一个Array而不是对象。 So, change the following: 因此,更改以下内容:

$this->user_id = $_SESSION['user_id'] = $user->id;

To

$this->user_id = $_SESSION['user_id'] = $user['id'];

Finally I have found the solution to this. 最后,我找到了解决方案。

Guess what? 你猜怎么了? It wasn't my codes fault but it was my servers(WAMP 2) fault. 这不是我的代码错误,而是我的服务器(WAMP 2)错误。 So, I uninstalled WAMP 2 and updated to the newer version WAMP 2.5. 因此,我卸载了WAMP 2,并更新到了较新的版本WAMP 2.5。 Solved my problem and no more getting redirect to login page! 解决了我的问题,不再需要重定向到登录页面!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM