Register/ Login Error

Please use this forum to discuss anything you want to.
Forum rules
Please be careful about sharing personal information. Never use any identifiable information.
Post Reply
User avatar
Can
New User
Posts: 2
Joined: 23 Aug 2018, 15:22

Register/ Login Error

Post by Can » 09 Jan 2020, 20:27

I've been having issues with the register and login functionality. I am connected to the base because I can print information in the database. The problem is I can't insert the users trying to register. Or if there is data in the database already I can't login with it.

This is the register process:

Code: Select all

<?php
session_start();
include('database_inc.php');
$email = $_POST['email'];
$password = $_POST['password'];
$username = $_POST['username'];
$email = mysqli_real_escape_string($connect,$email);
$username = mysqli_real_escape_string($connect,$username);
$safe_password = password_hash($password,PASSWORD_DEFAULT);
$unique_id = uniqid($more_entropy = TRUE);
$result = mysqli_query($connect,
    "INSERT INTO `users` 
    (`username`, `password`, `email`, `role`, `unique_id`) 
    VALUES ('$username', '$safe_password', '$email', 'Member', '$unique_id');");
$_SESSION['registered_success'] = True;
header('location:index.php');
?>
This is the login process:

Code: Select all

<?php
session_start(); 
include('database_inc.php');
$email = $_POST['email'];
$password = $_POST['password'];
$result = mysqli_query($connect,
"SELECT * FROM users WHERE email LIKE '$email';");
if (mysqli_num_rows($result) == 0) {
  $no_email = True;
  $_SESSION['error_no_email'] = True;
  header('location:login.php');
} else {
while ($row = mysqli_fetch_array($result))
{
  $password_in_databases = $row['password'];
  if (password_verify($password,$password_in_databases)) {
    $unique_id_of_logged_in_user = $row['unique_id'];
    $time_date_now = date("Y-m-d H:i:s");
    
    $result2 = mysqli_query($connect,"UPDATE users SET 
    last_logged_in = '$time_date_now', 
    logged_in_now = 1,
    session_id = '$session_id'
     WHERE unique_id = '$unique_id_of_logged_in_user';");
    $_SESSION['unique_id_of_logged_in_user'] = $unique_id_of_logged_in_user;
    header('location:index.php');
  } else {
    $_SESSION['wrong_password'] = True;
    header('location:login.php');
  }
}
}
?>
The image of the structure of my database is attached.
Attachments
Screenshot 2020-01-09 at 20.27.15.png
Screenshot 2020-01-09 at 20.27.15.png (129.08 KiB) Viewed 41 times
0 x

Tags:

Post Reply