Hey,
I have a login script shown below:
Models/Database.class.php
public function login($username, $password) { // Login
$data = $this->sanitise(array('email'=>$username, 'password'=>$password));
$sql = 'SELECT * FROM hussaini_members
WHERE email = \\''.$data['email'].'\\' AND password = \\''.$data['password'].'\\'';
$results = $this->mysqli->query($sql);
$personArray = array();
while($row = $results->fetch_array(MYSQLI_ASSOC)) {
$personArray[] = new Person($row);
}
return $personArray;
}
login.php (Controller)
if(isset($_POST['submit']))
{
$personArray = $database->login($_POST['email'], $_POST['password']);
if(sizeof($personArray) == 1) {
$_SESSION['currentUser'] = $personArray[0];
$_SESSION['loggedin'] = true;
$message = "";
header ("Location: homepage");
}
else {
$message = "<span style=\\"color:#00cc00\\">Invalid user credentials.</span>";
}
}
I am trying to access the session like so:
Views/template/header.phtml
<p>You are logged in as <span> <?php echo $_SESSION['loggedin']?> </span></p>
But this returns the ID. So it outputs like so:
You are logged in as 1
But i want to show the name of the person. How can i do this?
Thanks
Should be:
<p>You are logged in as <span> <?php echo $_SESSION['currentUser']?> </span></p>
Hey,
That gives me this error:
Catchable fatal error: Object of class __PHP_Incomplete_Class could not be converted to string in G:\xampp\htdocs\Manstore\Views\ emplate\header.phtml on line 23
Any ideas?
__PHP_Incomplete_Class means you deserialized an object whose class isn’t loaded. Include the file with your user class before reading from the session.
Hey,
Well in my header.phtml file at the top i have this code:
<?php
session_start();
require_once('Models/Database.class.php');
require_once('Models/Person.class.php');
$database = new Database('localhost', '****', '', '***');
?>
The Database.class includes the Person.class within it so does this still mean i need to include the Person.class.php?? I did anyway, but i still get the same error:
Catchable fatal error: Object of class __PHP_Incomplete_Class could not be converted to string in G:\xampp\htdocs\Manstore\Views\ emplate\header.phtml on line 23
Any ideas?
Hey,
Something weird i just tested on this, when i moved the session_start line below the database connection like this:
<?php
require_once('Models/Database.class.php');
require_once('Models/Person.class.php');
$database = new Database('localhost', '****', '', '***');
session_start();
?>
The error changes to this:
Catchable fatal error: Object of class Person could not be converted to string in G:\xampp\htdocs\Manstore\Views\ emplate\header.phtml on line 23
Why is this?
Any ideas what i am doing wrong?
I also tried echoing out the session like this:
<?php
$myInstance_s = $_SESSION['currentUser'];
echo $_SESSION['currentUser'];
?>
This printed out ‘N;’…
Does this mean anything?
Thanks again
Yes. php has told you the file and even the line number. You are trying to convert an object to a string. You can’t always do this. The conversion may be implicit, for example when using an object in a string context, like echo.
For the “cant be converted to string” error, you can fix it by giving Person the __toString() method:
class Person
{
public function __toString()
{
return $this->username;
}
}
Hey,
I managed to get it working like this:
$_SESSION['currentUser']->getForename();
Thanks, everyones suggestions led me to this answer 