Jump to content

Php Redirect + Keeping Referrer..?


cyfer65

Recommended Posts

I know that:

header("Location: $URL");header("Refresh: 0; $URL");

can both redirect to another URL, but the referrer gets cut out though..??

Print "<html><body onLoad=\"java script: window.location='$URL';\"></body></html>";

seems to keep the referrer - but this is using JavaScript..is there anyway to use only PHP to keep the referrer when redirecting..??

Link to comment
Share on other sites

Locate the redirection script within the page making the redirect.In other words, instead of having

http://example.com/page.php

go to

http://example.com/redirect.php?to=http%3A%2F%2Fexample.org%2F

from which point you'll be redirected to

http://example.org/

and therefore have the referrer set to

http://example.com/redirect.php?to=http%3A%2F%2Fexample.org%2F

make

http://example.com/page.php

redirect to

http://example.org/

whenever a certain $_POST or $_COOKIE condition is met, and therefore, the referrer remains

http://example.com/page.php

Link to comment
Share on other sites

boen_robot pretty much said it, although in a confusing manor :).You pretty much just have to have the page you want to be the referrer be just that, the referrer. No middleman.I found it easier to store the page name in a session variable before the redirect.

$_SESSION['referrer'] = 'http://www.example.com/' . $_SERVER['PHP_SELF'];

That allowed me to check the referrer of each page, and skip possible middlemen I didn't care about. Although if you want the middlemen, you'll have to be creative with it.Also, if you're using $_SERVER['HTTP_REFERER'], it's worth noting that the user could be using a browser that could lie about it, or some of the user's software (i.e. antimalware, antispyware, etc.) could prevent the true referrer from being given. Just an FYI.OH, if you use PHP_SELF, it won't keep any variables set at the end. Read this:http://php.about.com/od/learnphp/qt/_SERVER_PHP.htmEdit: Forgot a slash :).

Link to comment
Share on other sites

so how can i make my code keep the referrer when it forwards to the URL that is set after the ? 'QUERY_STRING'"http://example.com/page.php?http://example.org/"

<?phpif ($_SERVER['QUERY_STRING'] != ''){//---------------------------(("URL FORWARDER"))----------------------------------//If (stristr($_SERVER['QUERY_STRING'], "HTTP")){$URL = $_SERVER['QUERY_STRING'];header("Refresh: 0; $URL");}//---------------------------(("URL FORWARDER"))----------------------------------//}?>

Link to comment
Share on other sites

your code in Post #4 is what you need to use as the file named redirect.php

http://example.com/redirect.php?to=http%3A%2F%2Fexample.org%2F

Be sure to urlencode the query string as per Boen Robot's sample above.

Link to comment
Share on other sites

I am already using this method but the referrer is not being past on for any browser at all..I need to make a PHP redirect that does this..

If (stristr($_SERVER['QUERY_STRING'], "HTTP")){$URL = $_SERVER['QUERY_STRING'];header("Location: $URL");}

so when I visit this site "http://my-site.com/?http://stardrifter.org/cgi-bin/ref.cgi"it'>http://my-site.com/?http://stardrifter.org/cgi-bin/ref.cgi"it will redirect to "http://stardrifter.org/cgi-bin/ref.cgi"and on that page it will say my Referrer is "http://my-site.com/?http://stardrifter.org/cgi-bin/ref.cgi"But'>http://my-site.com/?http://stardrifter.org/cgi-bin/ref.cgi"But with the current code, no referrer is being listed..?? becuz the php function "header("Location: $URL");" seems to strip the referrer out when redirecting..is there any way to make the "header("Location: $URL");" function retain the referrer at all..??as i mentioned in my 1st post, this code will semi work, listing my referrer as "http://my-site.com/?http://stardrifter.org/cgi-bin/ref.cgi" on the page http://stardrifter.org/cgi-bin/ref.cgi

Print "<html><body onLoad=\"java script: window.location='$URL';\"></body></html>";

but this only seems to work with non IE browsers, I need it to work for all browsers..?What are my options here..??

Link to comment
Share on other sites

I am already using this method but the referrer is not being past on for any browser at all..
You mean there isn't any referrer header in the next request? If so, I'd look closely at what ragae said:
Also, if you're using $_SERVER['HTTP_REFERER'], it's worth noting that the user could be using a browser that could lie about it, or some of the user's software (i.e. antimalware, antispyware, etc.) could prevent the true referrer from being given. Just an FYI.
You probably have such a software on YOUR computer, only instead of preventing the "true referrer", it prevents any referrer from being given.
Link to comment
Share on other sites

No, i don't have any software's that could prevent the true referrer from being given..Look real simple here...The code below will redirect to whatever URL you put after the ? ['QUERY_STRING']Now visit yer site directly "http://my-site.com/?http://stardrifter.org/cgi-bin/ref.cgi"it redirects to "http://stardrifter.org/cgi-bin/ref.cgi"which is a page that tells you yer referrer... OKwhen you visit "http://my-site.com/?http://stardrifter.org/cgi-bin/ref.cgi"NO REFERRER IS SET..!!!The Function "header("Location: $URL");" strips out the referrer..i want to visit this page directly "http://stardrifter.org/cgi-bin/ref.cgi"and'>http://my-site.com/?http://stardrifter.org/cgi-bin/ref.cgi"and be redirected to "http://stardrifter.org/cgi-bin/ref.cgi"and have my referrer display as "http://my-site.com/?http://stardrifter.org/cgi-bin/ref.cgi"NOW HOW TO MAKE THAT HAPPEN..??

<?php$URL = $_SERVER['QUERY_STRING'];header("Location: $URL");?>

Link to comment
Share on other sites

The Function "header("Location: $URL");" strips out the referrer..
The header function doesn't "strip" anything out. This has nothing to do with PHP. PHP just tells your browser to redirect. Your browser is responsible for sending the next request to the server. If your browser decides not to send the referer header, that's not PHP's fault.If the browser doesn't set a referer when it receives a location header, try sending other status codes. The 3xx HTTP response codes are used for content being in a different place:http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.htmlTry sending a 301, 302, 303, or 307 response. This has examples about sending different headers:http://www.jonasjohn.de/snippets/php/headers.htmThat page has examples showing HTTP/1.1, but it's going to be more portable to use something like this:header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");instead of something like this:header("HTTP/1.1 404 Not Found");
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...