Php 页面不是';t使用isset正确重定向
我发现在mozilla和chrome中,页面没有正确重定向。此网页有一个重定向循环 这就是我想做的。在我的根目录中有一个index.php和information.php文件夹common 常见的fodler包括: common.php、db.php、header.php 在根目录中的index.php文件中,这是从公共文件夹调用header.php的代码 index.phpPhp 页面不是';t使用isset正确重定向,php,html,mysql,redirect,Php,Html,Mysql,Redirect,我发现在mozilla和chrome中,页面没有正确重定向。此网页有一个重定向循环 这就是我想做的。在我的根目录中有一个index.php和information.php文件夹common 常见的fodler包括: common.php、db.php、header.php 在根目录中的index.php文件中,这是从公共文件夹调用header.php的代码 index.php <?php include('common/header.php'); ?> <p>this
<?php
include('common/header.php');
?>
<p>this is some content in Index file.</p>
<?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>";
}
}
?>
<?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>
<?php
include_once('common/db.php');
include_once('common/common.php');
if(isset($_SESSION['username'])) {
header("Location: information.php");
}
?>
<section>
<div class="row">
...
.
这是索引文件中的一些内容
common.php
<?php
include('common/header.php');
?>
<p>this is some content in Index file.</p>
<?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>";
}
}
?>
<?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>
<?php
include_once('common/db.php');
include_once('common/common.php');
if(isset($_SESSION['username'])) {
header("Location: information.php");
}
?>
<section>
<div class="row">
...
.
如果您没有登录用户的会话,您将无休止地丢弃它们
在您的索引中包括header.php,如果您没有登录,它会将您发送回index.php,在那里,只要没有会话,同样的问题也适用。
由于您被无休止地抛来抛去,因此您没有机会进行此会话。如果您没有登录用户的会话,您将无休止地抛去它们
在您的索引中包括header.php,如果您没有登录,它会将您发送回index.php,在那里,只要没有会话,同样的问题也适用。
由于您被无休止地抛来抛去,因此您没有机会进行此会话。您正在重定向到index.php
,但是index.php
已经包含了header.php
,其中包含重定向登录
尝试重定向到类似于login.php
的页面,该页面没有包含header.php
您还可以使用php\u SELF
在index.php
中使用签入头,而不是重定向
if (!isset($_SESSION['username']) && !strstr($_SERVER['PHP_SELF'], "index.php")) {
header("Location:index.php");
}
您正在重定向到index.php
,但是index.php
已经包含了header.php
,其中包含重定向登录
尝试重定向到类似于login.php
的页面,该页面没有包含header.php
您还可以使用php\u SELF
在index.php
中使用签入头,而不是重定向
if (!isset($_SESSION['username']) && !strstr($_SERVER['PHP_SELF'], "index.php")) {
header("Location:index.php");
}
这就是我所做的,使它与当前的逻辑工作。我只需修改index.php,其余部分保持不变
在index.php中
<?php
include('common/header.php');
?>
<p>this is some content in Index file.</p>
<?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>";
}
}
?>
<?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>
<?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文件 这就是我所做的,以使其与当前逻辑一起工作。我只需修改index.php,其余部分保持不变
在index.php中
<?php
include('common/header.php');
?>
<p>this is some content in Index file.</p>
<?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>";
}
}
?>
<?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>
<?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文件 尝试添加一个退出代码>在标题
函数之后。侧注:停止使用不推荐的mysql.*
函数。使用或代替。这是一个好的PDO。此外,您的代码还受到SQL注入攻击,因为您直接允许在查询中插入POST值。旁注:如果您正在调试代码,请不要抑制警告/错误。删除那些@
。旁注:记得调用退出代码>在标题之后(“位置:$url”)代码>如果不是最后一行而不是使用包含一次
,请使用要求一次
。当试图包含的文件丢失时,抛出错误优于抛出警告。请尝试添加退出代码>在标题
函数之后。侧注:停止使用不推荐的mysql.*
函数。使用或代替。这是一个好的PDO。此外,您的代码还受到SQL注入攻击,因为您直接允许在查询中插入POST值。旁注:如果您正在调试代码,请不要抑制警告/错误。删除那些@
。旁注:记得调用退出代码>在标题之后(“位置:$url”)代码>如果不是最后一行而不是使用包含一次
,请使用要求一次
。当试图包含的文件丢失时,抛出错误要比抛出警告好。OP最好不要修改上述逻辑,而是重新设计流。@Raptor:我同意你的意见。好的,我已经在header.php中给出了签入,所以OP可以使用当前design@SajithNair请检查我的答案,它的工作。感谢您的帮助。OP最好重新设计流程,而不是修改上述逻辑。@Raptor:我同意您的意见。好的,我已经在header.php中提供了签入,所以OP可以使用当前的design@SajithNair请检查我的答案,它的工作。谢谢你的帮助,那是不对的。header()
函数也将页面的其余部分发送到information.php
。记住在标题('Location:xxx')之后添加退出代码>。另外,正如前面所说,require_once()
比include_once()
@Raptor Yes肯定会这样做。再次感谢你的帮助,真的很感激。那是不对的。header()
函数也将页面的其余部分发送到information.php
。记住在标题('Location:xxx')之后添加退出代码>。另外,正如前面所说,require_once()
比include_once()
@Raptor Yes肯定会这样做。再次感谢你的帮助,真的很感激。