tags:

views:

29

answers:

3

I have a web site with several pages. Page1, Page2, .. etc

I don't want the user to visit Page2 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.

+4  A: 

You could use

 $_SERVER['HTTP_REFERER']

though you can't rely on it being set every time.

Mike C
+2  A: 

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;
}
Znarkus
+1 - `HTTP_REFERER` is supplied by the user and not to be trusted
danlefree
A: 

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

Kel
I will be reading your link. Thanks.