Home » Php » url – Get referer inside php file_get_contents

url – Get referer inside php file_get_contents

Posted by: admin July 12, 2020 Leave a comment

Questions:

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

How to&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.

Answer:

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.

Answer:

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