tp5 redirect 重定向次数过多
来源:8-12 利用tp5-session处理商户后台登录模块(下)

qq_得默_0
2018-06-21
Login.php
<?php
namespace app\bis\controller;
use think\Controller;
class Login extends Controller
{
public function index()
{
if (request() -> isPost()){
//获取相关数据
$data = input('post.');
//相关校验
$ret = model('BisAccount') -> get(['username' => $data['username']]);
if (!$ret || $ret -> status != 1){
$this -> error('该用户不存在或者未被审核通过');
}
if ($ret -> password != md5($data['password'].$ret -> code)){
$this -> error('密码不正确');
}
model('BisAccount') -> uploadById(['last_login_time' => time()],$ret -> id);
//保存用户信息 bis是作用域
session('bisAccount',$ret,'bis');
return $this -> success('登录成功',url('index/index'));
}else{
//获取session值
$account = session('bisAccount','','bis');
if ($account && $account -> id){
return $this -> redirect('index/index');
}
return $this -> fetch();
}
}
public function loginout(){
//清除session
session(null,'bis');
$this -> redirect('login/index');
}
}
Base.php
<?php
namespace app\bis\controller;
use think\Controller;
class Base extends Controller{
public $account;
public function _initialize()
{
//判断用户是否登录
$isLogin = $this -> isLogin();
if (!$isLogin){
return $this -> redirect('login/index');
}
}
public function isLogin(){
//获取session值
$user = $this -> getLoginUser();
if ($user && $user -> id){
return true;
}
return false;
}
public function getLoginUser(){
if ($this -> account){
$this -> account = session('bisAccount','','bis');
}
return $this -> account;
}
}
Index.php
<?php
namespace app\bis\controller;
use think\Controller;
class Index extends Base
{
public function index()
{
return $this -> fetch();
}
}
redirect 的部分都没有用url()把路径包起来,如果包起来的话整个url会乱,
如果注释掉,页面地址始终是http://singwa.com/index.php/bis/index/index.html,无法跳转到登录地址
public function _initialize()
{
//判断用户是否登录
$isLogin = $this -> isLogin();
if (!$isLogin){
return $this -> success('请先登录',url('login/index'));
// return $this -> redirect('login/index');
}
}
5回答
-
qq_枫_
2019-11-25
最关键的是login控制器不能继承Base,其他的都需要继承。如果login继承了,则导致login和base之间总是在跳转。
00 -
MichonneHsu
2019-11-14
请大家注意两个session之间的差异, 一个是bisAccount 另一个BisAccount 这就是为什么无线重定向的原因,不是thinkphp版本问题。 老师难免会犯错误,毕竟不是每个人都是完美的,自己要学会审核,老师教的是开发思想。
00 -
承诺先生
2019-11-06
return $this -> success('请先登录',url('login/index')); 这种方法也不行的。一直在请先登录提示页面循环3秒。 上面南船座的回答也不管用的,加不加return都不管用。还是框架的问题。 我用的5.0.24。
00 -
南船座
2019-08-01
singwa前言已经说了 本地先在host配置一个域名
而且 $this->redirect()前 不要加return
012019-11-20 -
qq_得默_0
提问者
2018-06-22
换了5.0.03的框架可以解决
012019-10-25
相似问题
回答 3
回答 5