I redirect to a URL in PHP using the following code:
header('Location: http://site.com/target.php');
How do I get the URL that redirected to the target? I tried this code but it's not working:
echo $_SERVER["HTTP_REFERER"];
Thanks
I redirect to a URL in PHP using the following code:
header('Location: http://site.com/target.php');
How do I get the URL that redirected to the target? I tried this code but it's not working:
echo $_SERVER["HTTP_REFERER"];
Thanks
Try this:
<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>
Sumit's Code gave you the current URL and you can use it using session :
$current_url="http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$_SESSION['url']=$current_url;
header('Location: http://site.com/target.php');
Get the URL on the Page :
echo $_SESSION['url'];
You can't rely on the REFERER
header, because some browsers will not send it as a security precaution.
If you need to know what URL they requested originally, you could pass it as a query parameter in the Location
URL. Alternatively, you could store it in a database on the server and pass a key into the database as part of the query string, or similarly store it in a session on the server and pass the session ID as part of the query string.
You may get a more useful answer if you can explain what you're trying to do exactly. Why do you need to know what address they came from, and why do you need to redirect to a different address? Also, are the two addresses on the same website (i.e., the same domain name)?
<?php echo $_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'] ; ?>
this link will really help you $_SERVER
Each time you visit a page store that in $_SESSION
, so whenever you need the value of the previous page you can view it easily.