简体   繁体   English

登录后PHP无法打开网页

[英]PHP won't open webpage after logging in

I am trying to create a login system using OOP PHP code and MySQL, however it won't take me to the next page after logging in. It tells me correctly if I haven't used credentials that are in the database, it seems to always redirect me back to the login page even if I have used a user in the database. 我正在尝试使用OOP PHP代码和MySQL创建一个登录系统,但是登录后不会带我进入下一页。它正确地告诉我我是否未使用数据库中的凭据,似乎即使我在数据库中使用了用户,也总是将我重定向回登录页面。 Below are the php from the webpages and my classes, everything appears to be correct but it doesn't work and I'm not sure why. 以下是网页和我的课程中的php,所有内容似乎都是正确的,但是它不起作用,我不确定为什么。

login.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 SecLogin.php

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

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

init.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 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 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 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();

This is a job for PHP Debugging. 这是PHP调试的工作。

Start like this: go to the place in your application that you suspect is most like not working. 这样开始:在您的应用程序中怀疑最可能无法运行的位置。 If it were me, I would start here: 如果是我,我将从这里开始:

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

if the application dies there then you may have cookies turned off or are lacking session support in your php installation. 如果应用程序死在那里,那么您可能已关闭Cookie或在php安装中缺少会话支持。 If the application does not make it there, go backwards until you find where it is not behaving as you had hoped. 如果该应用程序不在此放置,请向后退,直到找到它所不希望出现的位置。

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

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