Jump to content

__PHP_Incomplete_Class in php session


billy_111

Recommended Posts

Hi everyone,

 

I have a problem and i don't know how to solve it. Basically i am setting up a login. I have a Database & Person Class, and i am using an MVC structure. This is my code:

 

Models/Database.class.php

 

<?php
require_once('Person.class.php');

class Database {
    protected $mysqli;

    public function sanitise($data) { // Escapes parameters before sending SQL query
        foreach($data as $key => $value){
            $data[$key] = $this->mysqli->real_escape_string($value);
        }
        return $data;
    }

    public function __construct($host, $username, $password, $dbname) {
        $this->mysqli = new mysqli($host, $username, $password, $dbname);
        if ($this->mysqli->errno){
            echo 'Unable to connect'.$this->mysqli->error;
            exit();
        }
    }

    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;
    }

    public function __destruct() {
        $this->mysqli->close();
    }
}

 

Models/Person.class.php

 

<?php

class Person {
    public $id, $email, $password, $forename, $surname, $address, $postcode, $date_added;

    public function __construct($dbrow) {
        $this->id = $dbrow['ID'];
        $this->email = $dbrow['email'];
        $this->password = $dbrow['password'];
        $this->forename = $dbrow['forename'];
        $this->surname = $dbrow['surname'];
        $this->address = $dbrow['address'];
        $this->postcode = $dbrow['postcode'];
        $this->date_added = $dbrow['date_added'];
    }

    public function getId(){
        return $this->id;
    }

    public function getEmail(){
        return $this->email;
    }

    public function getPassword(){
        return $this->password;
    }

    public function getForename(){
        return $this->forename;
    }

    public function getSurname(){
        return $this->surname;
    }

        public function getAddress(){
        return $this->address;
    }

    public function getPostcode(){
        return $this->postcode;
    }

    public function getDateAdded(){
        return $this->date_added;
    }
}

 

Then in my login.php file i have this:

 

<?php
$view->pageTitle = 'Login';
require_once('Models/Database.class.php');
$database = new Database('localhost', '*****', '', '******');
session_start();

$message = "";

  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>";
    }
  }
    
require_once('Views/login.phtml');

 

Now when i want to display the session i write this:

 

<?php echo $_SESSION['currentUser']?>

 

But i get an error saying:

 

Catchable fatal error: Object of class __PHP_Incomplete_Class could not be converted to string in G:\xampp\htdocs\Manstore\Views\template\header.phtml  on line 24

 

I realised that this was because i needed to include my Person class before session_start(); so i now have this in my header of the page:

 

<?php
require_once('Models/Database.class.php');
session_start();
$database = new Database('localhost', '****', '', '****');
?>

 

And then try echoing out the $_SESSION['currentUser'] but now i get another error:

 

Catchable fatal error: Object of class Person could not be converted to string in G:\xampp\htdocs\Manstore\Views\template\header.phtml  on line 24

 

What does this mean? And how can i fix this?

 

I would really appreciate any help. Sorry for all my code i thought it was necessary to show you exactly what i am trying to do so you can find the error..

 

Thanks again

 

Kind regards

Billy

Link to comment
Share on other sites

Perfect answer, thank you so much!

 

Much appreciated.

 

Just something for advice, would it be wise to create a Session.class file to manage the sessions instead of in the login.php page?

 

If so, how could i do this with the code i already have?

 

Thanks again.

Link to comment
Share on other sites

You could create a session object but it won't replace your login.php page code, you still need to call objects to be able to use them.

 

ps: Your login method doesn't belong in the database object either.

Link to comment
Share on other sites

Ok, so should i simply create another class called login.class.php and include the login function within there?

 

Is there a specific reason why you have suggested the login not to be in the database class?

 

thanks, i appreciate the advice

Link to comment
Share on other sites

Ok, so should i simply create another class called login.class.php and include the login function within there?

 

Yeah, something like that. Maybe, an Authenitcate[/m] class?

 

Is there a specific reason why you have suggested the login not to be in the database class?

 

Because it has nothing to do with a database.

 

You could also remove all those variable assignments from your Person objects constructor and simply have your login method return an already constructed Person. Something like.....

 

public function login($username, $password) { // Login
  $data = $this->db->sanitise(array('email'=>$username, 'password'=>$password));
  $sql = '
    SELECT * FROM hussaini_members
    WHERE email = \''.$data['email'].'\' AND password = \''.$data['password']. "' LIMIT 1";
  if ($results = $this->db->query($sql)) {
    if ($results->num_rows()) {
        return $results->fetch_object('Person');
    }
  }
  return false;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.