Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php/html - http_referer

I am creating a website and on one particular page, am wanting to send the user back to the previous page. I am fairly new to PHP/HTML and have been using some existing code for ideas and help.

The existing code uses the following method:

if (! empty($HTTP_REFERER)) 
{
    header("Location: $HTTP_REFERER");
} else 
{
    header("Location: $CFG->wwwroot");
}

However, when I use this code the HTTP_referer is always treated as empty and the user redirected to the root page. Any obvious flaws in this code?

like image 436
Tray Avatar asked Mar 03 '09 12:03

Tray


2 Answers

Don't rely on the HTTP Referrer being a valid or even non-empty field. People can choose to not have this set leaving any checks for that variable going to the empty side of the IF-ELSE clause.

You can guard against this by sending along a parameter in either the URL or POST parameters that would hold a value that you can use to redirect the user back to.

like image 156
random Avatar answered Oct 04 '22 07:10

random


You need to use:

$_SERVER['HTTP_REFERER']
like image 42
jonstjohn Avatar answered Oct 04 '22 06:10

jonstjohn