I’ve looked at similar questions posted here as well as did a basic Google search but the best I could find is about why W3Schools is a horrible place to learn from which didn’t help much with my problem. I don’t necessarily want a solution, I’d prefer an explanation as to why this isn’t working as I have had a similar problem in the past but worked around since this is me simply debugging and testing other sections of code.
To begin with, I’ve got a pesky echo inside an if statement that isn’t printing, the funny thing is that neither the if, nor the else block appear to be executed.
My index.php file:
<!DOCTYPE html>
<html>
<head>
<title>A Title</title>
</head>
<body>
<?php
session_start();
include "scripts/Employee.php";
error_reporting(E_ALL);
$user = new Employee('bbuck', 'password');
echo "<p>I have some stuff that works here.</p>";
if (!($user->error() === false)) {
echo "<p>Authenticated!</p>";
}
else {
echo $user->error();
}
?>
</body>
</html>
The Employee constructor:
public function __construct($username, $password) {
$db = Database::getInstance();
$result = $db->authenticate($username, $password);
if ($result !== false) {
$this->id = $result['id'];
$this->fname = $result['fname'];
$this->lname = $result['lname'];
$this->lastAction = $result['last_action'];
$this->error = false;
}
else
$this->error = "Failed to authenticate the user, wrong username/password combination.";
}
I don’t think it’s really important, but just as a reference the Employee::error() function is simply return $this->error;.
And finally, the Database::authenticate() function:
public function authenticate($username, $password) {
$query = "SELECT * FROM `employee` WHERE `username` = '"
. $this->conn->escape_string($username)
. "' AND `password` = PASSWORD('"
. $this->conn->escape_string($password) . "')";
$result = $this->conn->query($query);
if (!$result)
return false;
$rowCount = $result->num_rows;
if ($rowCount == 1) {
$row = $result->fetch_assoc();
$update = "UPDATE `employee` SET `session_id` = UUID(), `session_expires` = (NOW() + INTERVAL 10 MINUTE) WHERE `id` = {$row['id']}";
$select = "SELECT `session_id`, `session_expires` FROM `employee` WHERE `id` = {$row['id']}";
$updateResult = $this->conn->query($update);
if (!$updateResult)
die("Failed to update the employee!");
$updateResult = $this->conn->query($select);
if (!$updateResult)
die("Failed to pull Session data for employee!");
$updateRow = $updateResult->fetch_assoc();
$_SESSION[SESSION_ID] = $updateRow['session_id'];
$_SESSION[SESSION_EXPIRES] = new DateTime($updateRow['session_expires']);
return $row;
}
return false;
}
Now, the echo statements within the if/else block of index.php are not printing anything, I’ve used var_dump on $user and $user->error() === false and both gave me the results that I expected to receive from them. I’ve also placed an echo before the if statement and it worked, as well as one after the statement, it also worked. I’m confused as to why they are being overlooked.
This is for debugging, I’m trying to test code, but as I said, I’m curious why this is being skipped because I’ve encountered this before.
I’m not sure if this is the full extent of your problem, but your conditions aren’t correct.
The
echo $user->error();inside theelsestatement will only ever execute if$user->error() === false. In this case you will be executingecho false;, which will give you no echoed output.To resolve this problem, use this instead (assuming
$user->error();returnsfalseif no errors exist):