2

我想创建一个页面,如果用户按下后退、前进或刷新页面,它应该说无效页面,如果用户点击该页面上可用的链接,它应该说有效页面。

我不想禁用后退和前进按钮。它应该在 PHP 中完成

提前致谢。

4

4 回答 4

2

我会用 JavaScript 和unload 事件来做到这一点。然后当用户离开页面时,最后的动作是清除 DOM 或存储一些 cookie,让您测试页面是返回按钮的结果还是返回页面的链接

于 2013-04-21T19:30:51.483 回答
1

我认为在 PHP 中完成此操作的唯一方法是在页面上的每个链接上附加一个一次性令牌,并在第一次使用时使它们失效。但是,这需要将它们存储在某个地方,例如数据库或缓存。

会话也可以工作,为每个页面加载生成一个唯一的 ID。

于 2013-04-21T19:28:48.620 回答
0

设置一个键,例如uniqid(),并将其作为GET变量放入链接中。然后,下次该用户加载页面时,您应该期待该键。例如,您可以将其保存在 中$_SESSION,以确保服务器知道会发生什么。

于 2013-04-21T19:27:39.507 回答
0

我真的认为这是一个坏主意,主要是因为它无助于您的网站速度。back大多数浏览器在和期间使用缓存的信息副本forward

其次,这意味着您需要生成 uniqid 并将其存储..您可以这样使用memcache

这是我认为您的代码的样子(注意:不要在生产中使用

<?php

/**
 * Make Sure the Broswer does to cache a copy
 */
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');

// Use Fast Storage
$memcache = new Memcache();
$memcache->addserver("127.0.0.1");

// Just a sample link to know what has been clicked
$link = isset($_GET['link']) ? $_GET['link'] : null;
$link = basename($link);

try {

    // uniqid per page
    $uid = isset($_GET['uid']) ? $_GET['uid'] : false;

    if (! $uid) {
        throw new Exception("Invalid Link");
    }
    if ($memcache->get($uid))
        throw new Exception("Refresh Detected");

        // Add the link as used
    $memcache->add($uid, 1, null, 600); // this would be deleted in 10 mins

    // Simple Return message
    $message = "Clicked:" . ucwords($link);
} catch ( Exception $e ) {

    // Simple Error Message
    $message = $e->getMessage();
    // do somthing usefull here
}

// New Ramdom uinqid
$uid = bin2hex(mcrypt_create_iv(50, MCRYPT_DEV_URANDOM));

?>

<html>

<head>
<title><?php echo $message ?></title>
<script type="text/javascript"
    src="http:////ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

<script type="text/javascript">
$(function(){

    // Load all links
    $('a').each(function() {

        // Check with correspond to localhost .. since that what am using
        if(this.href.match("/localhost/"))
        {
            // Replace all links
            $(this).attr('href', 'http://localhost/lab/stackoverflow/a.php?uid=<?php echo $uid ;?>&link=' + this.href);
        }
    });
});
</script>
</head>

<body>
    <h3><?php echo $message ?></h3>
    <ul>
        <li><a href="index">Index</a></li>
        <li><a href="home">Home</a></li>
        <li><a href="about">About</a></li>
        <li><a href="http://google.com">Google</a></li>

    </ul>


</body>
</html>
于 2013-04-21T20:17:22.527 回答