I have a page where I need to get the referring URL so I can redirect them back after they compete a form. My problem is, the form has several drop menus and each time a selecti
if($_SESSION['GOBACK']=='')
{
$_SESSION['GOBACK'] = $_SERVER['HTTP_REFERER'];
}
This seems to work. Thanks everyone.
If you insist on doing it with forms and hidden inputs, that's easy.
<input type="hidden" name="referrer" value="<?php echo $_SERVER['HTTP_REFERER']; ?>" />
You could also do it with sessions, though, supposing you use session_start()
.
session_start();// At the very top of your page. Literally THE TOP.
// Set our session variable only if it is not currently set.
if (!isset($_SESSION['referrer'])) {
$_SESSION['referrer'] = $_SERVER['HTTP_REFERER'];
}
For more on sessions, see this. Sessions were meant for just this sort of thing, taking variables across pages and preserving state.
When they go to the page save the referrer in a session variable.
After it refreshes and does the page logic, redirect to the session variable you saved.
Store the referer in the first request into a session variable or a cookie. $_SESSION
or $_COOKIE
super globals can help achieve this.
I recommend sessions as the user won't be able to hijack the value locally.