...

View Full Version : header/ document.referrer



TrainReq
02-25-2007, 07:39 PM
I'd like to have a PHP page accessible only if the referrer comes from http://www.site.com/blah.html , if the referrer isn't /blah.html , it will header.location to another page that has an "Access Denied" error, and if the referrer is blah.html let all the PHP/HTML go through.. I was wondering if there is any way you can do that with javascript (with document.referrer) and set an if in the PHP , or do it with header Referrer.

JayStang
02-25-2007, 09:22 PM
No, you wouldn't be able to do that. PHP cannot use a javascript variable (such as document.referrer). Remember PHP (being a server side language) will run first, before any client side code.

Try using HTTP_REFERER. See link below for more info.

http://us2.php.net/reserved.variables

TrainReq
02-25-2007, 09:39 PM
er.. ok.. i found out... now... how do I run it on " if(empty($_SESSION['id'])) " (more/less if the session is not there run this following code):


<?php
if($_SERVER['HTTP_REFERER'] =="http://www.site.com"){
CODE
}else{
header("location: http://google.com");
}
?>

JayStang
02-25-2007, 10:03 PM
i'm not sure if i follow? are you trying to see if a session variable is defined or not? or if its blank?

if(isset($_SESSION['id']) || $_SESSION['id'] != ''){

}

TrainReq
02-25-2007, 10:04 PM
if there is no session in ID or if the session is expired, run the above query.

JayStang
02-25-2007, 10:17 PM
yeah if it is expired it will no longer be set.. just run the if statement i wrote above.

TrainReq
02-25-2007, 10:21 PM
so I used



<?php



if(isset($_SESSION['id']) || $_SESSION['id'] != ''){

if($_SERVER['HTTP_REFERER'] =="http://www.site.com"){
CODE
}else{
header("location: http://google.com");
}

?>

And the query doesnt work period now (while no session, of course)

JayStang
02-26-2007, 12:58 AM
i'm confused, are you saying its working how you want it now or its not?

rafiki
02-26-2007, 11:53 AM
<?php

session_start() //isnt this required at top of all pages within a session?

if(isset($_SESSION['id']) || $_SESSION['id'] != ''){ //opens and doesn't close

if($_SERVER['HTTP_REFERER'] =="http://www.site.com"){
CODE
}else{
header("location: http://google.com");
}

?>

JayStang
02-26-2007, 02:44 PM
i thought you only needed session_start() on the page thats setting the session variable. i could be wrong though.

194673
05-27-2007, 07:50 PM
No, session_start() is required on any page using the session.
session_start() can be used to start a new session, or continue from the current one ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum