按类从数据库中检索数据

时间:2016-03-12 16:38:39

标签: php oop fetch

我想学习编程,所以我下载了一个用OOP构建的安全脚本来管理登录和用户注册,我想通过类和对象来学习这门语言。

脚本是:https://github.com/aguvasu/Secure-PHP-OOP-user-reg-login-system-with-Boostrap

我归结为它,我无法找到解决方案的问题是我想创建一个页面,其中member.php显示注册用户列表(用户名,电子邮件等) ),但不知道该怎么做。

User.php类显示了这个:

class User {

    private $_db, $_data, $_sessionName, $_cookieName, $_isLoggedIn;

    function __construct($user = null) {
        $this->_db = DB::getInstance();
        $this->_sessionName = Config::get('session/session_name');
        $this->_cookieName = Config::get('remember/cookie_name');

        if (!$user) {
            if (Session::exists($this->_sessionName)) {
                $user = Session::get($this->_sessionName);
                if ($this->find($user)) {
                    $this->_isLoggedIn = true;
                } else {
                    //logout
                }

            }
        } else {
            $this->find($user);
        }

    }

    public function find($user = null)  {
        if ($user) {
            $field = (is_numeric($user)) ? 'id' : 'username' ;  
            $data = $this->_db->get('users', array($field, '=', $user));
            if ($data->count()) {
                $this->_data = $data->first();
                return true;
            }
        }
        return false;
    }

    public function create($fields = array()) {
        if (!$this->_db->insert('users', $fields)) {
            throw new Exception("Error Processing Request", 1); 
        }
    }

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

        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('users_sessions', array('user_id', '=', $this->data()->id));
                        if (!$hashCheck->count()) {
                            $this->_db->insert('users_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;
    }

    public function update($fields = array(), $id = null) {
        if (!$id && $this->isLoggedIn()) {
            $id = $this->data()->id;
        }

        if (!$this->_db->update('users', $id, $fields)) {
            Session::flash('uperr', 'We experienced an error while updating your profile');
            Redirect::to('account.php');
            exit();
        }
    }

    public function hasPermission($key) {
        $group = $this->_db->get('groups', array('id', '=', $this->data()->user_group));

        if ($group->count()) {
            $permissions = json_decode($group->first()->permissions, true);

            if ($permissions[$key]) {
                return true;        
            }
        }

        return false;
    }

    public function logout() {
        $this->_db->delete('users_sessions', array('user_id', '=', $this->data()->id));
        Session::delete($this->_sessionName);
        Cookie::delete($this->_cookieName);
    }

    public function data()  {
        return $this->_data;
    }

    public function isLoggedIn()
    {
        return $this->_isLoggedIn;
    }

    public function exists() {
        return (!empty($this->_data)) ? true : false ;
    }

}

0 个答案:

没有答案
相关问题