Php / html - http_referer

I create a website on one specific page, wanting to send the user to the previous page. I am new to PHP / HTML and used some existing code for ideas and help.

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, HTTP_referer is always considered empty and the user is redirected to the root page. Any obvious flaws in this code?

+6
html php
source share
6 answers

You need to use:

 $_SERVER['HTTP_REFERER'] 
+13
source

Do not rely on an HTTP link, which is a valid or even non-empty field. People can refuse this set, leaving any checks that this variable goes to the empty side of the IF-ELSE clause.

You can protect yourself from this by sending a parameter in the URL or POST parameters, which will contain a value that you can use to redirect the user.

+14
source

If you want to send a person to the previous page and make him work regardless of whether the referrer is installed correctly, you can add the GET parameter to the URL (or POST) .. you will need to encode the URL. Something like

http://www.domain.com.au/script.php?return=http%3a%2f%2fwww.domain.com.au%2fthis-is-where-i-was%2f

You can use the PHP function urlencode() .

+1
source

isset ($ _ SERVER ['HTTP_REFERER'])? $ _SERVER ['HTTP_REFERER']: '';

+1
source

Also note that the abstracting title may be empty or missing in any case, so you should not rely on it at all.

0
source

You have to use

 $_SERVER['HTTP_REFERER'] 

However, look at the register_globals configuration in php.ini, it should be disabled for security reasons. You can read more at the PHP Manual site .

0
source

All Articles