Whiskas
19th November 2016, 20:12
Maybe someone smart can help figure it out for me.
I am making some website with accounts etc. On the index page, there is a form to login. After clicking Sign in button, this is executed:
$userService = new UserService($mysqli, $_POST['identity_number'], $_POST['password']);
if ($userService->login()) {
$_SESSION[user] = $userService->getUser();
header("Location: home.php");
}
I am creating object of UserService class, which looks like this:
class UserService
{
protected $_id;
protected $_passwd;
public $_db;
public $_user;
public function __construct(mysqli $db, $id, $passwd)
{
$this->_db = $db;
$this->_id = $id;
$this->_passwd = $passwd;
}
public function login()
{
$user = $this->_checkCredentials();
if ($user) {
$this->_user = new User($this->_db, $user->iduzytkownika, $user->imie, $user->nazwisko, $user->idgrupy, $user->idpodgrupy);
print_r($this->_db);
print_r($this->_user);
return $user;
}
return false;
}
protected function _checkCredentials()
{
$this->_id = $this->_db->real_escape_string($this->_id);
$this->_passwd = $this->_db->real_escape_string($this->_passwd);
$result = $this->_db->query("SELECT * FROM uzytkownicy WHERE iduzytkownika = $this->_id AND passwd = '$this->_passwd'");
if($result)
return $result->fetch_object();
return false;
}
public function getUser()
{
return $this->_user;
}
}
Everything is working fine, if the id and password is properly, new User class object is created, which looks like this:
class User
{
public $_id;
public $_name;
public $_lastname;
public $_groupID;
public $_subgroupID;
public $_db;
public function __construct(mysqli $db, $id, $name, $lastname, $groupID, $subgroupID)
{
$this->_db = $db;
$this->_id = $id;
$this->_name = $name;
$this->_lastname = $lastname;
$this->_groupID = $groupID;
$this->_subgroupID = $subgroupID;
}
public function __toString()
{
return $this->_name . " " . $this->_lastname;
}
public function getUserGroups()
{
print_r($this->_db); // For the debug purposes
}
}
And there comes the problem with $db mysqli object, which despites having proper structure (of mysqli class), all the values are empty. print_r from getUserGroups() returns:
mysqli Object
(
[affected_rows] =>
[client_info] =>
[client_version] => 50012
[connect_errno] => 0
[connect_error] =>
[errno] =>
[error] =>
[error_list] =>
[field_count] =>
[host_info] =>
[info] =>
[insert_id] =>
[server_info] =>
[server_version] =>
[stat] =>
[sqlstate] =>
[protocol_version] =>
[thread_id] =>
[warning_count] =>
)
I would like to note that mysqli object in UserService ain't empty.
Anyone got a clue to fix it or even properly name that problem. Cause I have trouble finding similar posts to mine.
Thanks in advance,
Whisky
I am making some website with accounts etc. On the index page, there is a form to login. After clicking Sign in button, this is executed:
$userService = new UserService($mysqli, $_POST['identity_number'], $_POST['password']);
if ($userService->login()) {
$_SESSION[user] = $userService->getUser();
header("Location: home.php");
}
I am creating object of UserService class, which looks like this:
class UserService
{
protected $_id;
protected $_passwd;
public $_db;
public $_user;
public function __construct(mysqli $db, $id, $passwd)
{
$this->_db = $db;
$this->_id = $id;
$this->_passwd = $passwd;
}
public function login()
{
$user = $this->_checkCredentials();
if ($user) {
$this->_user = new User($this->_db, $user->iduzytkownika, $user->imie, $user->nazwisko, $user->idgrupy, $user->idpodgrupy);
print_r($this->_db);
print_r($this->_user);
return $user;
}
return false;
}
protected function _checkCredentials()
{
$this->_id = $this->_db->real_escape_string($this->_id);
$this->_passwd = $this->_db->real_escape_string($this->_passwd);
$result = $this->_db->query("SELECT * FROM uzytkownicy WHERE iduzytkownika = $this->_id AND passwd = '$this->_passwd'");
if($result)
return $result->fetch_object();
return false;
}
public function getUser()
{
return $this->_user;
}
}
Everything is working fine, if the id and password is properly, new User class object is created, which looks like this:
class User
{
public $_id;
public $_name;
public $_lastname;
public $_groupID;
public $_subgroupID;
public $_db;
public function __construct(mysqli $db, $id, $name, $lastname, $groupID, $subgroupID)
{
$this->_db = $db;
$this->_id = $id;
$this->_name = $name;
$this->_lastname = $lastname;
$this->_groupID = $groupID;
$this->_subgroupID = $subgroupID;
}
public function __toString()
{
return $this->_name . " " . $this->_lastname;
}
public function getUserGroups()
{
print_r($this->_db); // For the debug purposes
}
}
And there comes the problem with $db mysqli object, which despites having proper structure (of mysqli class), all the values are empty. print_r from getUserGroups() returns:
mysqli Object
(
[affected_rows] =>
[client_info] =>
[client_version] => 50012
[connect_errno] => 0
[connect_error] =>
[errno] =>
[error] =>
[error_list] =>
[field_count] =>
[host_info] =>
[info] =>
[insert_id] =>
[server_info] =>
[server_version] =>
[stat] =>
[sqlstate] =>
[protocol_version] =>
[thread_id] =>
[warning_count] =>
)
I would like to note that mysqli object in UserService ain't empty.
Anyone got a clue to fix it or even properly name that problem. Cause I have trouble finding similar posts to mine.
Thanks in advance,
Whisky