Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get referer inside php file_get_contents

Tags:

url

php

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?'

like image 302
OPOPO Avatar asked Mar 21 '13 12:03

OPOPO


1 Answers

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 ( http://be2.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.

like image 59
jeromej Avatar answered Oct 20 '22 01:10

jeromej