2

hello i have this code for login and register in PHP OOP

<?php
class DB {
    public static $instance = null;

    private     $_pdo = null,
                $_query = null,
                $_error = false,
                $_results = null,
                $_count = 0;

    private function __construct() {
        try {
            $this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'), array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
            //$this->query('SET NAMES utf8');

        } catch(PDOExeption $e) {
            die($e->getMessage());
        }

    }

    public static function getInstance() {
        if(!isset(self::$instance)) {
            self::$instance = new DB();
        }
        return self::$instance;
    }

    public function query($sql, $params = array()) {

        $this->_error = false;

        if($this->_query = $this->_pdo->prepare($sql)) {
            $x = 1;
            if(count($params)) {
                foreach($params as $param) {
                    $this->_query->bindValue($x, $param);
                    $x++;
                }
            }

            if($this->_query->execute()) {
                $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                $this->_count = $this->_query->rowCount();
            } else {
                $this->_error = true;
            }
        }

        return $this;
    }

    public function get($table, $where) {
        return $this->action('SELECT *', $table, $where);
    }

    public function delete($table, $where) {
        return $this->action('DELETE', $table, $where);
    }

    public function action($action, $table, $where = array()) {
        if(count($where) === 3) {
            $operators = array('=', '>', '<', '>=', '<=');

            $field      = $where[0];
            $operator   = $where[1];
            $value      = $where[2];

            if(in_array($operator, $operators)) {
                $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";

                if(!$this->query($sql, array($value))->error()) {
                    return $this;
                }

            }

            return false;
        }
    }

    public function insert($table, $fields = array()) {
        $keys   = array_keys($fields);
        $values = null;
        $x      = 1;

        foreach($fields as $value) {
             $values .= "?";
            if($x < count($fields)) {
                 $values .= ', ';
            }
            $x++;
        }

        $sql = "INSERT INTO {$table} (`" . implode('`, `', $keys) . "`) VALUES ({$values})";

        if(!$this->query($sql, $fields)->error()) {
            return true;
        }

        return false;
    }

    public function update($table, $id, $fields = array()) {
        $set    = null;
        $x      = 1;

        foreach($fields as $name => $value) {
            $set .= "{$name} = ?";
            if($x < count($fields)) {
                $set .= ', ';
            }
            $x++;
        }

        $sql = "UPDATE {$table} SET {$set} WHERE id = {$id}";

        if(!$this->query($sql, $fields)->error()) {
            return true;
        }

        return false;
    }

    public function results() {
        // Return result object
        return $this->_results;
    }

    public function first() {
        return $this->_results[0];
    }

    public function count() {
        // Return count
        return $this->_count;
    }

    public function error() {
        return $this->_error;
    }
    public function lastInsertId(){
        return $this->_pdo->lastInsertId();
    }
    public function __sleep(){
        return array();
    }
}

and i save the user login data in session, everything works fine in localhost, but in my web server i have a problem in the line $this->_query->bindValue($x, $param);

Catchable fatal error: Object of class __PHP_Incomplete_Class could not be converted to string in

i know that is something wrong with my session but i cant find whats the problem, and this is my session class

<?php
class Session {
    public static function exists($name) {
        return (isset($_SESSION[$name])) ? true : false;
    }

    public static function get($name) {
        return $_SESSION[$name];
    }

    public static function put($name, $value) {
        return $_SESSION[$name] = $value;
    }

    public static function delete($name) {
        if(self::exists($name)) {
            unset($_SESSION[$name]);
        }
    }

    public static function flash($name, $string = null) {
        if(self::exists($name)) {
            $session = self::get($name);
            self::delete($name);
            return $session;
        } else if ($string) {
            self::put($name, $string);
        }
    }
}

please tell me if you can what i can to do with that error thank you very much.
and this is the code where i store my session

