此页面不能正确地重定向

此页面不能正确地重定向

Firefox 检测到该服务器正在将指向此网址的请求无限循环重定向。


BASE

<?php

namespace app\index\controller;

use think\Controller;

class Base extends Controller
{
    public $account;

    /**
     * 空操作
     * @auth
     * @param  [type] $name [description]
     * @return [type]       [description]
     */
    public function _empty($name)
    {
        // todo
        //
        return $name;
    }

    public function _initialize()
    {
        //判断用户是否登录 直接调用这个方法就行了  $this->isLogin()
        $isLogin = $this->isLogin();
        if (!$isLogin) {
            //如果存在用户是登录的  如果不存在 就跳转
            return $this->redirect('index/login');
        }
    }

    //判断是否登录
    public function isLogin()
    {
        //获取session值
        $user = $this->getLonginUser();
        if ($user ) {  //如果 user是真的 并且 id有值
            return true;
        }
        return false;


    }

//获取session值 的方法
    public function getLonginUser()
    {
        if (!$this->account) {  //不存在获取这个值 存在就返回这个值
            $this->account = session('user', '', 'bbs');
        }
        return $this->account;
    }

}

index

<?php

namespace app\index\controller;

use think\Db;
use app\common\model\User;
use app\common\model\Topic as TopicModel;

class Index extends Base
{
    public function index()
    {
        return $this->fetch();
    }

    //注册
    public function register()
    {
        if (request()->isPost()) {
            $data = input('post.');
            if ($data['password'] != $data['password_confirmation']) {
                $this->error('两次输入的密码不一样');
            }
            $data['password'] = md5($data['password']);
            $res = model('User')->add($data);
            //申请成功 跳转url
            $this->success('注册成功', url('index/login'));
        } else {


            return $this->fetch();
        }
    }

    //登录
    public function login()
    {

        return $this->fetch();

    }

    public function logincheck()
    {

        //如果他是用post提交的 就用登录的逻辑
        if (request()->isPost()) {
            $data = input('post.');
            $res = model('User')->getusername($data['username']);
            if (!$res) {
                $this->error('用户名不存在');
            }
            //判断密码是否正确
            if (md5($data['password']) != $res->password) {
                $this->error('密码错误');
            }
            //把用户的信息记录到session
            session('user', $res, 'bbs');
            $this->success('登录成功', url('index/index'));
        } else {

            //获取session值
            $account = session('user', '', 'bbs');
            if ($account ) {
                //如果 存在 redirect调用这个方法输入url 就跳转到这个页面
                return $this->redirect(url('index/index'));
            }
        }
    }


    //退出登录
    public function logout()
    {
        //清楚session  第一个参数传null 第二个参数传作用域
        session(null, 'bss');
        //跳出 到登录页面
        return $this->redirect(url('index/login'));
    }

}

清除session值   出问题

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
guly 2019-11-27 17:59:44

你好,首页代码参考如下:

<?php
namespace app\index\controller;
use think\Db;
use app\common\model\User;
use app\common\model\Topic as TopicModel;
class Index extends Base
{
    public function index()
    {
        $topic = new TopicModel();
        $topics = $topic->getTopics();
        $this->assign('topics',$topics);
        return $this->fetch("",['user' =>session('user')]);
    }
    public function login()
    {
        if (request()->isPost()) {
            $login = input('post.login');
            $password = input('post.password');
            $cond =[];
            $cond['name|email'] = $login;
            $cond['password'] = md5(md5($password));
            $user = User::get($cond);
            if ($user) {
                session('user', $user);
                return $this->success('恭喜,登陆成功!');
            }
            return $this->error('抱歉,登陆失败!');
        }
        echo $this->fetch('login', ['user' => session('user')]);
    }
    public function register() {
        if (request()->isPost()) {
            $postData = input('post.');
            $user = new User();
            $user->name = $postData['username'];
            $user->email = $postData['email'];
            $user->avatar = 'images/avatar.jpg';
            $user->password = md5(md5($postData['password']));
            $user->created_at = intval(microtime(true));
            if($user->save()){
                return $this->success('恭喜!注册成功!', 'index/login');
            }
        }
        echo $this->fetch('register', ['user' => session('user')]);
    }
    public function logout() {
        session('user', null);
        $this->assign('user',session('user'));
        $this->success('退出成功!','index/login');
    }
}

如果还是不能解决,为了能准确解决您的问题,建议同学一作业形式提交项目代码,以便老师找出问题。祝学习愉快!

guly 2019-11-27 17:42:52

你好,清除session代码建议参考:

public function logout()
{
    //清楚session  第一个参数传null 第二个参数传作用域
    session('bss', null);
    $this->assign('bss',session('bss'));
    //跳出 到登录页面
    return $this->redirect(url('index/login'));
}

如果解决您的问题请采纳,祝学习愉快!

  • 提问者 昵称加载中__ #1
    我首页刷新 还是无法退出 没判断他 清空了session
    2019-11-27 17:48:46
  • 提问者 昵称加载中__ #2
    清除 session 第一个参数是传作用域 第二个参数传null 吗
    2019-11-29 14:43:20
  • guly 回复 提问者 昵称加载中__ #3
    是的,祝学习愉快!
    2019-11-29 15:01:29
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师