Php 用户可以使用直接链接访问受保护的页面。如何创建重定向?
我有一个班级的网站作业,我遇到了一些麻烦。当用户使用直接链接访问受保护的页面时,他们仍然可以访问该页面。但是,如果他们使用不正确的凭据登录主页,则不会将他们指向受保护的页面。如何保护我的网页不被直接链接访问 索引页面是登录页面,当人们登录时,它将进行身份验证并指向受保护的_page.php 但如果我要在网络浏览器中输入:它仍然会引导我通过。如何阻止或重定向 如果有帮助,我可以发布一些源代码Php 用户可以使用直接链接访问受保护的页面。如何创建重定向?,php,html,mysql,redirect,url-redirection,Php,Html,Mysql,Redirect,Url Redirection,我有一个班级的网站作业,我遇到了一些麻烦。当用户使用直接链接访问受保护的页面时,他们仍然可以访问该页面。但是,如果他们使用不正确的凭据登录主页,则不会将他们指向受保护的页面。如何保护我的网页不被直接链接访问 索引页面是登录页面,当人们登录时,它将进行身份验证并指向受保护的_page.php 但如果我要在网络浏览器中输入:它仍然会引导我通过。如何阻止或重定向 如果有帮助,我可以发布一些源代码 <?php include_once 'includes/db_connect.php'; incl
<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';
sec_session_start();
if (login_check($mysqli) == true) {
$logged = 'in';
} else {
$logged = 'out';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;charset=ISO-8859-1" />
<link rel="stylesheet" href="style.css" type="text/css" />
<title>Login</title>
<script type="text/JavaScript" src="js/sha512.js"></script>
<script type="text/JavaScript" src="js/forms.js"></script>
</head>
<body>
<?php
if (isset($_GET['error'])) {
echo '<p class="error">Error Logging In!</p>';
}
?>
<form action="includes/process_login.php" method="post" name="login_form">
<p>Usuario:</p>
<p>
<input type="text" name="email" />
</p>
<p>Contrasena:</p>
<p>
<input type="password"
name="password"
id="password"/>
<input type="button"
value="Aceptar"
onclick="formhash(this.form, this.form.password);" />
</p>
</form>
<?
<?php
if (login_check($mysqli) == true) {
echo '<p>Currently logged ' . $logged . ' as ' . htmlentities($_SESSION['username']) . '.</p>';
echo '<p>Do you want to change user? <a href="includes/logout.php">Log out</a>.</p>';
} else {
echo '<p>Currently logged ' . $logged . '.</p>';
echo "<p>If you don't have a login, please <a href='register.php'>register</a></p>";
}
?>
这就是index.php
这里是protected_page.php
<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';
sec_session_start();
?>
<?php if (login_check($mysqli) == true) : ?>
</h2>
<p>Welcome <?php echo htmlentities($_SESSION['username']); ?>!</p>
<p>Return to <a href="index.php">login page</a></p>
<?php else : ?>
<p> <span class="error">You are not authorized to access this page.</span> Please <a href="index.php">login</a>. </p>
<?php endif; ?>
<br />
if(isset($_SESSION['logged_in'])){
echo "</h2>
<p>Welcome <?php echo htmlentities($_SESSION['logged_in'], ENT_QUOTES, 'UTF-8'); ?>!</p>
<p>Return to <a href="index.php">login page</a></p>" ;
} else {
//redirect the user
header('Location:index.php');
exit();
}
检查$\u SESSION变量,如果该变量无效或定义的会话无效,请使用下面的方法
中可用的标头方法允许您重定向用户:
<?php
header("Location: http://www.yourRedirect.com/"); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit;
?>
所以在你的情况下
<?php if (login_check($mysqli)) : ?>
</h2>
<p>Welcome <?php echo htmlentities($_SESSION['username']); ?>!</p>
<p>Return to <a href="index.php">login page</a></p>
<?php else : ?>
<?php header("Location: errorPageLocation");
<?php endif; ?>
<br />
您应该使用会话标识符
if (login_check($mysqli) == true) {
$_SESSION['logged_in'] = 'logged_in' ;
}
然后在protected_page.php中
<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';
sec_session_start();
?>
<?php if (login_check($mysqli) == true) : ?>
</h2>
<p>Welcome <?php echo htmlentities($_SESSION['username']); ?>!</p>
<p>Return to <a href="index.php">login page</a></p>
<?php else : ?>
<p> <span class="error">You are not authorized to access this page.</span> Please <a href="index.php">login</a>. </p>
<?php endif; ?>
<br />
if(isset($_SESSION['logged_in'])){
echo "</h2>
<p>Welcome <?php echo htmlentities($_SESSION['logged_in'], ENT_QUOTES, 'UTF-8'); ?>!</p>
<p>Return to <a href="index.php">login page</a></p>" ;
} else {
//redirect the user
header('Location:index.php');
exit();
}
我只关注protected_page.php,因为您不希望使用直接链接访问它
<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';
sec_session_start();
if(!isset($_SESSION['username'])) {
header("Location: index.php");
}
?>
<?php if (login_check($mysqli) == true) : ?>
</h2>
<p>Welcome <?php echo htmlentities($_SESSION['username']); ?>!</p>
<p>Return to <a href="index.php">login page</a></p>
<?php else : ?>
<p> <span class="error">You are not authorized to access this page.</span> Please <a href="index.php">login</a>. </p>
<?php endif; ?>
<br />
这意味着如果未设置$_SESSION['username'],它将重定向到index.php,这是headerLocation:index.php的工作。顺便说一下,只有在有人登录时才会设置$\u会话['username'.会话开始之前,我是否将会话标识符放入index.php中?非常感谢您的帮助,顺便说一句。课后开始非常感谢您。哈哈,这太简单了,所以现在我想知道我会不会把这个附加到每一页上?如果我只做一个单独的PHP,然后在每个页面上添加一个require,会更容易吗?是的,您需要将它附加到您不想使用直接链接访问的每个页面上。如果有太多的页面,您不想被访问,最好使用单独的PHP,然后添加一个“require”。如果需要,它还可以减少代码更新的麻烦。