php开发高可用app后端,在 php开发高可用高安全的app后端 (后台登录时候的一个问题)...

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

AAffA0nNPuCLAAAAAElFTkSuQmCC2、代码分析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;

}执行代码:

AAffA0nNPuCLAAAAAElFTkSuQmCC知道了是为什么?那么我们不需要修改源码,可以这样写: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());

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值