我正在做一个关于PHP课程的任务,而且我在最后一部分做了一些工作。
该任务是创建一个简单的登录表单,并使用会话以及硬编码的用户名和密码(即无数据库)。
我遇到的问题是处理登录和会话的类。有很多代码,我不知道我可以删除什么,因此我已将它放在Pastebin上,希望没关系。
事情是单元测试是内置到课程中的,除了nr。 4,检查用户是否登录的问题。问题似乎是$ _SESSION [$ this-> loginSession]没有设置,这是我需要帮助的。
变量$ loginSession是在类的开头声明的,当用户键入正确的用户名和密码但不会发生(无错误消息)时应该设置为“isLoggedIn”。
我的班级:
<?php
class LoginHandler {
private $loginSession;
public function IsLoggedIn() {
if($_SESSION[$this->loginSession] == "isLoggedIn") {
return true;
}
else {
return false;
}
}
public function DoLogin($username, $password){
if ($username != null && $password != null){
switch ($username){
case "hello";
if ($password == "1234"){
$_SESSION[$this->loginSession] == "isLoggedIn";
return true;
}
else return false;
case "hello2";
if ($password == "12345"){
$_SESSION[$this->loginSession] == "isLoggedIn";
return true;
}
else return false;
}
}
else {
return false;
}
}
public function DoLogout(){
unset($_SESSION[$this->loginSession]);
}
public function Test() {
$this->DoLogout();
// Test 1: Check so you're not logged in.
if($this->IsLoggedIn() == true){
echo "Test 1 failed";
return false;
}
// Test 2: Check so that it's not possible to login with wrong password.
if ($this->DoLogin("hello", "4321") == true){
echo "Test 2 failed";
return false;
}
// Test 3: Check that it's possible to log in.
if ($this->DoLogin("hello", "1234") == false){
echo "Test 3 failed";
return false;
}
// Test 4: Check that you're logged in
if ($this->IsLoggedIn() == false){
echo "Test 4 failed";
return false;
}
return true;
}
}
?>
我希望它足以包括类,而不是所有的其他文件,否则我就会把他们。
你在哪里打电话给所有这些?我只在这里看到声明...请提供更多的上下文。 – antonpug