阻止来自其他网站PHP的file_get_contents

时间:2012-10-05 15:19:11

标签: php file-get-contents

  

可能重复:
  how to prevent PHP's file_get_contents( )

我想阻止来自其他网站的file_get_contents来保护我的内容不被其他网站窃取。 我找到了这样一个话题,但对我来说似乎不太清楚。 how to prevent PHP's file_get_contents( )

有什么帮助吗? 示例http://ddth.com。你不能file_get_contents("http://ddth.com");

1 个答案:

答案 0 :(得分:1)

您可以使用HTTP身份验证来保护

$login = 'root';
$pass = '12345';

if(($_SERVER['PHP_AUTH_PW']!= $pass || $_SERVER['PHP_AUTH_USER'] != $login)|| !$_SERVER['PHP_AUTH_USER'])
{
 header('WWW-Authenticate: Basic realm="Test auth"');
 header('HTTP/1.0 401 Unauthorized');
 echo 'Auth failed';
 exit;
}

else
{
 echo "ok";
 }

并且可以使用curl来阅读

    $username = 'user';
    $password = 'password';
    $ip = $_SERVER["SERVER_ADDR"];      
    $browser_id = "Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3";
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url); 
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
    curl_setopt($curl, CURLOPT_USERAGENT, $browser_id);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_TIMEOUT, 15);
    curl_setopt($curl, CURLOPT_REFERER, $ip);       
    $retValue = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);

要提取信息,请在此处here