Fatal error: Call to undefined method stdClass::_setSession() on line 58
Fatal error: Call to undefined method stdClass::_setSession() on line 58
我对 PHP 比较陌生,正在构建我网站的登录部分。我试图让代码正常工作,但没有成功,我变得非常沮丧。当我提交有效的用户名和密码时,出现上述错误
Fatal Error: Call to undefined method stdClass::_setSession() on line 58
我已经将查询与其余代码分开测试,并按照从表单(即 $User= "..." & $Pass="..."
)提供变量的方式提供变量,并通过身份验证。此外,数据库中不存在的数据找不到用户(由 php 日志验证)。
我对这个错误进行了广泛的搜索,但我似乎无法找到适合这个实例的任何答案(请参阅下面的附加信息,了解我尝试过的一些有意义的事情大部头书)。我在下面包含了相关文件。预先感谢您提供的任何帮助!
附加信息:
- 我已尝试删除 $This 的 "new stdClass" 定义,这会产生以下错误:"Warning: Creating default object from empty value" 指的是遇到的第一个 $This。
- 我试图将 stdClass 定义更改为 "User" 并收到以下错误:"Fatal error: Maximum function nesting level of '100' reached, aborting!" 指的是 "new User" 行。
相关文件:
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
你让它变得更加复杂。 语法错误 if (!isset($This)) {$This = new stdClass();}
这是错误的做法。 $this with lowercase t
是当前 class.
的默认对象
您正在尝试用 stdClass 覆盖 User ($this) 的对象,因此解析器使用 _setSession()
stdClass
的成员方法而不是 User
,因此它是未定义的很明显。尝试删除错误的语法并使用 $this->_setSession()
和 replace $This to $this
以便更好地理解。
我对 PHP 比较陌生,正在构建我网站的登录部分。我试图让代码正常工作,但没有成功,我变得非常沮丧。当我提交有效的用户名和密码时,出现上述错误
Fatal Error: Call to undefined method stdClass::_setSession() on line 58
我已经将查询与其余代码分开测试,并按照从表单(即 $User= "..." & $Pass="..."
)提供变量的方式提供变量,并通过身份验证。此外,数据库中不存在的数据找不到用户(由 php 日志验证)。
我对这个错误进行了广泛的搜索,但我似乎无法找到适合这个实例的任何答案(请参阅下面的附加信息,了解我尝试过的一些有意义的事情大部头书)。我在下面包含了相关文件。预先感谢您提供的任何帮助!
附加信息:
- 我已尝试删除 $This 的 "new stdClass" 定义,这会产生以下错误:"Warning: Creating default object from empty value" 指的是遇到的第一个 $This。
- 我试图将 stdClass 定义更改为 "User" 并收到以下错误:"Fatal error: Maximum function nesting level of '100' reached, aborting!" 指的是 "new User" 行。
相关文件:
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
你让它变得更加复杂。 语法错误 if (!isset($This)) {$This = new stdClass();}
这是错误的做法。 $this with lowercase t
是当前 class.
您正在尝试用 stdClass 覆盖 User ($this) 的对象,因此解析器使用 _setSession()
stdClass
的成员方法而不是 User
,因此它是未定义的很明显。尝试删除错误的语法并使用 $this->_setSession()
和 replace $This to $this
以便更好地理解。