I'm doing a 301 redirect on site A to site B - when the user arrives at site B it needs to find the page the user came from. This doesn't seem to be working though:
$_SERVER['HTTP_REFERER']
whereas if I do a link to the page
go
I get the referrer through. Is there a reason it doesn't come through after the redirect? If so can anyone offer any advice on how to do this. I want to avoid at all costs having a query string on the redirect.
Is there maybe another header I need to send with the page that redirects?
Thanks for any advice!
The thing is, the HTTP_REFERER is site A. That's just how a 301 works.
That said, the easy way to do this is to take the url of the referrer to site A onto the end of site B's URL as a variable. Then, at site B, any time you have a referral from site A, you can have it.
<?php
header("Location: http://site-b.com/?ref="
.urlencode($_SERVER['HTTP_REFERER']),TRUE,301);
?>
Then of course at site B, access urldecode($_GET['ref']) for your referrer.
However... if you're looking to avoid _GET variables, you have a few options.
A) Collect the _GET request, then re-munge the URL -- IE have site B redirect to a "clean" version of itself.
B) Have your redirecting page make a curl or stream_get_contents over to a "collection" page prior to issuing a header(), where you collect and store the any session information (like the refererer) and have it prepared to be processesed when they redirect.
You could try adding a CGI query string to the end of your URL when doing the redirect -- eg
http://www.site-b.com?redirectfrom=www.site-a.com
site-b.com would ignore the URL parameter, but it would be recorded in the logs and would be accessible from within PHP.
You can do it with javascript. Use the following script, but it between <head> and </head>
<script type="text/javascript">
location.href='http://www.site-b.com';
</script>
This will of course not make a proper HTTP 301 redir, but I just tested it, and it will send the referer (the referer being site-a).
Related
Our website is currently using Google Universal Analytics to track goal conversions from our own private domain and a 3rd party payment processor. Currently our goal flow is:
/cart -> /checkout -> /receipt
On the /cart page which resides on our private domain, the user clicks "checkout". When they do, it goes to /precheckout which is a preprocessing script which finishes with header("Location: external.com/checkout"); exit;
Our suspicions lead us to believe that it's this intermediary page that is breaking our funnel. From what I understand, when header("Location: "); is used and no data is sent to the browser, that it should simply follow the redirect. What is happening?
I'm willing to bet that the server changing the location is causing the browser to not send the "Referer" header.
Try header('Referer: /cart'); in the /checkout.
Since the user is not clicking on anything in between precheckout and checkout, you need to manually set it. If the user isn't always going to be clicking that link to get to the checkout page (i.e. bookmarking it), it may be better to set a session variable to determine if they just came from the cart and if they did use the header code above.
The 301 redirect is the preferred way to redirect URLs, since it informs search engines that the URL has moved for good, and search engines should put the new URL page in the search results instead of the old URL page and transfer the new URL page, the page rank of the old URL page.
The 301 redirect can be done across domains or on the same domain.
Google ""analytics"" recommends to use 301 redirect.
<?php
// PHP permanent URL redirection
header("Location: http://www.example.com/new-page.php", true, 301);
exit();
?>
https://www.rapidtables.com/web/dev/url-redirect.html
How do I create a php which redirects with use of the URL. I.e. - Redirect-To.com/Change.php?=Google.com Then goes to google etc
I'm wanting to create a php page where a user goes to
Redirect-To.com/Change.php?=Google.com
or
Redirect-To.com/Change.php?=Youtube.com
or
Redirect-To.com/Change.php?=Yahoo.com
Then the visited page redirects to Google or whatever page is after ?=
This is basically what URL shorteners do, except they lookup the destination from a database and probably store some tracking information.
Check out the header function that lets you set HTTP headers. You want to set a 302 redirect, which means moved temporarily, and a Location header to set the new location.
http://php.net/manual/en/function.header.php
header('HTTP/1.1 302 Moved Temporarily');
header('Location: ' . $newLocation');
301 is another common redirect code, but it means "Moved Permanently" and a lot of browsers will cache that status and not hit your website if the person clicks your link again.
The HTTP spec says you should give the full URL with the redirect, though most browsers will work without it.
You may also want to white list destinations; a user might follow a link to your site and be redirected to a site owned by a a malicious third party. Thinking they were in the confines of your site, they may enter information or perform actions allowing the third party to gain access to their data.
In the URL, values without keys will be ignored, so you should write:
...change.php/?redirect=http://www.google.com
Then you can access the "redirect to" address with $_GET['redirect'].
To redirect to a given address, you can use to Location header (make sure nothing is sent to the output before calling the function):
header('Location: ' . $_GET['redirect']);
You have to start the redirecting URL with "http(s)://" like I wrote above.
If you want to stop the script (as it does not stop immediately after the redirect function), you have to call the die() or the exit() function.
(If a shorter URL is better for you, the URL can be just ...change.php/?http://www.google.com, then you can access the address with $_SERVER['QUERY_STRING'].)
I'm trying to redirect users to another page, however, I need the Referer header to show that there was a redirection. In other words, if the user starts on SiteA, then goes to SiteB, which redirects to SiteC, I need the Referer header to show that the user came from SiteB when they arrive at SiteC.
Most browsers don't change the Referer header when encountering a 3xx (301, 307, etc.) redirect code so I can't use that. So I'm wondering what the best way is to accomplish what I need? I have HTML, PHP, and if necessary JavaScript at my disposal.
Thanks,
Harry
P.S. I've done enough HTML, PHP, and JavaScript coding to get by, just not enough to know the answer to this question.
Correct me if I'm wrong, but the 3xx status codes is to tell the browser that the page has move, and that they are to request the page again at the new url. So to a browser, the current page has not changed, and thus the referrer will remain he same.
Use a javascript like location=url during page load to redirect the page instead.
Is it possible to detect source of web forwarding?
For example,
Domain A redirects to Domain B where Domain B has PHP hosting?
Basically I would like something like the following:
if ($was_redirected_from_domain_a) { ... }
As #MoarCodePlz and #Christopher Armstrong point out, $_SERVER["HTTP_REFERER"] is the solution.
However, in your specific case, two redirects take place:
http://fhc.quickmediasolutions.com/image/-1457172086.png
This way, the original referrer info is lost. You will need to disable the second redirect, and run your PHP in my-art-gallery.co.uk's index page.
Update after seeing the phpinfo() output:
$_SERVER["HTTP_REFER"] is indeed completely non-existent.
I suspect the culprit is this configuration setting:
suhosin.server.strip = On
your hosting company is running the Suhosin PHP patch, which allows removing certain data from the PHP page for enhanced security. You may need to ask them to activate HTTP_REFERER.
The only other way would be redirecting domain A to something like
domainb.co.uk/index.php?camefrom=domainA
You could then fetch the domainA argument through $_GET["camefrom"] - if the hosting provider's control panel allows that sort of redirection.
What you need to look at is known as the url referrer of the page. The url referrer is the url from which the current user made it to the site. Be careful, though, as the url referrer will be nonexistent if the user opened up a tab and simply typed in the url.
The url referrer should be able to be found using the following:
$myVar = $_SERVER['HTTP_REFERER'];
As Pekka said, it depends on how the user was forwarded. Try checking the $_SERVER['http_referrer'] value:
if ($_SERVER['HTTP_REFERER'] == 'mydomain.com/mypage'){
echo 'Came from mydomain';
}
$_SERVER["HTTP_REFERER"] is not a reliable solution. There are different cases where it does not work.
HTTP_REFERER does not contain the URL of the page that redirected, but the URL of the page where the user clicked.
E.g. On the page example.com is a link to t.co/somelink, which redirects to yoursite.com.
$_SERVER["HTTP_REFER"] will contain http://example.com, and there is no way to know that your visitor was redirected on your site from a twitter short URL.
The only way to know that the user came from your twitter link, is to include a $_GET parameter, like already proposed: Let the link t.co/somelink redirect to yoursite.com/?camefrom=twitter.
I'm having difficulty trying to figure this out. I'm not even sure if it's possible.. I will appreciate any sort of help!!
On site A, I have a link (an affiliate type link) that redirects to site B. When clicking the link on site A, I use this script to redirect..
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $url);
exit;
the $url var is just site's "A" URL. What I can't figure out is how to pass a variable from the redirection script onto site B without using a query string in the URL itself (for example, http://www.siteB.com/?var_to_pass=something)
Also, both sites are on a different server so I'm not sure if sessions will work. But between sites I have a script which I hope I can use someone to achieve what I need.
There's only one way to pass data between sites via a redirection, just as you're doing, in the URL via query vars. You can't make a browser redirect via POST, so GET's your only option.
You can do it with a POST and javascript but it's not as pretty (or as reliable). Simply have a form that's submitted by the body onLoad event.
You just want to hide the variable from people? You can do this:
www.sitea.com -> (redirect) -> www.siteb.com/incoming?var=blah
# then
www.siteb.com/incoming?var=blah -> (redirect) -> www.siteb.com/
It will happen so quickly that the user won't even see and the end result is that siteb gets the variable from sitea and the user ends up on a clean looking url.
Alternatively you can use curl (but note, it won't redirect, if you want to redirect you have to use solutions descrbed above), to send and receive variables from one server to another, just when receive make sure to save your variables somewhere(database)..