1

I have a PHP page and session, i started session on top of every site, but this isn't working. I'm setting session (I'm setting class into session) with

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

and accessing it by:

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

but it doesnt work. I haven't idea why session not working.

Account class

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(){

  }
}
Account class 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" } }
15
  • 1
    have you start the session with session_start()? Commented Mar 29, 2016 at 11:56
  • 1
    Where is $_SESSION['account'] being set? Commented Mar 29, 2016 at 11:57
  • @chriz at login page, i will give you more code. Commented Mar 29, 2016 at 11:57
  • 1
    Is the base URL on each page the same? www.example.com and example.com don't share sessions. Commented Mar 29, 2016 at 11:58
  • @OrangeFlash81 yes, i use localhost Commented Mar 29, 2016 at 11:58

2 Answers 2

1

To store an object in $_SESSION you have to serialize it, then deserialize it to call the method ->isAccountTrue(), otherwise just store the result:

$_SESSION['accountValid']=$account->isAccountTrue();
...
if($_SESSION['accountValid']===true){
// do stuff
}
Sign up to request clarification or add additional context in comments.

Comments

1

Put

error_reporting(-1);
ini_set('display_errors', true);

on top of the page to enable the php errors, and you will see that you can't serialize a resource, PDO instances, PDOStatement, etc. into session.

You can't serialize Account's $acc_data, since it's an instance of PDOStatement. You cannot serialize or unserialize PDOStatement instances.

Change

$this->acc_data = $pdo->query('SELECT * FROM `'.$dbTable.'`');

to

$this->acc_data = $pdo->query('SELECT * FROM `'.$dbTable.'`')->fetchAll();

then delete your session and retry.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.