Issue
I’m making a register/login web page and the problem is when user fill in username and password and press continue, it won’t go to “index.php” but it directed to “login/db.php” which is a blank page.
I tried to change variable names such as password and username but still won't working
This is a code of login.php
<?php
session_start();
include('server.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login Page</title>
<link rel="stylesheet" href="styleSignUp.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;700&display=swap" rel="stylesheet">
</head>
<body>
<div class="SignUpPic">
<img src="img/SignIn.png" style="width:250px; margin-top: 30px;">
</div>
<div class="header" style="margin-top:310px; font-size: 20px;" >
<h2>Log In</h2>
</div>
<div class="SubHeader">
Welcome back, our champ! We missed you :-)
</div>
<form action="login_db.php" method="post">
<?php if (isset($_SESSION['error'])) : ?>
<div class="error">
<h3>
<?php
echo $_SESSION['error'];
unset($_SESSION['error']);
?>
</h3>
</div>
<?php endif ?>
<div class="input-group" style="margin: 130px 40px 0px 40px;">
<label for="username">Username</label>
<div class="input-icon">
<input type="text" name="username">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M3 22H21C21 17.5817 16.9706 14 12 14C7.02944 14 3 17.5817 3 22Z" stroke="#262626" stroke-width="1.5"stroke-linejoin="round"/>
<path d="M16.5 6.5C16.5 8.98528 14.4853 11 12 11C9.51472 11 7.5 8.98528 7.5 6.5C7.5 4.01472 9.51472 2 12 2C14.4853 2 16.5 4.01472 16.5 6.5Z" stroke="#262626" stroke-width="1.5"/>
</svg>
</div>
</div>
<div class="input-group">
<label for="password_1" style="margin: 0px 0px 0px 0px;">Password</label>
<div class="input-icon">
<input type="password" name="password_1">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M14.491 15.5H14.5M9.5 15.5H9.50897" stroke="#262626" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M4.26781 18.8447C4.49269 20.515 5.87613 21.8235 7.55966 21.9009C8.97627 21.966 10.4153 22 12 22C13.5847 22 15.0237 21.966 16.4403 21.9009C18.1239 21.8235 19.5073 20.515 19.7322 18.8447C19.879 17.7547 20 16.6376 20 15.5C20 14.3624 19.879 13.2453 19.7322 12.1553C19.5073 10.485 18.1239 9.17649 16.4403 9.09909C15.0237 9.03397 13.5847 9 12 9C10.4153 9 8.97627 9.03397 7.55966 9.09909C5.87613 9.17649 4.49269 10.485 4.26781 12.1553C4.12105 13.2453 4 14.3624 4 15.5C4 16.6376 4.12105 17.7547 4.26781 18.8447Z" stroke="#262626" stroke-width="1.5"/>
<path d="M7.5 9V6.5C7.5 4.01472 9.51472 2 12 2C14.4853 2 16.5 4.01472 16.5 6.5V9" stroke="#262626" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</div>
</div>
<div class="btn">
<button type="submit" name="login_user" class="btn" style="margin: 40px 0px 10px 0px;">Continue</button>
</div>
<div class="LogInAsk">
<p>Not yet a member? <a href="SignUp.php" style="color: #676767;">Sign Up</a> </p>
</div>
</form>
</body>
</html>
and this is a code of login_db.php
<?php
session_start();
include('server.php');
$errors = array();
if (isset($_POST['login_user'])) {
$username = mysqli_real_escape_string($conn, $_POST['username']);
$passwords = mysqli_real_escape_string($conn, $_POST['password_1']);
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
if (count($errors) == 0) {
$password = md5($passwords);
$query = "SELECT * FROM user WHERE username = '$username' AND password = '$password' ";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) == 1) {
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header("location: index.php");
} else {
array_push($errors, "Wrong username/password combination");
$_SESSION['error'] = "Wrong username or password try again!";
header("location: login.php");
}
}
}
?>
Solution
In code, provided for file login_db.php, there is variable "$password" issue as follows: The password field is retrieved, using this code:
$passwords = mysqli_real_escape_string($conn, $_POST['password_1']);
After this code, the condition is checked, using this code:
if (empty($password)) {
array_push($errors, "Password is required");
}
Please note that this condition "if (empty($password))" is always true because the password value is retrieved in variable "$passwords", not "$password". Due to this, this condition "if (count($errors) == 0)" is always false, the code, in this conditional block, regarding redirection to "index.php" page, on successful login, will not work. So, to make this code working, in file login_db.php, this code:
$passwords = mysqli_real_escape_string($conn, $_POST['password_1']);
should be replaced with this code:
$password = mysqli_real_escape_string($conn, $_POST['password_1']);
It can be checked, if it works.
Answered By - Jai
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.