第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

在 php開發(fā)高可用高安全的app后端 (后臺(tái)登錄時(shí)候的一個(gè)問題)

標(biāo)簽:
PHP ThinkPHP

1、当在登录页面输入不存在的用户名密码时提交时返回以下结果,那么是为什么呢?

https://img1.sycdn.imooc.com//5bc959790001901c03150197.jpg

  2、代码分析

try {
   // 判定username password
   $user = model('AdminUser')->get(['username' => $data['username']]);
   if (!$user || $user->status != config('code.status_normal')) {
       $this->error('该用户不存在');
   }
} catch (\Exception $e) {
   $this->error($e->getMessage());
}


这里程序走到了 $this->error('该用户不存在'); 那么我们看源码里有这样一段代码

throw new HttpResponseException($response);
class HttpResponseException extends \RuntimeException
{
    /**
     * @var Response
     */
    protected $response;

    public function __construct(Response $response)
    {
        $this->response = $response;
    }

    public function getResponse()
    {
        return $this->response;
    }
    
}

这里的 HttpResponseException 继承了 RuntimeException类,而RuntimeException类 继承了Exception类。这里需要php的异常处理的基础知识。

当php的语法错误或者内部错误的时候,会自动给到Exception中的 $message属性,而我们这里是程序上的逻辑错误,并没有给到 $message。

当执行到 HttpResponseException 的时候,就会直接走 catch 中的Exception,自然就是空值了。

 3、验证一下

   把 

 throw new HttpResponseException($response);

   修改为

throw new HttpResponseException($response, $msg);

 

HttpResponseException 类中构造方法修改为
public function __construct(Response $response, $message)
{
    $this->response = $response;
    $this->message = $message;
}

执行代码:

https://img1.sycdn.imooc.com//5bc95dde000112a502960241.jpg

  知道了是为什么?那么我们不需要修改源码,可以这样写:

try {
   // 判定username password
   $user = model('AdminUser')->get(['username' => $data['username']]);
   if (!$user || $user->status != config('code.status_normal')) {
      //exception('该用户不存在'); // 方式1。
      //throw new Exception('该用户不存在');// 方式2.
   }
} catch (\Exception $e) {
   $this->error($e->getMessage());
}


點(diǎn)擊查看更多內(nèi)容
1人點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消