My PHP knowledge level: Novice.
Learning resources: Codecademy, TheNewBoston, Wikipedia, PHP Documentation.
What I am trying to achieve: I am trying to learn how to work with object oriented PHP since I have read that is more efficient in the long term than procedural methods. I am trying to redirect users to the login.php page if the entered name and password match with the database and relogin.php upon fail.
The problem: I can't figure out why my conditional is not working as expected. It does work, otherwise I would have had an error message.
What I tried: Worked on the below code on scratch. I did a google search on the problem, tried to fix it by checking if any semicolons or brackets are missing, and even checked if my database connection could be established. It seems there are no errors so it means the conditional is only showing true.
Current error messages: No error messages on screen or in the source code which means the code is right but not working as expected.
If you want me to comment the code please let me know.
My PHP code:
if(isset($_POST['name'])) {
$name= $_POST['name'];
$password = $_POST['password'];
$login = new login($name, $password);
}
class Login {
public function __construct ($name, $password) {
if ($this->check($name, $password)){
header("location:login.php");
}
else {
header("location:relogin.php");
}
}
public function check ($name, $password) {
$request = "SELECT `id` FROM `members` WHERE `name` LIKE '$name' AND ´password´ LIKE '$password'";
$result = mysqli_query($connection, $request);
return mysqli_num_rows($result) > 0;
}
}
public function check ($name, $password) {
$request = "SELECT `id` FROM `members` WHERE `name` LIKE '$name' AND ´password´ LIKE '$password'";
$result = mysqli_query($connection, $request);
return $result;
}
}
**HTML code**
<form action="Login.php" method="post">
<input type="text" placeholder="Name" name="name">
<input type="password" placeholder="Password" name="password">
<input type="submit" name="button" value="Login">
</form>