i made a login form but can't get trough the last check. It has been days debugging, but i can't find my mistakes. I made a database connection class for data handling with the db, and a user class for userdata handling. The Login function resides inside the user class.
Everything worked untill one point that i can't even recollect.
Database connection class:
class DbConnect
{
public $connection;
public $result;
public function __construct()
{
$this->connectToDb();
}
public function __destruct()
{
mysqli_close($this->connection);
}
private function connectToDb(){
$this->connection = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
if(mysqli_connect_error()){
return "can not connect to database ".mysqli_connect_error();
}
}
public function query($query){
$this->result = $this->connection->query($query);
return $this->result;
}
public function fetch(){
if(!$this->result){
return "no results";
}
while($row = $this->result->fetch_assoc()){
$rows[] = $row;
}
return $rows;
}
}
The user class:
class User
{
private $id = 0;
private $name = "";
private $email = "";
private $password = "";
private $rights = 0;
private $consultant = "";
public function __construct()
{
}
public function setUser($id)
{
$db = new DbConnect();
$query = "SELECT * FROM `real1ze_users` WHERE `user_id` = {$id}";
$result = $db->query($query);
if ($result->num_rows > 0) {
$row = $db->fetch();
$this->id = $row[0]['user_id'];
$this->name = $row[0]['user_name'];
$this->email = $row[0]['user_email'];
$this->password = $row[0]['user_password'];
$this->rights = $row[0]['user_rights'];
$this->consultant = $row[0]['user_consultant'];
}
}
public function getUser()
{
$id = $this->id;
$name = $this->name;
$email = $this->email;
$password = $this->password;
$rights = $this->rights;
$consultant = $this->consultant;
return array('id'=>$id,'name'=>$name,'email'=>$email,'password'=>$password,'rights'=>$rights,'consultant'=>$consultant);
}
public function loginUser($postEmail, $postPassword)
{
$db = new DbConnect();
$email = mysqli_escape_string($db->connection,$postEmail);
$password = mysqli_escape_string($db->connection,$postPassword);
$query = "SELECT * FROM `real1ze_users` WHERE `user_email` = {$email}";
$result = $db->query($query);
if ($result->num_rows > 0) {
$row = $db->fetch();
$this->setUser($row[0]['user_id']);
$user = $this->getUser();
print_r($user);
//Check if user exists
if ($row[0]['user_email'] === $email && password_verify($password,$row[0]['user_password']))
{
//Set session variables
$_SESSION['loggedIn'] = true;
$_SESSION['id'] = $row[0]['user_id'];
return true;
}
else{
return "User does not exist.";
}
}
}
public function logoutUser()
{
session_destroy();
header('location: ../login.php');
return 'User logged out and session destroyed';
}
public function checkLoggedIn()
{
return $_SESSION['loggedIn'];
}
}
Login form:
session_start();
//Check if $_POST is set and not empty
if (isset($_POST) && !empty($_POST)){
//Initiate new user
$c_user = new User();
//Log user in with form email and password
$c_user->loginUser($_POST['user_email'],$_POST['user_password']);
//If user is logged in
if ($c_user->checkLoggedIn()){
//redirect to dashboard
header('location: index.php');
}else{
//Debug
echo "logged in session not set";
}
}
/*---------------- PHP Custom Scripts ---------
YOU CAN SET CONFIGURATION VARIABLES HERE BEFORE IT GOES TO NAV, RIBBON, ETC.
E.G. $page_title = "Custom Title" */
$page_title = "Login";
/* ---------------- END PHP Custom Scripts ------------- */
//include header
//you can add your custom css in $page_css array.
//Note: all css files are inside css/ folder
$page_css[] = "your_style.css";
$page_css[] = "lockscreen.min.css";
$no_main_header = true;
include("inc/header.php");
// Include error message script
//include("inc/message.php");
?>
<!-- ==========================CONTENT STARTS HERE ========================== -->
<!-- MAIN PANEL -->
<div id="main" role="main" style="margin-left:0px;">
<!-- MAIN CONTENT -->
<form class="lockscreen animated flipInY" method="post">
<div class="logo">
<h1 class="semi-bold"><img src="<?php echo ASSETS_URL; ?>/img/real1ze/logo_realize.png" alt="Logo"/></h1>
</div>
<div>
<img src="<?php echo ASSETS_URL; ?>/img/avatars/sunny-big.png" alt="" width="120"/>
<div>
<h1>Analytics Log in</h1>
<div class="input-group" style="margin-bottom: 10px; padding-top: 10px;">
<input class="form-control" type="text" placeholder="E-mail" name="user_email" id="email">
<div class="input-group-btn">
<button class="btn btn-primary" type="submit">
<i class="fa fa-user"></i>
</button>
</div>
</div>
<div class="input-group">
<input class="form-control" type="password" placeholder="Password" name="user_password" id="password">
<div class="input-group-btn">
<button class="btn btn-primary" type="submit">
<i class="fa fa-key"></i>
</button>
</div>
</div>
<p class="no-margin margin-top-5">
Wachtwoord vergeten? <a href="#"> Klik hier</a>
</p>
</div>
</div>
</form>
</div>
Aucun commentaire:
Enregistrer un commentaire