Forum Moderators: coopster

Message Too Old, No Replies

PHP HTTP_REFERER Help

HNeed to make a HTML page only load grom a few URL's

         

deisenbacher

1:12 pm on Apr 25, 2005 (gmt 0)

10+ Year Member



Hello
I have a basic HTML page that I want to only load from a certain Referering URLS. can anyone help
Basicaly the cod eis a web form but I only want it to load if you come from [1.com,...] [2.com,www.3.com....]

I can make it php or jsut add java script I dont care i just want it to work.

Any help would be lovely.

DE

buriedUnderGround

2:23 pm on Apr 25, 2005 (gmt 0)

10+ Year Member



Here's some simple PHP to get you started on this one:

<?
//create array of valid referers
$allowedReferers = array("http://www.1.com",
"http://www.2.com",
"http://www.3.com");

//do a simple check to see
//if $HTTP_REFERER is in $allowedReferers array
if(!in_array($HTTP_REFERER,$allowedReferers)){
//if check fails end script.
echo "Invalid referer!";
die();
}
?>

deisenbacher

4:48 pm on Apr 25, 2005 (gmt 0)

10+ Year Member



Hello
Thanks
with all the issues with Referer, is it better to look for a session
If session = "" run if not die

Would you be so kind in giving an example to set session and to check the session

DE

jatar_k

5:02 pm on Apr 25, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



this thread talks about sessions
[webmasterworld.com...]

buriedUnderGround

6:40 am on Apr 26, 2005 (gmt 0)

10+ Year Member



very roughly...

in every referring page include something like this:

<?
//start session
session_start();
//register a variable to pass to the next page
$_SESSION['validReferer'] = TRUE;
?>

then in your form that does the checking do something like this:

<?
//start session
session_start();
//check if variable is in $_SESSION
//if it's not, die()
if(!isset($_SESSION['validReferer'])){
echo "Invalid referer!";
die();
}
?>