$ _SESSION变量不会取值

时间:2015-12-01 01:19:09

标签: php session

我正在构建一个Login函数,由于某种原因我的$ _SESSION变量没有得到任何值,如果他们这样做,我会尝试echo他们,但他们不会显示。

这是我试图设置它们的地方:

function login()
{
$db=new Database();
session_start();
if(isset($_POST['login'])){
$username=$_POST['username'];
$password=md5($_POST['password']);

$query=("SELECT * FROM user
        WHERE username = '$username'
        AND password = '$password'");
$row=$db->select($query);
if(mysqli_num_rows($row)==1)
{
  $_SESSION['is_logged_in'] = true;
  $_SESSION['id'] = $row['id'];
  $_SESSION['username'] = $row['username'];
  $_SESSION['name'] = $row['name'];
  header('Location: index.php');
 }
}
}

在我的index.php我只有session_start();echo $_SESSION['username'];

可能导致这些错误的原因是什么?

我使用它们错了吗?我使用$query变量错了吗?

任何帮助都是非常苛刻的

编辑: 这是我的config.php

<?php
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','');
define('DB_NAME','PHPWizard');
?>

这是我的Database.php

<?php
class Database{
public $host = DB_HOST;
public $username = DB_USER;
public $password = DB_PASS;
public $db_name = DB_NAME;

public $link;
public $error;

/*
 * Class Constructor
 */
public function __construct(){
    //Call Connect Function
    $this->connect();
}

/*
 * Connector
 */
 private function connect(){
    $this->link = new mysqli($this->host, $this->username, $this->password, $this->db_name);

    if(!$this->link){
        $this->error = "Connection Failed: ".$this->link->connect_error;
        return false;
    }
 }

 /*
  * Select
  */
  public function select($query){
    $result = $this->link->query($query) or die($this->link->error.__LINE__);
    if($result->num_rows > 0){
        return $result;
    } else {
        return false;
    }
  }

  /*
   * Insert
   */
   public function insert($query){
        $insert_row = $this->link->query($query) or die($this->link->error.__LINE__);

        //Validate Insert
        if($insert_row){
            header("Location: index.php?msg=".urlencode('Record Added'));
            exit();
        } else {
            die('Error : ('. $this->link->errno .') '. $this->link->error);
        }
   }

   /*
   * Update
   */
   public function update($query){
        $update_row = $this->link->query($query) or die($this->link->error.__LINE__);

        //Validate Insert
        if($update_row){
            header("Location: index.php?msg=".urlencode('Record Updated'));
            exit();
        } else {
            die('Error : ('. $this->link->errno .') '. $this->link->error);
        }
   }

    /*
   * Delete
   */
   public function delete($query){
        $delete_row = $this->link->query($query) or die($this->link->error.__LINE__);

        //Validate Insert
        if($delete_row){
            header("Location: index.php?msg=".urlencode('Record Deleted'));
            exit();
        } else {
            die('Error : ('. $this->link->errno .') '. $this->link->error);
        }
   }

}

我的联系可能是变量没有得到任何值的原因吗?

1 个答案:

答案 0 :(得分:1)

在将$ row用作数组之前,您必须获取数据:

此:

$row = $db->select($query);
$_SESSION['id'] = $row['id'];

应该是:

$result = $db->select($query);
if ($result) {
    $row = $result->fetch_assoc();
    $_SESSION['id'] = $row['id'];
}

更多信息:http://php.net/manual/es/mysqli-result.fetch-assoc.php