简体   繁体   English

如果用户直接在URL中输入页面名称,如何重定向页面

[英]How to redirecting page if user directly entered page name in URL

I have to redirect page if the user entered page name in URL. 如果用户在URL中输入页面名称,则必须重定向页面。 I have two pages called as index.php and shop.php 我有两个页面,分别称为index.php和shop.php

In index.php page there is a link called <a href="shop.php?function=add">Click here</a> . 在index.php页面中,有一个名为<a href="shop.php?function=add">Click here</a>的链接。 If i clicked on link then page is redirecting on shop.php properly. 如果我单击链接,则页面正确重定向到shop.php。

Now the issue is if any user directly entered shop.php on URL then the page should be redirected on index.php page. 现在的问题是,如果有任何用户直接在URL上输入shop.php,则该页面应在index.php页面上重定向。 That means the user will not able to access directly shop.php page. 这意味着用户将无法直接访问shop.php页面。

I am not talking about header('location') 我不是在谈论header('location')

I think, you should try something like: 我认为,您应该尝试类似的方法:

if(isset($_GET['function']) && $_GET['function'] == 'add') {
    // Show page contents
}
else {
    header('location: index.php');
}

You should try adding a random string to a session 您应该尝试在会话中添加随机字符串

 <?php 
 session_start();
$random = bin2hex(random_bytes(32));
$_SESSION['random'] = $random;
 echo '<a href="shop.php?function=add&token='.$random.'">Click Here</a>';
?>

Then check by adding this to the top off shop.php 然后通过将其添加到shop.php中进行检查

if(!isset($_GET['token']) || $_GET['token'] != $_SESSION['random']){
header('Location : index.php');
}

This way it will be impossible for the user to access the page directly 这样,用户将不可能直接访问页面

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();
}

If you not want to use header('location: index.php'); 如果您不想使用header('location: index.php'); you can use. 您可以使用。

?>
<script type="text/javascript">
    window.location.href = 'index.php';
</script>
<?php

OR 要么

$URL = 'index.php';
echo '<script type="text/javascript">document.location.href="' . $URL . '";</script>';
echo '<meta http-equiv="refresh" content="0; url=' . $URL . '">';

Finally, I found my solution. 最后,我找到了解决方案。 I don't know my code is in the right format or not but my issue got resolved. 我不知道我的代码格式是否正确,但是我的问题已解决。

I change the link from this 我从这里改变链接

<a href="shop.php?function=add">Click here</a>
to
<a href="shop.php?function=add&p_id=1">Click here</a>

and add below code in shop.php 并在shop.php中添加以下代码

if ( !($_GET['function']='add' && $_GET['p_id'])){
    header("Location: index.php");// send to login page
   exit;
}

Please suggest me if anything wrong. 如果有什么问题请给我建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM