我对PHP相对较新,正在构建我网站的登录部分。我试图让代码工作无济于事,并且变得非常沮丧。当我提交有效的用户名&密码我得到上面的错误
致命错误:在第58行调用未定义的方法stdClass :: _ setSession()
我已经与其余代码分开测试了查询,并提供了变量,因为它们将从表单中提供(即$User= "..." & $Pass="..."
)并且auth通过。此外,数据库中不存在的数据找不到用户(由php日志验证)。
我已经对此错误进行了广泛的搜索,我似乎无法做出任何我认为适合此实例的答案(请参阅下面的其他信息,了解我尝试过的一些对我有用的事情)。我在下面列出了相关文件。提前感谢您提供的任何帮助!
其他信息:
相关文件:
class User {
public $Id;
public $Email;
public $FirstName;
public $LastName;
public $Address;
public $City;
public $State;
public $Zip;
public $Phone;
public $PhoneType;
public $IsLoggedIn = false;
function __construct() {
if (session_id() == "" || !isset($_SESSION)) {
session_start();
}
$_SESSION['IsLoggedIn'] = false;
if (!isset($This)) {$This = new stdClass();}
$This->IsLoggedIn = false;
if (isset($_SESSION['IsLoggedIn']) && $_SESSION['IsLoggedIn']== true) {
$This->_initUser();
}
} //end __construct
public function authenticate($User,$Pass) {
$mysqli = new mysqli(DBHOST,DBUSER,DBPASS,DB);
if ($mysqli->connect_errno){
error_log("Cannot connect to MySQL: " .$mysqli->connect_error);
return false;
}
$hasher= new PasswordHash(8, false);
$SafeUser = $mysqli->real_escape_string($User);
$SafeUser = strtolower($SafeUser);
$IncomingPassword = $mysqli->real_escape_string($Pass);
$Stored_hash= "*";
$Query = "SELECT * FROM donors";
$QueryUser = $mysqli->query($Query);
while ($FindUserRow = $QueryUser->fetch_array()){
if ($FindUserRow['Email']== $SafeUser){
$Stored_hash = $FindUserRow['Password'];
if ($hasher->CheckPassword($IncomingPassword, $Stored_hash)) {
$Check = 'Authentication succeeded';}
else { error_log("Passwords for {$User} don't match");
$Check = 'Authentication failed';
return false;}
if (!isset($This)) {$This = new stdClass();}
$This->Id = $FindUserRow['Id'];
$This->Email = $FindUserRow['Email'];
$This->FirstName = $FindUserRow['First_Name'];
$This->LastName = $FindUserRow['Last_Name'];
$This->Address = $FindUserRow['Street'];
$This->City = $FindUserRow['City'];
$This->Zip = $FindUserRow['Zip'];
$This->State = $FindUserRow['State'];
$This->Phone = $FindUserRow['Phone'];
$This->PhoneType = $FindUserRow['Phone_Type'];
$This->IsLoggedIn = true;
$This->_setSession();
return true;}
else{
error_log("Cannot retrieve account for {$User}");
return false;}}
} //end function authenticate
private function _setSession() {
if (session_id() == '' || !isset($_SESSION)) {
session_start();
}
$_SESSION['Id'] = $This->Id;
$_SESSION['Email'] = $This->Email;
$_SESSION['FirstName'] = $This->FirstName;
$_SESSION['LastName'] = $This->LastName;
$_SESSION['Address'] = $This->Address;
$_SESSION['City'] = $This->City;
$_SESSION['Zip'] = $This->Zip;
$_SESSION['State'] = $This->State;
$_SESSION['Phone'] = $This->Phone;
$_SESSION['PhoneType'] = $This->PhoneType;
$_SESSION['IsLoggedIn'] = $This->IsLoggedIn;
} //end function setSession
private function _initUser() {
if (session_id() == '' || !isset($_SESSION)) {
session_start();
}
$This->Id = $_SESSION['Id'];
$This->Email = $_SESSION['Email'];
$This->FirstName = $_SESSION['FirstName'];
$This->LastName = $_SESSION['LastName'];
$This->Address = $_SESSION['Address'];
$This->City = $_SESSION['City'];
$This->Zip = $_SESSION['Zip'];
$This->State = $_SESSION['State'];
$This->Phone = $_SESSION['Phone'];
$This->PhoneType = $_SESSION['PhoneType'];
$This->IsLoggedIn = $_SESSION['IsLoggedIn'];
} //end function initUser
答案 0 :(得分:0)
你让它变得更加复杂。 语法错误 if (!isset($This)) {$This = new stdClass();}
这是错误的做法。 $this with lowercase t
是当前类的默认对象。
您正在尝试使用stdClass覆盖User($ this)的对象,因此解析器正在使用_setSession()
stdClass
的成员方法而不是User
,因此它明显未定义。尝试删除错误的语法并使用$this->_setSession()
和replace $This to $this
以便更好地理解。