Now i have a fully functional PHP codes for login and registeration i was wondering if anyone could offer improvements to the code
The code:
Config.php:
<?php
return [
'db' => [
'hostname' => 'localhost',
'username' => 'Bebo',
'password' => 'Bebo',
'database' => 'Bebo',
'port' => 3306,
],
'db_charset' => 'utf8mb4',
];
init.php:
<?php
date_default_timezone_set('Asia/Riyadh');
$error = ['Username' => '', 'Email' => '', 'Password' => ''];
$input = ['Username' => '', 'Email' => '', 'Password' => ''];
session_start();
$config = require 'Config.php';
$db = new mysqli(...$config['db']);
$db->set_charset($config['db_charset']);
Registeration.php:
<?php
require 'init.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// input validation
$Username = $input['Username'] = trim(filter_input(INPUT_POST, 'Username'));
if (mb_strlen($Username) < 3 || mb_strlen($Username) > 30) {
$error['Username'] = 'Please enter your name, it must be from 3 to 30 charaters long.';
echo "<p class='Center'> <font color=White size='50pt'>Username should be at least 3 characters long!</font> </p>";
}
$Email = $input['Email'] = trim(filter_input(INPUT_POST, 'Email'));
if (!filter_var($Email, FILTER_VALIDATE_EMAIL)) {
$error['Email'] = 'Please enter a valid email address.';
echo "<p class='Center'> <font color=White size='50pt'>Please enter a valid email address!</font> </p>";
} else {
$result = $db->execute_query("SELECT 1 FROM users WHERE email = ?", [$Email]);
if ($result->fetch_row()) {
$error['Email'] = 'Email address already taken.';
echo "<p class='Center'> <font color=White size='50pt'>Email address already taken.Please Login!</font> </p>";
}
}
$Password = $input['Password'] = filter_input(INPUT_POST, 'Password');
if (strlen($Password) < 3 || strlen($Password) > 72) {
$error['Password'] = 'Please enter password, it must be from 3 to 72 characters long.';
echo "<p class='Center'> <font color=White size='50pt'>Password should be at least 3 characters long!</font> </p>";
}
// if no errors
if (implode("", $error) === '')
{
// Password MUST be hashed using the dedicated function
$Password = password_hash($input['Password'], PASSWORD_DEFAULT);
$VIP= "NO";
$Admin = "NO";
$Creation_date = date('d-M-Y h:i:s A');
$Last_Login = date('d-M-Y h:i:s A');
$Login_Times=1;
// a parameterized query MUST be used to avoid errors and injections
$stmt = $db->prepare("INSERT INTO Users (Username, Email, Password, VIP, Admin, Creation_Date, Last_login, Login_Times) VALUES (?,?,?,?,?,?,?,?)");
$stmt->execute([
$Username,
$Email,
$Password,
$VIP,
$Admin,
$Creation_date,
$Last_Login,
$Login_Times,
]);
echo "<p class='Center'> <font color=White size='50pt'>Registeration successful</font> </p>";
$_SESSION['Email'] = $Email;
header("Location: ../home.php");
die;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="4;URL=../login.php">
<link rel="stylesheet" href="../Styles/General.css">
<link rel="stylesheet" href="../Styles/Background.css">
<link rel="icon" href ="favicon.ico" type="image/x-icon" /> <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<title>Register & Login</title>
</head>
<body class="Blue-Black">
<h1 class="Center">Please wait, you will be automatically redirected to the login & registeration page.</h1>
</body>
</html>
Validation.php:
<?php
require 'init.php';
$Email = $input['Email'] = trim(filter_input(INPUT_POST, 'Email'));
$Password = $input['Password'] = filter_input(INPUT_POST, 'Password');
$result = $db->execute_query("SELECT Email FROM Users WHERE Email = ?", [$Email]);
if ($result->fetch_row()) {
$select = "SELECT Password FROM Users WHERE Email = ?;";
$result2 = $db ->execute_query($select, [$Email]) ;
$Get_hash = $result2 ->fetch_assoc();
$hash = $Get_hash['Password'];
if (password_verify($Password, $hash)) {
$_SESSION['Email'] = $Email;
$Date = date('d-M-Y h:i A');
$Update = "UPDATE Users SET Last_Login = ?, Login_Times = Login_Times + 1 WHERE Users.Email = ?";
$stmt = $db->execute_query($Update, [$Date, $Email]);
header('location:../home.php');
}else{
echo "<p class='Center'> <font color=White size='50pt'>Invalid Password. Try again!</font> </p>";
}
}else{
echo "<p class='Center'> <font color=White size='50pt'>There is no account associated with this email address please sign up!</font> </p>";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="4;URL=../login.php">
<link rel="stylesheet" href="../Styles/General.css">
<link rel="stylesheet" href="../Styles/Background.css">
<link rel="icon" href ="favicon.ico" type="image/x-icon" /> <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<title>Register & Login</title>
</head>
<body class="Blue-Black">
<h1 class="Center">Please wait, you will be automatically redirected to the login & registeration page.</h1>
</body>
</html>
Any suggestions and improvements associated with explanation would be appreciated!!