dongyulian5801 2014-03-10 17:14
浏览 39
已采纳

PHP OOP记住我的功能错误

I've been following this tutorial by phpacademy on OOP Login and Register functions and I cant seem to be able to get the remember me function to work. I think the problem is that $user-> login(); doesnt pass any data but i cant seem to be able to fix it.

and here is a part of my init.php

if (Cookie::exists(Config::get('remember/cookie_name')) && !Session::exists(Config::get('session/session_name'))) {
$hash = Cookie::get(Config::get('remember/cookie_name'));
$hashCheck = DB::getInstance()->get('users_session', array('hash', '=', $hash));

if ($hashCheck->count()) {
    $user = new User($hashCheck->first()->user_id);
    $user-> login();
}

}

this is my User.php file

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

    if (!$username && !$password  && $this->exists()) { 

//problematic if statement removing $this->exists() gives me the error  'Trying to get property of non-object'
        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_session', array('user_id', '=', $this->data()->id));

                    if (!$hashCheck->count()) {
                        $this->_db->insert('users_session', 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 exists() {
    return (!empty($this->_data)) ? true : false;
}
  • 写回答

2条回答 默认 最新

  • dongnei3634 2014-03-10 18:38
    关注

    These (partial)files work for me:

    init.php:

    if(Cookie::exists(Config::get('remember/cookie_name')) && !Session::exists(Config::get('session/session_name'))) {
        $hash = Cookie::get(Config::get('remember/cookie_name'));
        $hashCheck = DB::getInstance()->get('users_session', array('hash', '=', $hash));
    
        if($hashCheck->count()) {
            $user = new User($hashCheck->first()->user_id);
            $user->login();
        }
    }
    

    User.php: (the complete file I have)

    <?php
        class User {
            private $_db,
                    $_data,
                    $_sessionName,
                    $_cookieName,
                    $_isLoggedIn;
    
            public 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 {
    
                        }
                    }
                } else {
                    $this->find($user);
                }
            }
    
            public function update ($fields = array(), $id = null) {
                if(!$id && $this->isLoggedIn()) {
                    $id = $this->data()->id;
                }
    
                if(!$this->_db->update('users', $id, $fields)) {
                    throw new Exception('There was a problem updating');
                }
            }
    
            public function create($fields) {
                if(!$this->_db->insert('users', $fields)) {
                    throw new Exception('There was a problem creating an account');
                }
            }
            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 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('users_session', array('user_id', '=', $this->data()->id));
    
                                if(!$hashCheck->count()) {
                                    $this->_db->insert('users_session', 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 hasPermission($key) {
                $group = $this->_db->get('groups', array('id', '=', $this->data()->group));
    
                if($group->count()) {
                    $permissions = json_decode($group->first()->permissions, true);
    
                    if($permissions[$key] == true) {
                        return true;
                    }
                }
                return false;
            }
    
            public function exists() {
                return (!empty($this->_data)) ? true : false;
            }
    
            public function logout() {
                $this->_db->delete('users_session', 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;
            }
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
  • ¥20 关于#stm32#的问题:STM32串口发送问题,偶校验(even),发送5A 41 FB 20.烧录程序后发现串口助手读到的是5A 41 7B A0
  • ¥15 C++map释放不掉
  • ¥15 Mabatis查询数据
  • ¥15 想知道lingo目标函数中求和公式上标是变量情况如何求解
  • ¥15 关于E22-400T22S的LORA模块的通信问题
  • ¥15 求用二阶有源低通滤波将3khz方波转为正弦波的电路