【问题标题】:file_get_contents ($URL) returns "Permission Denied" errorfile_get_contents ($URL) 返回“权限被拒绝”错误
【发布时间】:2013-12-25 11:01:34
【问题描述】:

我解除了以下 PHP 代码以使我能够找到用户的位置信息,这将使我能够执行某些级别的限制。

但是,该代码在我的本地主机服务器上运行良好。但是当我将相同的 php 文件上传到我的远程 Web 服务器时,它会返回一个错误:

警告: 文件获取内容(http://api.codehelper.io/ips/?php&ip=192.168.1.1): 无法打开流:权限被拒绝 /home/www/xyberinternational.com/lotto247.biz/visitorlocation/userip/ip.codehelper.io.php 在第 41 行。

我在下面包含了以下文件及其代码。我该如何解决这个错误?

Index.php 文件

<?php
require_once("userip/ip.codehelper.io.php");
require_once("userip/php_fast_cache.php");
$_ip = new ip_codehelper();

$real_client_ip_address = $_ip->getRealIP();
$visitor_location       = $_ip->getLocation($real_client_ip_address);

$guest_ip   = $visitor_location['IP'];
$guest_country = $visitor_location['CountryName'];
$guest_city  = $visitor_location['CityName'];
$guest_state = $visitor_location['RegionName'];

echo "IP Address: ". $guest_ip. "<br/>";
echo "Country: ". $guest_country. "<br/>";
echo "State: ". $guest_state. "<br/>";
echo "City: ". $guest_city. "<br/>";


$ip             = $visitor_location['IP'];
$Continent_Code     = $visitor_location['ContinentCode'];
$Continent_Name     = $visitor_location['ContinentName'];
$Country_Code2      = $visitor_location['CountryCode2'];
$Country_Code3      = $visitor_location['CountryCode3'];
$Country        = $visitor_location['Country'];
$Country_Name       = $visitor_location['CountryName'];
$State_Name         = $visitor_location['RegionName'];
$City_Name      = $visitor_location['CityName'];
$City_Latitude      = $visitor_location['CityLatitude'];
$City_Longitude     = $visitor_location['CityLongitude'];
$Country_Latitude   = $visitor_location['CountryLatitude'];
$Country_Longitude  = $visitor_location['CountryLongitude'];
$Country_Longitude  = $visitor_location['CountryLongitude'];
$LocalTimeZone      = $visitor_location['LocalTimeZone'];
$Calling_Code       = $visitor_location['CallingCode'];
$Population     = $visitor_location['Population'];
$Area_SqKm      = $visitor_location['AreaSqKm'];
$Capital        = $visitor_location['Capital'];
$Electrical     = $visitor_location['Electrical'];
$Languages      = $visitor_location['Languages'];
$Currency       = $visitor_location['Currency'];
$Flag           = $visitor_location['Currency'];
?>

ip.coderhelper.io.php 文件

<?php
class ip_codehelper {
public function getRealIP() {
    $ipaddress = '';
    if(isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
        $ipaddress = $_SERVER['HTTP_CF_CONNECTING_IP'];
    } else if (isset($_SERVER['HTTP_X_REAL_IP'])) {
        $ipaddress = $_SERVER['HTTP_X_REAL_IP'];
    }
    else if (isset($_SERVER['HTTP_CLIENT_IP']))
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    else if(isset($_SERVER['HTTP_X_FORWARDED']))
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    else if(isset($_SERVER['HTTP_FORWARDED']))
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    else if(isset($_SERVER['REMOTE_ADDR']))
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    else
        $ipaddress = 'UNKNOWN';

    return $ipaddress;
}

public function getLocation($ip="") {
    if($ip == "") {
        $ip = $this->getRealIP();
    }
    if(!class_exists("phpFastCache")) {
        die("Please required phpFastCache Class");
    }
    // you should change this to cURL()
    $data = phpFastCache::get("codehelper_ip_".md5($ip));
    // caching 1 week


   if($data == null) {
        $url = "http://api.codehelper.io/ips/?callback=codehelper_ip_callback&ip=".$ip;
        $json = file_get_contents($url);
        $data = json_decode($json,true);
        phpFastCache::set("codehelper_ip_".md5($ip),$data,3600*24*7);
    }

    return $data;
}

public function SSLForwardJS() {
    $ip = $this->getRealIP();
    if(!class_exists("phpFastCache")) {
        die("Please required phpFastCache Class");
    }

    // you should change this to cURL()
    $data = phpFastCache::get("codehelper_ip_ssl".md5($ip));
    // caching 1 week
    if($data == null) {
        $url = "http://api.codehelper.io/ips/?callback=codehelper_ip_callback&ip=".$ip;
        $data = file_get_contents($url);

            phpFastCache::set("codehelper_ip_ssl".md5($ip),$data,3600*24*7);
        }
        return $data;
    }
}

同时,fastcatch.php 相当大。

【问题讨论】:

  • 尝试改用 cUrl (php.net/curl)。
  • 使用cURL 作为一些(大多数)主机阻止file_get_contents 的URL。这是一个安全的事情......
  • 在我的本地主机上,我启用了 curl.php.extension 并且它可以工作。但是,我在远程主机上做了同样的事情,但它仍然无法正常工作。

标签: php location ip file-get-contents access-denied


【解决方案1】:

这是你的问题:

    $url = "http://api.codehelper.io/ips/?callback=codehelper_ip_callback&ip=".$ip;
    $data = file_get_contents($url);

(靠近 ip.coderhelper.io.php 文件的底部)

这会引发错误,因为您尝试从中获取数据的服务器拒绝您访问该文件。你有几个选择:

1) 与 codehelper.io 工作人员交谈,看看您的服务器的 IP 是否被列入黑名单或一般范围是否(有时,取决于您从公司购买服务器的位置,将 IP 列入黑名单,因为已知它们会发送恶意攻击或请求。)。询问他们是否可以解决问题。

2)如果这不是一个可行的选择,您可以查看this post,第二个答案有一些棘手的工作。值得一试。

【讨论】:

  • 你看,如果我用我的本地主机运行这段代码,它工作正常。即使我将 $ip=127.0.1.1 更改为 $ip=80.44.93.12,它也可以正常工作。它列出了这个ip来自的国家。但是,当我将它上传到远程服务器时,它会生成一个错误......因为无法打开流。没有权限。请再看一遍。谢谢。
  • 看我刚才说的,远程服务器被codehelper.io阻塞了。 $ip 变量不是决定 403 错误的东西,数据发送的实际位置是。
猜你喜欢
  • 2018-07-28
  • 2014-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-19
  • 1970-01-01
  • 2012-04-26
  • 1970-01-01
相关资源
最近更新 更多