0
The following code allows the user to log in and direct to a particular page, or log in as an administrator and direct to another page. The user login is working, however when logging out appears undefinied variable on line 28 and 29. And if I try to log in as an administrator and not redirect the page is empty.
<?php
require('config.php');
if (isset($_POST['email'])) {
$stmt = $conn->prepare("SELECT password FROM registo WHERE email=?");
$stmt->bind_param("s", $email);
$email = $_POST['email'];
$password = $_POST['password'];
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($pass);
$stmt->fetch();
if($stmt->num_rows > 0) {
if(password_verify($password,$pass)) {
$_SESSION['email'] = $email;
$_SESSION['user'] = true;
header("Location: home.php");
} else {
echo "<div class='form'>
<h3>Email/password is incorrect.</h3> <br>
Click here to <a href='memberarea.html'>Login</a>
</div>";
}
}
} else {
$ustmt = $conn->prepare("SELECT password FROM Admin WHERE email=?");
$ustmt->bind_param("s", $email);
$email = $_POST['email'];//linha 28
$password = $_POST['password']; //linha 29
$ustmt->execute();
$ustmt->store_result();
$ustmt->bind_result($pass);
$ustmt->fetch();
if($ustmt->num_rows > 0) echo "hello"; //linha 36 {
if(password_verify($password,$pass)) {
$_SESSION['email'] = $email;
$_SESSION['Admin'] = true;
header("Location: adminarea.html");
} else { //linha 47
echo "<div class='form'>
<h3>Email/password is incorrect.</h3> <br>
Click here to <a href='memberarea.html'>Login</a>
</div>";
}
} else {
echo "<div class='form'>
<h3>You are now logged out!.</h3> <br>
Click here to <a href='home.php'>Home</a>
</div>";
}
}
?>
you need to add the form code as well.
– Gabriel Heming
This code is very strange. You check if the variable
email
exists and executes one code if it does not execute another, but the two blocks need e-mail.– Wendel Rodrigues
There is serious error of logic there. How you want to use
$_POST['email']
if you are already saying that it does not exist when falling into theELSE
of conditionif (isset($_POST['email']))
.– Don't Panic
what I want is if a user email is inserted to add to a certain page, if it is an administrator email to add to a different page. any suggested resolution?
– Diana Madeira
I have been doing echo on each line. and on line 36 I put and complained of Else on line 47. pf edited the code! but I’m still not solving.
– Diana Madeira
Yes, because it doesn’t make any sense to do what you did. The
else
cannot exist without theif
.– Woss
(1) Are the e-mail and password of users and administrators informed in the same form? (2) How do you differentiate whether it is a user or admin login attempt? Will you first try logging in as a user and if you can’t, try as an admin? (3) Considering two different tables, is it possible that there are two equal user and admin registrations? (4) If yes, the account must be considered user or admin?
– Woss
the form is the same, as I said before, before assigning bind Parameter was working perfectly. when logging in when I click submit detects whether it is user email or admin email, ie the code tries to see if it is user email if yes all right goes to x page. if it detects q is Admin email goes to another page! there are two different tables there is no chance that there are two equal entries.
– Diana Madeira
Are you making this guarantee that there will not be equal records in code? By the way, first try to implement the Wendel solution. It makes a little more sense, by merging the two tables into one. Greatly simplifies the code.
– Woss