解決laravel5中auth用戶登錄其他頁面獲取不到登錄信息的問題
首先創(chuàng)建user表,里面有:id, name, password,remember_token等字段。
然后再M(fèi)odels添加表模型User.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use DB;
class User extends Model implements AuthenticatableContract
{
protected $table = 'user';
protected $primarykey = 'id';
public $timestamps = false;
protected $fillable = ['user_name', 'password', 'user_phone', 'user_email', 'user_role_id', 'user_avart', 'user_sex', 'user_age', 'user_birthday', 'last_login_ip', 'last_login_time', 'is_disabled', 'remember_token'];
/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier(){
return $this->getKey();
}
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword(){
return $this->password;
}
/**
* Get the token value for the "remember me" session.
*
* @return string
*/
public function getRememberToken()
{
return $this->{$this->getRememberTokenName()};
}
/**
* Set the token value for the "remember me" session.
*
* @param string $value
* @return void
*/
public function setRememberToken($value)
{
$this->{$this->getRememberTokenName()} = $value;
}
/**
* Get the column name for the "remember me" token.
*
* @return string
*/
public function getRememberTokenName()
{
return 'remember_token';
}
}
然后需要在配置文件config/auth.php中修改如下配置:
'model' => App\Models\User::class, //指定模型 'table' => 'user', //指定用戶表(user是我數(shù)據(jù)中儲(chǔ)存用戶的表)
接著在登錄方法里使用Auth::login() 方法登錄,如下:
public function store(Request $request)
{
if(empty($request->get('chkCode')) || trim($request->get('chkCode')) != trim(Session::get('admincaptcha'))){
$error->add('result','驗(yàn)證碼不正確');
return back()->withErrors($error);
}
$adminUser = User::where('user_name',$request->get('user_name'))->where('user_role_id', '>', 0)->first();
if(empty($adminUser)){
$error->add('result','用戶無后臺(tái)權(quán)限');
return back()->withErrors($error);
}else{
if (md5($request->get('user_pwd'))===$adminUser->password&&$adminUser->user_role_id){
Auth::login($adminUser);
Session::put('admincaptcha', "");
return redirect()->route('admin.home');
}else{
$error->add('result','用戶名或密碼錯(cuò)誤');
return back()->withErrors($error);
}
}
}
然而雖然這個(gè)頁面可以獲取到登錄信息,然而其他頁面卻沒有,原來是因?yàn)閕d和密碼我用的是user_id和user_pwd不是id和password,這兩個(gè)必須不能變,改了之后可以正常登錄。
以上這篇解決laravel5中auth用戶登錄其他頁面獲取不到登錄信息的問題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
php實(shí)現(xiàn)批量下載百度云盤文件例子分享
本文使用百度開放云的PHP SDK實(shí)現(xiàn)批量下載百度云盤的文件,需要的朋友可以參考下。2014-04-04
PHP+Ajax異步通訊實(shí)現(xiàn)用戶名郵箱驗(yàn)證是否已注冊(cè)( 2種方法實(shí)現(xiàn))
在網(wǎng)站注冊(cè)用戶時(shí)使用,主要為了無刷新異步驗(yàn)證用戶輸入的用戶名或者Email是否已注冊(cè)。2011-12-12
yii實(shí)現(xiàn)級(jí)聯(lián)下拉菜單的方法
這篇文章主要介紹了yii實(shí)現(xiàn)級(jí)聯(lián)下拉菜單的方法,需要的朋友可以參考下2014-07-07
淺談PHP解析URL函數(shù)parse_url和parse_str
這篇文章主要介紹了PHP解析URL函數(shù)parse_url和parse_str,并給出了相應(yīng)的示例,非常的實(shí)用,有需要的朋友們可以參考下2014-11-11
ThinkPHP5分頁paginate代碼實(shí)例解析
這篇文章主要介紹了ThinkPHP5分頁paginate代碼實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
單一index.php實(shí)現(xiàn)PHP任意層級(jí)文件夾遍歷(Zjmainstay原創(chuàng))
本程序?qū)崿F(xiàn)了使用一個(gè)index.php文件來實(shí)現(xiàn)所有文件夾的遍歷效果,避免了需要無窮復(fù)制index.php至文件夾下才能實(shí)現(xiàn)的效果2012-07-07
thinkPHP多表查詢及分頁功能實(shí)現(xiàn)方法示例
這篇文章主要介紹了thinkPHP多表查詢及分頁功能實(shí)現(xiàn)方法,結(jié)合具體實(shí)例形式分析了thinkPHP多表查詢以及查詢結(jié)果的分頁顯示相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-07-07

