[英]How to redirecting page if user directly entered page name in URL
如果用户在URL中输入页面名称,则必须重定向页面。 我有两个页面,分别称为index.php和shop.php
在index.php页面中,有一个名为<a href="shop.php?function=add">Click here</a>
的链接。 如果我单击链接,则页面正确重定向到shop.php。
现在的问题是,如果有任何用户直接在URL上输入shop.php,则该页面应在index.php页面上重定向。 这意味着用户将无法直接访问shop.php页面。
我不是在谈论header('location')
我认为,您应该尝试类似的方法:
if(isset($_GET['function']) && $_GET['function'] == 'add') {
// Show page contents
}
else {
header('location: index.php');
}
您应该尝试在会话中添加随机字符串
<?php
session_start();
$random = bin2hex(random_bytes(32));
$_SESSION['random'] = $random;
echo '<a href="shop.php?function=add&token='.$random.'">Click Here</a>';
?>
然后通过将其添加到shop.php中进行检查
if(!isset($_GET['token']) || $_GET['token'] != $_SESSION['random']){
header('Location : index.php');
}
这样,用户将不可能直接访问页面
if(isset($_GET['function'])) {
if($_GET['function'] == 'add'){
// Show page if function is "add"
}
else {
// "Function" is not "add", it can be anything else: "remove", "edit", etc.
}
}
else {
header('location: index.php');
exit();
}
如果您不想使用header('location: index.php');
您可以使用。
?>
<script type="text/javascript">
window.location.href = 'index.php';
</script>
<?php
要么
$URL = 'index.php';
echo '<script type="text/javascript">document.location.href="' . $URL . '";</script>';
echo '<meta http-equiv="refresh" content="0; url=' . $URL . '">';
最后,我找到了解决方案。 我不知道我的代码格式是否正确,但是我的问题已解决。
我从这里改变链接
<a href="shop.php?function=add">Click here</a>
to
<a href="shop.php?function=add&p_id=1">Click here</a>
并在shop.php中添加以下代码
if ( !($_GET['function']='add' && $_GET['p_id'])){
header("Location: index.php");// send to login page
exit;
}
如果有什么问题请给我建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.