PHP会话未设置

卡珀

我有一个PHP页面和一个会话,我在每个站点的顶部都开始了会话,但这是行不通的。我正在设置会话(我将类设置为会话)

}else if(isset($_POST['username']) and isset($_POST['password'])){
  $account = new Account;
  $password = hash('sha256', $_POST['password']);
  $account->setTheAccount($_POST['username'],$password);
  $acc_data = $account->getDatabaseAccounts('root','','schoolpage','localhost','accounts');
  $acc_status = $account->isAccountTrue();
  if ($acc_status==true){
    $_SESSION['account'] = $account;
    echo 'true';
    header('Location:panel.php');
  }else{
    echo 'false';
  }
}

并通过以下方式访问它:

if (isset($_SESSION['account'])){
      $account = $_SESSION['account'];
        if ($account->isAccountTrue() == true){
          echo 'XD';
        }
      }

但它不起作用。我不知道为什么会话无法正常工作。

帐户类别

class Account{
  private $acc_data,$username,$password;
  function setTheAccount($usernamei, $passwordi){
    $this->username = $usernamei;
    $this->password = $passwordi;
  }
  function getDatabaseAccounts($dbUser,$dbPassword,$dbName,$dbHost, $dbTable){
    $pdo = new PDO('mysql:host='.$dbHost.';dbname='.$dbName, $dbUser, $dbPassword);
    $this->acc_data = $pdo->query('SELECT * FROM `'.$dbTable.'`');
  }
  function isAccountTrue(){
    $acc_status=false;
    foreach ($this->acc_data as $i) {
      if ($i['username'] == $this->username and $i['password'] == $this->password){
        $acc_status = true;
      }
    }
    return $acc_status;
  }
  function isUserLogged(){

  }
}
帐户类var dump
truearray(2) { ["login"]=> bool(true) ["account"]=> object(Account)#1 (3) { ["acc_data":"Account":private]=> object(PDOStatement)#3 (1) { ["queryString"]=> string(24) "SELECT * FROM `accounts`" } ["username":"Account":private]=> string(5) "admin" ["password":"Account":private]=> string(64) "4813494d137e1631bba301d5acab6e7bb7aa74ce1185d456565ef51d737677b2" } }
阿利·希克斯(Arleigh Hix)

要将对象存储在$ _SESSION中,您必须对其进行序列化,然后反序列化以调用方法-> isAccountTrue(),否则只需存储结果即可:

$_SESSION['accountValid']=$account->isAccountTrue();
...
if($_SESSION['accountValid']===true){
// do stuff
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章