dongwen2162 2019-02-15 00:55
浏览 377

如何修复“重定向你太多次了。”

I was created simple login and register system and have some issue. But not sure is it with cookie or cookie and php. My code is next:

On index.php ( login page i have this code in header ):

<?php
include('includes/config.php');
if(!$user->is_logged_in()){
header('Location: index.php');
exit;
}
if(isset($_POST['submit'])){

$username = $_POST['username'];
$password = $_POST['password'];

if($user->login($username,$password)){ 
    $_SESSION['username'] = $username;
    header('Location: home.php');
    exit;

} else {
    $error[] = 'Wrong username or password or your account has not been activated.';
}
}
?>

And on home page when user is successfully logged in:

<?php include('includes/config.php'); 
if(!$user->is_logged_in()){
header('Location: index.php');
exit;
}
?>

Config file:

<?php
ob_start();
session_start();

date_default_timezone_set('Europe/London');

define('DBHOST','localhost');
define('DBUSER','root');
define('DBPASS','pass');
define('DBNAME','db_name');

define('DIR','http://example.com/');
define('SITEEMAIL','noreply@domain.com');

try {
$db = new PDO("mysql:host=".DBHOST.";dbname=".DBNAME, DBUSER, DBPASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch(PDOException $e) {
echo '<p class="bg-danger">'.$e->getMessage().'</p>';
exit;
}
include('classes/user.php');
include('classes/phpmailer/mail.php');
$user = new User($db);
?>

Problem is next, when user successfully logged in and redirected to home page, when user is log out everything is fine, but when user logged in and without log out go back to index page ( log in page ) i get this error:

This page isn’t working website.com redirected you too many times. Try clearing your cookies. ERR_TOO_MANY_REDIRECTS

And when i clear my cookie from google chrome browser, error is fixed but user must login again and when same step do again i get again same error.

  • 写回答

2条回答

  • douhu2898 2019-02-15 01:21
    关注

    You should break this up into three scripts, with the following logic:

    Home page (and all other content pages): If they're not logged in, it redirects to the login page.

    Login page: If they're already logged in, redirect to the home page.

    Password checker: This is the action of the login form on the login page. It checks the username and password. If they're correct, it sets the session variable that says that the user is logged in, and redirects to home page. If they're not correct, it redirects back to the login page.

    评论

报告相同问题?

悬赏问题

  • ¥25 关于##爬虫##的问题,如何解决?:
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误