7

I've got some file on www.aaa.com/file.php

I've got another file on www.saveme.com/file2.php

And inside this 2nd one I call 1st one this way file_get_contents('www.aaa.com/file.php');

I want www.aaa.com/file.php to get url of site that has called it.

Is it possible?

www.lol.com calls www.aaa.com/file.php -> it gets www.lol.pl

www.another.com calls it -> it gets www.another.com

Something like 'who've called me from another server and what server was it?'

3 Answers 3

18

Actually, no, as it isn't as obvious as Captain Payalytic said it was, here is how to do it.

$referer = "http://abc.info";
$opts = array(
       'http'=>array(
           'header'=>array("Referer: $referer\r\n")
       )
);
$context = stream_context_create($opts);
file_put_contents($img, file_get_contents($node_img, false, $context)); 

Source: http://thuannvn.blogspot.be/2011/12/how-to-set-referer-in-php.html

After getting a headache on the official doc, not saying a word about referers, I finally did a classic search on the web and immediately found this. Hooray!

EDIT: The doc says header should be a string though ( https://www.php.net/manual/en/context.http.php ). One should check if it works with an array too.

Alternatively you should also be able to retrieve the domain's name from the IP of the request received. (Not always though)

You could also use curl too.

1
  • 1
    Responding to your edit: header works as both an array or a string. (I tested both ways.) As a string you would type 'header'=>"Referer: $referer\r\n" which is slightly shorter than your array code.
    – Eric Klien
    Jul 17, 2015 at 10:10
-1

You need to pass the referrer as an argument to file_get_contents:

file_get_contents('http://www.aaa.com/file.php?referrer='.urlencode($referrer));

It is not possible for the second server to get the original referrer otherwise.

2
  • yes, since file_get_contents launch a new HTTP session, it cannot inherit the original referrer.
    – Knaģis
    Mar 21, 2013 at 12:41
  • This code doesn't work as a general solution. (This code only works if you have control of both the source and destination URLs.) The solution provided by JeromeJ does work for all cases. I needed to determine if a site would allow you to access images there if the referer was not the same as the site's domain. JeromeJ's code enabled me to do this.
    – Eric Klien
    Jul 17, 2015 at 9:56
-2

See http://php.net/manual/en/function.file-get-contents.php where you will find out about get_headers() and $http_response_header.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.