Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果没有按钮点击,如何阻止对URL的直接访问?_Javascript_Php_Html - Fatal编程技术网

Javascript 如果没有按钮点击,如何阻止对URL的直接访问?

Javascript 如果没有按钮点击,如何阻止对URL的直接访问?,javascript,php,html,Javascript,Php,Html,我有一个本地站点,第二个页面只能通过一个特定的按钮访问,而不能通过编写URL直接访问 我有一个简单的例子: 第1页: <html> <head><title>page 1</title></head> <body> <button type="button"><a href="page2.html">to page 2</a></button> </body> &l

我有一个本地站点,第二个页面只能通过一个特定的按钮访问,而不能通过编写URL直接访问

我有一个简单的例子:

第1页:

<html>
<head><title>page 1</title></head>
<body>
<button type="button"><a href="page2.html">to page 2</a></button>
</body>
</html>

第1页
第2页:

<html>
<head><title>page 2</title></head>
<body>
<h1>PAGE 2</h1>
</body>
</html>

第2页
第2页

谢谢。

您不想依赖HTTP Referer头来完成这项工作,因为它很容易伪造。客户端可以简单地将refereheader与请求一起发送,即使它们从未加载第一页

要实现这一点,您需要生成一个唯一的令牌(只有您的服务器和客户端知道),并要求第二个页面接收该令牌以加载页面

例如:

<?php
function generateToken() {
    return base64_encode(random_bytes(64));
}

session_start(); // start a session to store this token
$_SESSION['token'] = generateToken(); // store the token temporarily in the session
// you can also store it in your database or in memcached if you prefer
?>
<html>
    <head><title>page 1</title></head>
    <body>
        <button type="button">
            <a href="page2.php?token=<?=htmlspecialchars($_SESSION['token'])?>">
            to page 2
            </a>
        </button>
    </body>
</html>

第1页
现在,第二页需要验证此令牌是否正确

<?php
session_start(); // load the session data
if (empty($_SESSION['token']) ||
    empty($_GET['token']) ||
    $_GET['token'] !== $_SESSION['token'])
{
    echo "You did not reach this page by the link from page1!";
    exit; // stop the page from loading
}
?>
<html>
    <head><title>page 2</title></head>
    <body>
        <h1>PAGE 2</h1>
    </body>
</html>

第2页
第2页

您不想依赖HTTP Referer头来完成这项工作,因为它很容易伪造。客户端可以简单地将refereheader与请求一起发送,即使它们从未加载第一页

要实现这一点,您需要生成一个唯一的令牌(只有您的服务器和客户端知道),并要求第二个页面接收该令牌以加载页面

例如:

<?php
function generateToken() {
    return base64_encode(random_bytes(64));
}

session_start(); // start a session to store this token
$_SESSION['token'] = generateToken(); // store the token temporarily in the session
// you can also store it in your database or in memcached if you prefer
?>
<html>
    <head><title>page 1</title></head>
    <body>
        <button type="button">
            <a href="page2.php?token=<?=htmlspecialchars($_SESSION['token'])?>">
            to page 2
            </a>
        </button>
    </body>
</html>

第1页
现在,第二页需要验证此令牌是否正确

<?php
session_start(); // load the session data
if (empty($_SESSION['token']) ||
    empty($_GET['token']) ||
    $_GET['token'] !== $_SESSION['token'])
{
    echo "You did not reach this page by the link from page1!";
    exit; // stop the page from loading
}
?>
<html>
    <head><title>page 2</title></head>
    <body>
        <h1>PAGE 2</h1>
    </body>
</html>

第2页
第2页

将按钮更改为“输入”,并为其命名

<html>
<head><title>page 1</title></head>
<body>
<button type="button"><a href="page2.html">to page 2</a></button>
   <form url="page2.php">
       <input type="button" value="1" name="btn" />
   </form>
</body>
</html>

将按钮更改为“输入”,并为其命名

<html>
<head><title>page 1</title></head>
<body>
<button type="button"><a href="page2.html">to page 2</a></button>
   <form url="page2.php">
       <input type="button" value="1" name="btn" />
   </form>
</body>
</html>