Search code examples
phphtmlbrowser-history

How can i make a page accessable only from a page that redirects the user to it?


i want to make a page accessable only from a page that redirects to it. this page which redirects to this page is called /purchase.php and then this page redirects to a page which is called /username.php, i want THIS page to be accessable only from /purchase.php and not directly from a url.

Solution: For purchase.php:

<?php
session_start();
//Put this when the purchase is vailidated
$SESSION_['fromMain'] = "true";
//Then redirect
header ("Location: url.com/username.php");
?>

For username.php:

<?php
//Check if the browser comes from purchase php
if($_SESSION['fromMain'] == "false"){
//If not redirect to index page
header ("Location: url.com/index.php
} else {
$SESSION_['fromMain'] = "false";
{
?>

Solution

  • As an additional measure (besides the comment already made by CD001) you could utilize the $_SERVER["HTTP_REFERER"] variable, which will be empty if the script is requested right from the browser interface.