NMaduro
NMaduro

Reputation: 31

Fatal error: Call to undefined method stdClass::_setSession() on line 58

I am relatively new to PHP and am building the log-in portion of my website. I have attempted to get the code to work for to no avail and have become extremely frusturated. When I submit a valid username & password I get the error above

Fatal Error: Call to undefined method stdClass::_setSession() on line 58

I have tested the query separately from the rest of the code and fed the variables as they would be fed from the form (i.e. $User= "..." & $Pass="...") and the auth passes. Also, data not present in the database does not find a user (verified by the php logs).

I have done extensive searches on this error and I can't seem to make any of the answers I've found fit this instance (See additional info below for a few of the things I've tried that made sense to me). I have included the relevant file below. Thank you in advance for any help you provide!

Additional Info:

  1. I have tried to remove the "new stdClass" definition for $This which creates the following error: "Warning: Creating default object from empty value" referring to the first $This encountered.
  2. I have tried to change the stdClass definition to "User" and receive the following error: "Fatal error: Maximum function nesting level of '100' reached, aborting!" referring to the "new User" line.

Relevant File:

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

Upvotes: 0

Views: 2096

Answers (1)

Ashwani Goyal
Ashwani Goyal

Reputation: 616

You have made it extra complicated. bad syntax if (!isset($This)) {$This = new stdClass();} this one is wrong practice. $this with lowercase t is something i.e. default object of the current class.

You are trying to overwrite object of User ($this) with stdClass, so parser is taking _setSession() a member method of stdClass instead of User and hence it is undefined by obvious. Try removing the bad syntax and use $this->_setSession() and replace $This to $this for better understanding.

Upvotes: 0

Related Questions