Why is it that after the teacher typed this code to set the user session value, there was no response when I clicked the login button and I couldn't log in?
人生如戏
人生如戏 2018-05-05 13:15:39
0
5
1566

  Session::set('user_id',$user->id);

  Session::set('user_info',$user->getData());


人生如戏
人生如戏

reply all(4)
David

tp5.1 changed the reference path to: use think\Facade\Session;

每一天,为明天

Print it first to see if you can output the data you submitted and see if the data is correct

人生如戏

user.php

....

use think\Request;

use app\index\model\User as UserModel;

use think\Session;

....

public function checkLogin(Request $request)

{

//Initial return parameters

$status = 0;

$result = '';

$data = $request -> param();

//Create validation rules

                  $rule = [                                                                                                                                                               ’ ’ ’ s ’ s ’ s                     ’ ’ ’ s ’ ’ s ’ ’ s ‐ ‐ ‐ ‐ ‐ ‐ to 'name|Username' => 'require',

##                      ## 'Veritfy | Verification Code' = & GT; 'Require | CAPTCHA',

##;

## // ## $ msg = [

# 'name' = & gt; ['Require' = & gt; 'Username cannot be empty, please check'],

'password' = & gt; [[[[[[[[ 'require'=>'The password cannot be empty, please check'],


'veritfy' =>['require'=>'The verification code cannot be empty, please check',

                         'captcha'=>'Verification code error, please check'

                                                                                                                            $result = $this -> validate($data,$rule,$msg);

##                                                                                                                                                                                                                                                ($ Result === True)

# {

// Construction query condition

$ map = [

# 'name' = & gt; $ data ['name'],

# 'Password' = & GT; md5 ($ data ['password']

##] [[

$ User = Usermodel :: get ($ map);

## counter ($ user == null) {


$ result = 'No found! ';

      }else{

            $status = 1;

              $result = 'Verification passed, click [OK] to enter!';

            //设置用户的Session值

            //Session::set('user_id',$user->id);//用户ID

          //  Session::set('user_info',$user->getData());//获取用户所有信息


          Session::set('user_id,$user[id]');

          Session::set('user_info.name',$user['name']);

        

          }

        }

        return ['status'=>$status,'message'=>$result,'data'=>$data];

    }

header.htnl

<li>超级管理员</li>

<li class="dropDown dropDown_hover"> <a href="#" class="dropDown_A">{:session('user_info.name')} <i class="Hui-iconfont"></i></a>

<ul class="dropDown-menu menu radius box-shadow">

<li><a href="{:url('user/logout')}">退出</a></li>


每一天,为明天

Post all the code! !

  • reply user.php .... use think\Request; use app\index\model\User as UserModel; use think\Session; .... public function checkLogin(Request $request) { //Initial return parameters $status = 0; $result = ''; $data = $request -&gt; param(); //Create validation rules $rule = [ 'name|username' =>'require', 'password|password' => 'require', 'veritfy|Verification code' =&gt;'require|captcha', ]; //Create verification failure information $msg = [ 'name' =&gt;['require'=&gt;'Username cannot be empty, please check'], 'password' =&gt;['require'=&gt;'Password cannot be empty, please check'], 'veritfy' =&gt;['require'=&gt;'Verification code cannot be empty, please check', 'captcha'=&gt;'Verification code error, please check' ], ]; //Perform simple verification $result = $this -&gt; validate($data,$rule,$msg); //If verification is successful, execute if($result === true) { //Construct query conditions $map = [ 'name'=&gt;$data['name'], 'password'=&gt;md5($data['password']) ]; //Query user information $user = UserModel::get($map); if($user == null){ $result = 'The user was not found! '; }else{ $status = 1; $result = 'Verification passed, click [OK] to enter!'; //Set the user's Session value //Session::set('user_id',$user-&gt;id);//User ID // Session::set('user_info',$user-&gt;getData());//Get all user information Session::set('user_id,$user[id]'); Session::set('user_info.name',$user['name']); } } return ['status'=&gt;$status,'message'=&gt;$result,'data'=&gt;$data]; } header.html &lt;li&gt;Super Administrator&lt;/li&gt; &lt;li class="dropDown dropDown_hover"&gt; &lt;a href="#" class="dropDown_A"&gt;{:session('user_info.name')} &lt;i class="Hui-iconfont"&gt;& #xe6d5;&lt;/i&gt;&lt;/a&gt; &lt;ul class="dropDown-menu menu radius box-shadow"&gt; &lt;li&gt;&lt;a href="{:url('user/logout')}"&gt;Exit&lt;/a&gt;&lt;/li&gt;
    人生如戏 author 2018-05-05 17:11:56
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template