在php file_get_contents中获取引用


Get referer inside php file_get_contents

我有一些关于www.aaa.com/file.php的文件

我在www.saveme.com/file2.php上有另一个文件

在第二个里面我这样称呼第一个file_get_contents('www.aaa.com/file.php');

我想让www.aaa.com/file.php得到网站的url已经调用了它。

有可能吗?

www.lol.com调用www.aaa.com/file.php ->得到www.lol.pl

www.another.com调用它->它得到www.another.com

像"谁从另一个服务器打电话给我,是哪个服务器?"'

事实上,不,因为这并不像Captain Payalytic所说的那么明显,这里是如何做到这一点。

$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)); 

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

在对官方文档感到头疼之后,对推荐人只字不提,我终于在网上做了一个经典的搜索,立即发现了这个。万岁!

EDIT:文档说header应该是一个字符串虽然(http://be2.php.net/manual/en/context.http.php)。我们应该检查它是否也适用于数组。

或者,您还应该能够从收到的请求的IP中检索域名。(并非总是如此)

你也可以使用curl

您需要将引用者作为参数传递给file_get_contents:

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

否则,第二个服务器不可能获得原始引用。

参见http://php.net/manual/en/function.file-get-contents.php,在那里您可以找到get_headers()和$http_response_header。