[英]The page isn't redirecting properly using isset
我得到的页面在mozilla和chrome中无法正确 重定向 。 该网页具有重定向循环 。
这就是我想要做的。 在我的根目录下有一个的index.php和information.php文件也是常见的文件夹
常见的文件包括:common.php,db.php,header.php
在根目录的index.php文件中,这是从公用文件夹调用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函数来检查用户是否存在,并且也在common.php文件,我正在设置会话变量。
您将重定向到index.php
,但是index.php
已经包含header.php
,它具有重定向登录名。
尝试重定向到诸如login.php
不包含header.php
的页面。
您还可以使用PHP_SELF
签入标头,以确定是否已经在index.php
中,而不进行重定向。
if (!isset($_SESSION['username']) && !strstr($_SERVER['PHP_SELF'], "index.php")) {
header("Location:index.php");
}
如果您没有已登录用户的会话,则将无休止地折腾他们。
在您的索引中包含header.php,如果您未登录,则该文件将使您返回index.php,只要没有会话,该问题同样适用。 由于您无休止地被折腾,因此您没有任何机会进行此会话。
这就是我所做的,以使其与当前的逻辑配合使用。 我只需要更改index.php,其余部分保持不变。
在index.php中
<?php
include_once('common/db.php');
include_once('common/common.php');
if(isset($_SESSION['username'])) {
header("Location: information.php");
}
?>
<section>
<div class="row">
...
.
我在这里检查了会话是否已设置,并将其重定向到information.php文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.