public function login($username = null, $password = null, $remember = false) {

        if(!$username && !$password && $this->exists()) {
            Session::put($this->_sessionName, $this->data()->id);
        } else {
            $user = $this->find($username);

            if($user) {
                if($this->data()->password === Hash::make($password, $this->data()->salt)) {
                    Session::put($this->_sessionName, $this->data()->id);

                    if($remember) {
                        $hash = Hash::unique();
                        $hashCheck = $this->_db->get(Config::get('mysql/tbl_user_sessions'), array('user_id', '=', $this->data()->id));

                        if(!$hashCheck->count()) {
                            $this->_db->insert(Config::get('mysql/tbl_user_sessions'), array(
                                'user_id' => $this->data()->id,
                                'hash' => $hash
                            ));
                        } else {
                            $hash = $hashCheck->first()->hash;
                        }

                        Cookie::put($this->_cookieName, $hash, Config::get('remember/cookie_expiry'));
                    }

                    return true;
                }
            }
        }

        return false;
    }

i just do var_dump of my session['user']

array(1) { 
    ["user"]=> &object(__PHP_Incomplete_Class)#1 (6) { 
        ["__PHP_Incomplete_Class_Name"]=> string(4) "User" 
        ["_db":"User":private]=> object(__PHP_Incomplete_Class)#2 (1) { 
            ["__PHP_Incomplete_Class_Name"]=> string(2) "DB" 
        } 
        ["_sessionName":"User":private]=> string(4) "user" 
        ["_cookieName":"User":private]=> string(4) "hash" 
        ["_data":"User":private]=> object(stdClass)#3 (7) { 
            ["id"]=> string(3) "144" 
            ["username"]=> string(5) "admin" 
            ["password"]=> string(64) "0611affa6664e471b939cd3197b49e0c3b47d146fc12a472c4275dbd85a7cd67" 
            ["salt"]=> string(32) "458a0dbfbd9bdca381e50b8d753329ea" 
            ["name"]=> string(12) "Artur Papyan" 
            ["joined"]=> string(19) "2013-11-29 07:41:54" 
            ["group"]=> string(1) "1" 
        } 
        ["_isLoggedIn":"User":private]=> bool(true) 
    } 
}
Passerby
  • 9,184
  • 2
  • 28
  • 44
Arturik1988
  • 103
  • 2
  • 13
  • Post the full error message, since you cut it off before the location. Also, reference the line number in your question. – Daniel Jan 16 '14 at 07:45
  • Show your code: building query, filling session. – sectus Jan 16 '14 at 07:51
  • Catchable fatal error: Object of class __PHP_Incomplete_Class could not be converted to string in oop/classes/DB.php on line 38 and the line 38 is this line $this->_query->bindValue($x, $param); – Arturik1988 Jan 16 '14 at 07:53
  • Show your code with `Session:get` – sectus Jan 16 '14 at 08:06
  • We need to see the surrounding class for `public function login` – Eric Jan 16 '14 at 08:21
  • [Please](http://stackoverflow.com/a/4596323/1607098), [don't](http://www.youtube.com/watch?v=-FRm3VPhseI) [use](http://www.youtube.com/watch?v=RlfLCWKxHJ0) [*singletons*](http://stackoverflow.com/q/137975/1607098) – Touki Jan 16 '14 at 08:25
  • There is no code where session become filled. – sectus Jan 16 '14 at 08:25
  • i have at top and my session class and my login class where i store session – Arturik1988 Jan 16 '14 at 08:32
  • i slove the problem i change the session name, from user i make it user_session thank's for help – Arturik1988 Jan 16 '14 at 08:47

1 Answers1

0

It looks as though you are attempting to output the result to a string, which it cannot do as the class returns an object.

Also, Referring to DB.php line 38 is a red herring, as it is displaying the error message to the called function.

$this->_query->bindValue($x, $param);

Instead look to where your code is calling the class, and how you are attempting to bind your variables within your code it's self, instead of the actual call to DB.php file.

guyver4mk
  • 527
  • 5
  • 11