选择语言 :

 Core_Auth::get_member_by_username

根据用户名获取用户

用户对象,不存在则返回false Core_Auth::get_member_by_username( string $username )

参数列表

参数 类型 描述 默认值
$username string $username
返回值
  • 用户对象,不存在则返回false
File: ./core/classes/auth.class.php
public function get_member_by_username($username)
{
    if (!isset(Auth::$user_info[$this->config_name][$username]))
    {
        if ($this->config['driver']==Auth::DRIVER_DATABASE)
        {
            # 数据库类型
            $tables         = $this->config['tablename'];
            $user_field     = $this->config['username_field']?$this->config['username_field']:'username';
            $password_field = $this->config['password_field']?$this->config['password_field']:'password';
            $data           = Database::instance($this->config['database'])->from($tables)->where($user_field, $username)->limit(1)->get()->current();
        }
        elseif ($this->config['driver']==Auth::DRIVER_FILE)
        {
            $file = DIR_DATA . 'auth-data-of-project-' . Core::$project . '.json';
            if (is_file($file))
            {
                $data = @json_decode(file_get_contents($file), true);
                if ($data && isset($data[$username]))
                {
                    $data = $data[$username];
                }
                else
                {
                    $data = array();
                }
            }
            else
            {
                $data = array();
            }
        }

        if ($data)
        {
            $member_obj = $this->config['member_object_name']?$this->config['member_object_name']:'Member';
            Auth::$user_info[$this->config_name][$username] = new $member_obj($data);
        }
        else
        {
            Auth::$user_info[$this->config_name][$username] = false;
        }
    }

    return Auth::$user_info[$this->config_name][$username];
}