简体   繁体   中英

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. 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.

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

This is a job for PHP Debugging.

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. If the application does not make it there, go backwards until you find where it is not behaving as you had hoped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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