I have a web site with several pages. Page1, Page2, .. etc
I don't want the user to visit Pag开发者_开发问答e2 posting its URL directly in the browser because Page2 should be visited only from Page1. In PHP, what should I code in Page2 to know that the user was before in Page1 and clicked a link or something that brough him or her to Page2? Thanks.
You could use
$_SERVER['HTTP_REFERER']
though you can't rely on it being set every time.
You could use PHP sessions.
page1.php
session_start();
$_SESSION['pages_visited'][basename(__FILE__)] = true;
page2.php
session_start();
$_SESSION['pages_visited'][basename(__FILE__)] = true;
if (!isset($_SESSION['pages_visited']['page1.php']) || !$_SESSION['pages_visited']['page1.php']) {
header('Location: page1.php');
exit;
}
In order to implement reliable mechanism, you may use sessions mechanism: http://www.php.net/manual/en/book.session.php
In this case information about visited pages can be stored on server side and will be retrieved by session id, which is stored in user's cookies / GET/POST variables
精彩评论