如何确定调用PHP头的URL

I've got a page under http://www.example.com/abc/def/a/ where a user can buy products. For a marketing activity (printed paper) the customer should type in a shorter url http://www.example.com/order/

When this url is called, the server executes this script:

<?php
header("Location: http://www.example.de/abc/def/a/");
exit;
?> 

The page under http://www.example.com/abc/def/a/ contains some informations (rebate-code etc.) which should only be visible to users coming from http://www.example.com/order/

$_SERVER['HTTP_REFERER'] seems to be not reliable from what I've read.

I checked with phpinfo(); if there is any info variable which contains "order" but I haven't found one.

Is it possible or do you recommend an alternative approach?

HTTP is in it's pure form a stateless-protocol, so you won't find anything in the protocol itself that will help you with your current problem.

Using $_SESSION to store data in-between requests is the easiest route to walk, and what I recommend.

As said; since the protocol used to transfer information is stateless you have no choice but to create a method for your web-application to recognize which request is done by which user.. this is a perfect problem solved by php-sessions.


As you have discovered, the HTTP Referer, along with all of the other headers, can easily be faked. The only reliable way I see of accomplishing this is logging users as they visit the orders page and when they visit the /abc/def/a/ page, verify that the log entry exists. This kind of log could be stored in $_SESSION, however be sure that when using multiple servers you have the proper setup to ensure all servers share the same session information (you can use a redis server to store session data).

On the order page:

session_start();
$_SESSION['order_visited'] = true;

On the rebate code page:

session_start();
if(!isset($_SESSION['order_visited']) || !$_SESSION['order_visited']) {
    header('Location: /order'); // Must visit order first
    die();
}