How to Stop direct links to web pages using PHP

Post Reply
Tony
Lieutenant
Lieutenant
Posts: 86
Joined: Tue Jul 21, 2009 4:11 pm

How to Stop direct links to web pages using PHP

Post by Tony » Sun Nov 29, 2009 4:48 am

Want to stop someone from entering your site unless they come in the "front door"? Or want to keep a frame page from being loaded outside of the frameset? This script example will not let you load page2 unless you have already loaded page1 in the same browser session.

Note that if someone loads page1 they can then go directly to page2 anytime in the SAME browser session. But if they close the browser, then next time they must again enter through the "front door".

page1.php has this script:

Code: Select all

<?php
session_start();
$_SESSION['valid'] = "yes";
echo "Page 1";
?>
page2.php has this script:

Code: Select all

<?php
session_start();
if ($_SESSION['valid'] != "yes") header("Location: page1.php");
echo "Page 2";
?>
Post Reply

Return to “PHP & MySQL”