Detect Domain Change? - php

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.

Related

How to check where a user opened a url in PHP without $_SERVER['HTTP_REFERER']

I'm trying to check whether the user has moved from a specific site.
But this site doesn't send $_SERVER['HTTP_REFERER'], so I can't use it.
Maybe some one knows, how to realise this:
1) User navigates to my site from example.com (no HTTP_REFERER)
2) When user loads my page, I need to check where this user is from.
Can this be done without the HTTP_REFERER header?
I answer IT.
I will use API on referring site.
Thanks!

Need tp find Request source URL in PHP

I want to determine the website from which the request to the particular website has been called. For Example..I have a website www.ex.com. Now this website link has been there on three websites www.a.com, www.b.com, and www.c.com. Suppose one user has clicked the link from www.a.com to go to www.ex.com. Now I want to determine that the request has come from the www.a.com and then the page in www.ex.com will be displayed accordingly. Similarly if the user has clicked the www.ex.com link from www.b.com then the page will be displayed accordingly.
So how can I determine this request source, means from which website www.ex.com has been called? It is good if you will explain this using code in PHP.
Thanks
Look here for HTTP_HOST and HTTP_REFERER.
Since both global variables are populated from HTTP headers, look also here for Host and Referer.
That's how you use them:
$host = $_SERVER['HTTP_HOST'];
$referer = $_SERVER['HTTP_REFERER'];
The 1st is the original host name (like 'www.a.com') referring to your page. The 2nd is the full URL (like https://www.a.com/some/path/to/some.html).
Then, based on some conditional logic, you may return to your users appropriate HTML.
There are two different websites.
Say from a.com, you click on ex.com
So, for the link in a.com, add some parameter:
Click Here
Now, your from parameter will be
1) a.com
2) b.com
3) c.com
If none of the above, then its clicked from ex.com itself.
You can also add some encryption for this link to add security.
You can use this
$_SERVER['HTTP_REFERER']
for referral url

Redirect, but hide referrer

Lets say I have a URL, www.mysite.com/go that I want to redirect to www.anothersite.com/site.php?id=999
The trick is, I do not want anothersite.com to be able to see that the request came from mysite.com. It should look like the address www.anothersite.com/site.php?id=999 was typed into the addressbar manually by the user.
It is important to note, that this has nothing to do with Google Analytics, and there will never exist an anchor link to www.mysite.com/go anywhere. Instead, the user will manually input www.mysite.com/go in the address bar (which is easier to remembar than the long URL).
How is this achieved? The technology in question is PHP. I imagine that it can be achieved with the header() function, but google searches reveal that this only works with https, not http. Can I via PHP control what the client provides of referrel information when the redirect is performed? I guess that if I want it to look like the address was typed into the address bar, I would have to blank out the referrer information. Is it possible?
It's not possible by means of a HTTP Redirect. You don't have any control over the outgoing referrer header as the browser handles it entirely client-side.
Your only real option that you can directly control is to use HTTPS. Referrers with a value of a HTTPS page are not carried forward by browsers.
Example flow:
http://www.mysite.com/go (so any existing links don't have to change)
https://www.mysite.com/go
http://www.anothersite.com/site.php?id=999

PHP get URL of page iframe is located on inside the iframe

I've been looking around on the web and I can't find a solution to my issue.
Lets say on the following page I have an iframe.
http://www.mysite.com/cakes.php - this is the url of the page the iframe is on
This is the iframe code on that page
<iframe src = "http://www.mysite.com/secret_frame.php"></iframe>
In the iframe (the file secret.php), how would I get the URL of the page the iframe is situated on, so in this case it would be http://www.mysite.com/cakes.php.
Thanks
Frank
The iframed page, secret_frame.php, can access the parent-page including it through the $_SERVER['HTTP_REFERER'] variable.
As mentioned in #Sven's answer, you can't completely trust the Referrer sent to the page though. Directly from the manual:
The address of the page (if any) which referred the user agent to the
current page. This is set by the user agent. Not all user agents will
set this, and some provide the ability to modify HTTP_REFERER as a
feature. In short, it cannot really be trusted.
It would usually be in the referrer - but remember referrers cannot be trusted, they might be changed or missing alltogether.

PHP 301 Redirect - collect referrer

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).

Categories