I have a PHP
website. I want to create a signup-login process
. I have completed the signup
process but when I log in
I need to do some verification only after that it should redirect to the page where I want it to redirect.
I have the following pages:
index.php,
login.php,
handleLogin.php, and
blog.php
I want to make the validation like this when we log in from index.php
or any other page but not from the blog
page, there will be a link named login
in the header
, and when someone wants to click from there the login
page will appear and after logged in
, it will redirect to index.php
. But when we click from the comment
on the blog
page, firstly it will redirect to the login
page and when logged in
is successful it will redirect to the same
blog
page.
Following are the HTML
codes of the login
page:
<div class="login_form">
<form action="handleLogin.php" method="POST" class="login">
<div class="mb-3">
<label for="exampleInputEmail1" class="form-label">Email address</label>
<input type="email" name="vEmail" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Password</label>
<input type="password" name="vPass" class="form-control" id="exampleInputPassword1">
<div id="emailHelp" class="form-text">We'll never share your email & password with anyone else.</div>
</div>
<button type="submit" class="btn btn-primary mb-3">Log in</button>
<div class="create_account">
<label for="exampleInputEmail1" class="form-label">Don’t have an account? <a href="signup.php"
class="signup_link">Sign up</a></label>
</div>
</form>
</div>
Following are the HTML
codes of the handleLogin
page:
<?php
// session_start ();
// $bid = $_GET['b'];
$bid = '0';
$showAlert = "false";
if($_SERVER["REQUEST_METHOD"]=="POST"){
include '../partials/_dbconnect.php';
$viewerEmail = $_POST['vEmail'];
$viewerPass = $_POST['vPass'];
$loginSql = "SELECT * FROM `viewers` WHERE `viewer_email`='$viewerEmail'";
$loginResult = mysqli_query($conn, $loginSql);
$loginNumRows = mysqli_num_rows($loginResult);
if ($loginNumRows==1) {
$showAlert = "Password not match";
$loginRow = mysqli_fetch_assoc($loginResult);
if (PASSWORD_VERIFY($viewerPass, $loginRow['viewer_pass'])) {
session_start();
$_SESSION['loggedin'] = true;
$_SESSION['vid'] = $row['viewer_id'];
$_SESSION['viewer_email'] = $row['viewer_email'];
$_SESSION['viewer_name'] = $row['viewer_name'];
// start
$showAlert = "No authority";
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin']==true) {
// header("Location: ../mains/blog?logininsuccess=true&b=$bid");
// exist();
if (($bid!=0) && ($bid!=NULL)) {
header("Location: ../mains/blog?logininsuccess=true&b=$bid");
exist();
}
else {
// header("Location: ../index");
// exist();
$showAlert = "No bid";
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin']==true) {
$showAlert = "Unknown bid";
if (($bid=0) && ($bid=NULL) && ($bid='')) {
header("Location: ../index?no_bid");
exist();
}
else {
header("Location: login?loginsuccess=false&error=$showAlert");
}
}
else {
header("Location: login?loginsuccess=false&error=$showAlert");
}
}
}
else {
header("Location: login?loginsuccess=false&error=$showAlert");
}
// end
}
}
else {
$showAlert = "Email not exist";
}
header("Location: login?loginsuccess=false&error=$showAlert");
}
?>
The result I am getting is as follows:
loginsuccess=false&error=Unknown%20bid
The result I am expected is as follows:
When $bid='0'
or $bid='NULL'
or $bid=''
or there is no $bid
then after logged in
is successful
it should redirect to index.php
, but if $bid='5'
then it should redirect to http://localhost/ows-backup-30-7-24/mains/blog?b=5
2
Answers
Could you try this
I would recommend staying away from nested
if
else
statements, it will greatly simplify your code and make things more understandable for yourself and others.In your script you have a number of issues:
1. SQL injection
The
$viewerEmail
variable is never escaped and attackers can exploit this.Use the following to ensure it is safe to use in a database query:
2. Assignment Not Comparison
The above is setting
$bid
to0
and evaluating falsely. Which means it will never execute.Use
empty($bid)
instead, as it does what you want. For example:3. Typo
The variable
$row
is never defined, it should be updated to$loginRow
.Recommended Solution
This is a cleaner version of your script that might help explain everything a bit better:
Edits