I want to make a page on which if user press back,forward or refresh the page it should says invalid page and if user clicks on the link available on that page it should says valid page.
I don't want to disable back and forward button. It should be done in PHP
thanks in advance.
The only way I can think of this being done in PHP is to append a one-time token to each and every link on the page, and expire them on the first use. That would however require storing them somewhere, for example a database or a cache.
Sessions could work too, with a unique ID generated for each page load.
Set a key, such as uniqid()
, and put it as a GET
variable in the link. Then, the next time that user loads a page, you should expect that key. You can save it in $_SESSION
, for example, to make sure the server knows what to expect.
I would do this with JavaScript and the unload event. Then as the user navigates off of the page it's last act is to clear the DOM or store some cookie that will let you test if the page is the result of a back button or a link back to the page
I really think this is a bad idea main because it does not help your site speed. Most browser use cached copy of information during back
and forward
Secondly which means you need to generate uniqid and store it .. well you can so that with memcache
Here is what i think your code would look like (Note: Not to be used in production)
<?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>