php - 该页面未使用 isset 正确重定向

标签 php html mysql redirect

我在 mozilla 和 chrome 中得到 The page isn't redirecting correctly This webpage has a redirect loop

这就是我想要做的。在我的根目录中有一个 index.phpinformation.php 还有一个文件夹 common

常见的饲料包括: common.php , db.php , header.php

在根目录的 index.php 文件中,这是从公共(public)文件夹调用 header.php 的代码。

index.php

<?php 
include('common/header.php');
?>
<p>this is some content in Index file.</p>

common.php

<?php
    @session_start();
    // Retrieve username and password from database according to user's input

    if(isset($_POST['l_submit'])){
        $user = $_POST['l_name'];
        $pass = $_POST['l_pass'];
        $stmt = mysql_query("SELECT * FROM users WHERE username = $user and password = $pass");
        $num_rows = mysql_num_rows($stmt);
        // Check username and password match
        if($num_rows > 0) {
        // Set username session variable
        $_SESSION['username'] = $_POST['l_name'];
        // Jump to secured page
        header('Location:information.php');
        }else{
            echo "<p class='message' align='center'>Wrong Username OR Password...!!</p>";
        }
    }
?>

header.php

<?php
include('db.php');
include('common.php');
if (!isset($_SESSION['username'])) {
  header("Location:index.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>..::University of Westminster::..</title>
<link rel="stylesheet" href="css/stylesheet.css"/>

<link rel="stylesheet" href="css/lightbox.css" media="screen"/>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript" src="js/light.js"></script>

<!--[if IE]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
<!--[if lte IE 7]>
    <script src="js/IE8.js" type="text/javascript"></script><![endif]-->
<!--[if lt IE 7]>

    <link rel="stylesheet" type="text/css" media="all" href="css/ie6.css"/><![endif]-->
</head>
<body>

我不明白为什么会发生重定向循环 我正在调用我的 index.php 文件中的 header.php 文件,如您所见,header.php 文件包含用于检查用户是否存在的 isset 函数,并在我正在设置 session 变量的 common.php 文件中进行重定向。

最佳答案

您正在重定向到 index.php,但是 index.php 已经包含 header.php,其中包含重定向登录。

尝试重定向到像 login.php 这样没有包含 header.php 的页面。

您还可以使用 PHP_SELF 检查 header ,无论您是否已经在 index.php 中,而不是重定向。

if (!isset($_SESSION['username']) && !strstr($_SERVER['PHP_SELF'], "index.php")) {
  header("Location:index.php");
}

关于php - 该页面未使用 isset 正确重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22345308/

相关文章:

mysql 从选择中选择

mysql - 选择表 A 中与表 B 中的一组项目相连接的行

php - 同步本地和远程 MySQL 数据库

php - 如何将现有套接字转换为使用 TLS/SSL?

java - 为什么新建的Jhipster项目body height=0?

html - 我需要 <h1> 标签才能在我的网页上获得良好的 SEO 报告。当我没有时会发生什么?

javascript - PHP根据php结果显示html字段

javascript - ajax 成功函数内的动态 PHP 内容

javascript - 使用 jquery 执行 "onchange"的正式方法?

mysql - 查找日期值为空的所有 id