亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Laravel 中限制用戶單次和多次登錄

在 Laravel 中限制用戶單次和多次登錄

PHP
當年話下 2022-06-11 09:24:10
我是 Laravel 的初學者。我想根據用戶角色限制用戶會話/登錄。所以我在我的登錄控制器中有這個方法。我有兩個角色基本的夫妻public function authenticated(Request $request, User $user){    if(Auth::check()) { //check if the user is logged in or not        $user = Auth::user();        if ($user->isBasic()) {            $previous_session = $user->session_id;            if ($previous_session) {                \Session::getHandler()->destroy($previous_session);            }            Auth::user()->session_id = \Session::getId();            Auth::user()->save();            return redirect(route('home'));        } elseif ($user->isCouple()) { //check if the user is logged in or not            $previous_session = $user->session_id;            $login = $user->no_of_logins;            if ($previous_session) {                if($login > 2) {                    \Session::getHandler()->destroy($previous_session);                    Auth::user()->no_of_logins = $user->decrement('no_of_logins');                }            }            Auth::user()->session_id = \Session::getId();            Auth::user()->no_of_logins = $user->increment('no_of_logins');            Auth::user()->save();            return redirect(route('home'));        }    }}}elseif 語句對我不起作用。我不知道該怎么做。在我的桌子上,我有這個public function up(){    Schema::create('users', function (Blueprint $table) {        $table->bigIncrements('id');        $table->string('name');        $table->string('email')->unique();        $table->enum('role', ['subscriber', 'admin', 'basic', 'couple', 'family'])->default('subscriber');        $table->timestamp('email_verified_at')->nullable();        $table->string('avatar')->nullable();        $table->integer('no_of_logins')->default(0);        $table->string('session_id')->nullable();        $table->string('password')->nullable();        $table->rememberToken();        $table->timestamps();    });}
查看完整描述

1 回答

?
POPMUISE

TA貢獻1765條經驗 獲得超5個贊

我能夠通過創建一個會話表來解決它


php artisan session:table


composer dump-autoload


php artisan migrate

然后在我的控制器中


public function authenticated(Request $request,User $user)

    {


        if(Auth::check())


        { //check if the user is logged in or not

            $user = Auth::user();

          //  $login = Session::where('user_id', Auth::id())->count();

          $login = DB::table('sessions')->where('user_id', Auth::id())->count();

           // dd($login);

            if ($user->isBasic()) 

            {

                if ($login > 0)

                {

                    Auth::logout();    

                    session()->flash('logout', "You are Logged in on other devices");

                    return redirect('login');

                }


                return redirect(route('welcome'));

            }


            elseif ($user->isCouple()) 

            {

                if ($login > 1)

                {

                    Auth::logout();    

                    session()->flash('logout', "You are Logged in on other devices");

                    return redirect('login');

                }


                return redirect(route('welcome'));

            }


            elseif ($user->isFamily()) 

            {

                if ($login > 5)

                {

                    Auth::logout();    

                    session()->flash('logout', "You are Logged in on other devices");

                    return redirect('login');

                }


                return redirect(route('welcome'));


                }

            }


            else 

            {

                return redirect(route('welcome'));

            }

        } 


查看完整回答
反對 回復 2022-06-11
  • 1 回答
  • 0 關注
  • 270 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號