Link to home
Start Free TrialLog in
Avatar of koston
koston

asked on

HTTP_REFERER and DOCUMENT_ROOT

hi, i have set up a membership system for a website.
eveything that updates i.e. add news, update news, edit user profile etc are all verified on a single php page called updated.php
it works by the following:

$last = getenv("HTTP_REFERER");  //get last page

and then using an if statement to check the last page with the page url i.e. if the last page was addnews

if ( $last == "http://localhost/addnews.php") {
then update the database

it all works fine. although i wanted to use the $_SERVER["DOCUMENT_ROOT"] function opposed to the lanky url that i would have to change when its uploaded. currently if i set it on my hard drive (localhost)

if ($last == $_SERVER["DOCUMENT_ROOT"]."/addnews.php");

they will never equate to the same thing becoz

getenv("HTTP_REFERER") returns 'http://localhost/addnews.php'
and
$_SERVER["DOCUMENT_ROOT"] returns 'c:\ webroot' and therefore

will never equal the same thing.

will this work when uploaded on to a webserver or am i going around it the wrong way, or should i just leave the lanky link and change it to what ever it should be when it is uploaded?

thanks in advance.
Avatar of WilliamFrantz
WilliamFrantz

FYI, what you are doing will work, but it doesn't provide real security.  Anybody can spoof HTTP_REFERER and some browsers turn it off completely.

However, this might be what you are looking for:

$newsPage = 'addnews.php';
$lastPage = $_SERVER['HTTP_REFERRER'];
if (basename($lastPage) === $newsPage)  /* The user is good */
...
Avatar of koston

ASKER

hey the solution worked, after the spelling corrections hehe.
im totally new to php and so i basically just implemented everything how i would get it working.

so if i was to alter this method, the only other ways i could see it working is, by using  $_GET['action'] on the updated.page

and then

updated.php?action=addnews
updated.php?action=updatenews
updated.php?action=editprofile

would this be a more secure method or do you know an alternative WilliamFrantz?
ASKER CERTIFIED SOLUTION
Avatar of WilliamFrantz
WilliamFrantz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of koston

ASKER

yes mate i am using sessions already, oki ill change it over to the method i suggested and award you the points