여기에 게시 된 유사한 질문과 기본 Google 검색을 살펴 보았지만 가장 좋은 것은 W3Schools가 왜 끔찍한 지에 대한 것입니다 내 문제에 많은 도움이되지 않은 것을 배울 곳. 필자는 솔루션을 원한다고 생각하지는 않는다. 이전에 비슷한 문제가 있었지만 지금까지는 코드의 다른 섹션을 디버깅하고 테스트했기 때문에 이것이 작동하지 않는 이유에 대한 설명을 선호한다.if/else 블록 내의이 메아리가 스크립트 끝에서 작동하지 않습니다.
처음에는 인쇄되지 않는 if 문 안에 성가신 에코가 있습니다. 재미있는 점은 if 또는 else 블록이 실행되지 않는 것입니다.
내 index.php
파일 :
<!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>
Employee
생성자 :
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.";
}
나는 그것이 정말 중요하다고 생각하지 않지만, 단지 기준으로 Employee::error()
기능은 단순히 return $this->error;
입니다.
그리고 마지막으로, Database::authenticate()
기능 : 모두가 나에게 결과를 제공
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;
}
이제 index.php
의 if/else
블록 내에서 에코 문 아무것도 인쇄되지 않습니다, 나는 $user
및 $user->error() === false
에 var_dump
사용했습니다 그 나는 그들로부터받을 것으로 예상했다. 또한 if 문 앞에 echo
을 두었습니다. 문 뒤에도 하나뿐 아니라 작동했습니다. 나는 그들이 왜 간과되고 있는지에 관해 혼란 스럽다.
이것은 디버깅 용이며, 코드를 테스트하려고하는데, 내가 말했듯이 왜 이런 일이 발생했는지 궁금합니다.
오류 로그에서 PHP 오류가 없는지 확인 했습니까? PHP 블록 이후의 내용이 출력됩니까? (예 :) – Corbin
'else'에 들어갈 수 있으며'$ user-> error()'가 빈 문자열이나 null 값을 반환하므로 echo가 없습니까? – babonk
또한 html 인쇄 후 session_start를 사용하는 것으로 나타났습니다. session_start는 쿠키를 클라이언트에 보내야합니다. HTTP 헤더를 보내야합니다.데이터가 헤더가 아닌 다른 세션 (예 : 세션 시작 호출 전에 HTML)을 통과하면 더 이상 헤더를 보낼 수 없습니다. 세션이 시작되지 않은 것 같습니다. 아니면 출력 버퍼링을 사용하고 있습니까? (만약 그렇다면, 출력하기 전에 session_start를 넣는 것이 더 나은 습관입니다.) – Corbin