Jump to content

How to block http direct request and only accept from certain URL


t3kizzleee

Recommended Posts

Hello,

 

I'm trying to make a PHP/HTML page where it blocks all direct requests and only allows requests from a certain redirect or URL.

 

For example, I have a page www.site.com/x.php

 

I don't want people to view the page if they type in "www.site.com/x.php" directly into the URL, perhaps display an error screen.

 

I want to make it so that it will only redirect from "www.site.com"

 

I hope I made some sort of sense.

My website is a shop.

 

Basically, I want to display a thank you message once a customer has placed an order.

 

I want to make a page that does that but I don't want it accessed by a direct request.

 

I want it to be displayed only by redirection from the URL of my choice whether it be www.site.com/checkout.php or whatever it is.

Okay, at the very start of the checkout.php have

<?php
session_start();
?>

 

then on the part of the check out that does the redirect, do this

$_SESSION['checkedout'] = true;
header("Location: thankyou.php"); //the re-direct

 

now in thankyou.php do this

<?php
session_start();
if(empty($_SESSION['checkedout'])){
header("Location: error.php"); //redirect to error page
exit();
}
unset($_SESSION['checkedout']); //clear session
echo "Thank you";

?>

 

To summarise: thankyou.php will redirect to an error.php if a session called 'checkedout' is not set or not set to true, and the checkout.php is the only place where it can be set to true.. thus it must be a valid checked out item

 

hope that helps

 

EDIT: oops a typo in the code (fixed)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.