此页面不能正确地重定向
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值 出问题
15
收起
正在回答 回答被采纳积分+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');
}
}如果还是不能解决,为了能准确解决您的问题,建议同学一作业形式提交项目代码,以便老师找出问题。祝学习愉快!
PHP常用技术与ThinkPHP5框架开发
- 参与学习 人
- 提交作业 225 份
- 解答问题 3372 个
掌握用PHP开发互联网网站的必备功能,掌握当下主流的Linux系统开发,并熟练使用热门框架ThinkPhp开发电商团购项目,是通向PHP工程师必经之路。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星