2016-11-24 87 views
0

我想創建一個使用OOP PHP代碼和MySQL的登錄系統,但是登錄後它不會把我帶到下一頁。它告訴我如果我沒有' t使用數據庫中的憑據,即使我在數據庫中使用了用戶,它似乎總是將我重定向回登錄頁面。 下面是從網頁和我的課的PHP,一切似乎是正確的,但它不工作,我不知道爲什麼。登錄後PHP不會打開網頁

的login.php

<?php 
ob_start(); 
require_once ("init.php"); 

if ($session->loggedIn()) {redirect("SecLogin.php");} 

if (isset($_POST['submit'])) 
{ 
    $email = trim($_POST['email']); 
    $password = trim($_POST['pword']); 

    $userFound = User::verify($email, $password); 

    if ($userFound) 
    { 
     $session->login($userFound); 
     redirect("SecLogin.php"); 
    } 
    else 
    { 
     $message = "Your Email Address or Password are not recognised"; 
     echo $message; 
    } 
} 
else 
{ 
    $email = ""; 
    $password = ""; 
} 
?> 

SecLogin.php

<?php 
require_once ("init.php"); 

if(!$session->loggedIn()) {redirect("login.php");} 
?> 

的init.php

<?php 

require_once ("functions.php"); 
require_once ("constants.php"); 
require_once ("database.php"); 
require_once ("user.php"); 
require_once ("session.php"); 

session.php文件

<?php 

class Session 
{ 
private $logIn = false; 
public $userE; 

public function __construct() 
//construct function 
{ 
    session_start(); 
    $this->check(); 
} 

public function loggedIn() 
//checks whether a user is logged in 
{ 
    return $this->logIn; 
} 

public function login($user) 
//Logs the user in 
{ 
    if ($user) 
    { 
     $this->userE = $_SESSION['UserE'] = $user->email; 
     $this->logIn = true; 
    } 
} 

public function logout() 
//Logs out the user 
{ 
    unset($_SESSION['UserE']); 
    unset($this->userE); 
    $this->logIn = false; 
} 

private function check() 
//Checks whether the user exists 
{ 
    if (isset($_SESSION['UserE'])) 
    { 
     $this->userE = $_SESSION['UserE']; 
     $this->logIn = true; 
    } 
    else 
    { 
     unset($this->userE); 
     $this->logIn = false; 
    } 
} 
} 
//instantiates the class 
$session = new Session(); 

user.php的

<?php 

class User 
{ 
public $id; 
public $firstname; 
public $lastname; 
public $email; 
public $password; 

public static function findUser() 
{ 
    return self::findQuery("SELECT * FROM user"); 
} 

public static function locateUser($userMail) 
{ 
    $datasetArray = self::query("SELECT * FROM user WHERE User_Email = $userMail LIMIT 1"); 

    return !empty($datasetArray) ? array_shift($datasetArray) : false; 
} 

public static function findQuery($stmt) 
{ 
    global $database; 
    $resultSet = $database->query($stmt); 
    $instantArray = array(); 

    while ($row = mysqli_fetch_array($resultSet)) 
    { 
     $instantArray[] = self::instant($row); 
    } 

    return $instantArray; 
} 

public static function verify($email, $password) 
{ 
    global $database; 
    $email = $database->escapeString($email); 
    $password = $database->escapeString($password); 

    $sql = "SELECT * FROM user WHERE "; 
    $sql .= "User_Email = '{$email}'"; 
    $sql .= "AND User_Password ='{$password}'"; 
    $sql .= "LIMIT 1"; 

    $verifyArray = self::findQuery($sql); 

    return !empty($verifyArray) ? array_shift($verifyArray) : false; 
} 

public static function instant($record) 
{ 
    $instant = new self; 


    foreach ($record as $attr => $value) 
    { 
     if ($instant->hasAttr($attr)) 
     { 
      $instant->$attr = $value; 
     } 
    } 

    return $instant; 
} 

private function hasAttr($attr) 
{ 
    $properties = get_object_vars($this); 
    return array_key_exists($attr, $properties); 
} 
} 

database.php中

<?php 

require_once ("constants.php"); 

class Database 
{ 
public $conn; 

function __construct() 
{ 
    $this->openDbConnection(); 
} 

public function openDbConnection() 
//Opens the connection to the database 
{ 
    $this->conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD,DB_NAME); 

    if ($this->conn->connect_errno) 
    { 
     die("Database Connection Failed" . $this->conn->connect_error); 
    } 
} 

public function query($sqlStmt) 
// 
{ 
    $result = $this->conn->query($sqlStmt); 

    $this->confirmQuery($result); 

    return $result; 
} 

private function confirmQuery($result) 
{ 
    if(!$result) 
    { 
     die("Query Failed".$this->conn->error); 
    } 
} 

public function escapeString($string) 
{ 
    $escape = $this->conn->real_escape_string($string); 
    return $escape; 
} 

public function insertId() 
{ 
    return $this->conn->insert_id; 
} 
} 

$database= new Database(); 

回答

1

這是PHP調試工作。

開始像這樣:去你的應用程序中你懷疑最不喜歡的地方。如果是我,我會從這裏開始:

$this->logIn = true; 
die('Did I make it to line '.__LINE__.'?'); 

如果應用程序沒有死,那麼你可能必須關閉Cookie或缺乏在你的PHP安裝會話的支持。如果應用程序不能實現,則向後退出,直到找到不符合預期的行爲。

+0

感謝您的回答,使用這個我發現它歸結於User類開始創建的字符串需要與數據庫表字段名稱相同。另一種選擇是當用戶的記錄被分配給數組'datasetArray'時,將每個字符串分配給數組中的特定值。 –

+0

@nickfallows好的工作! – WEBjuju