【问题标题】:CURLOPT_FOLLOWLOCATION cannot be activated [duplicate]无法激活 CURLOPT_FOLLOWLOCATION [重复]
【发布时间】:2011-10-18 14:49:45
【问题描述】:

所以我在多台服务器上不断收到这个烦人的错误(这是一个警告,所以我会忽略它,但我需要这个功能)

警告:curl_setopt() [function.curl-setopt]:启用安全模式或在 /home/xxx/public_html/xxx.php 第 56 行设置 open_basedir 时无法激活 CURLOPT_FOLLOWLOCATION

我将如何通过 SSH 解决此问题?

【问题讨论】:

  • 您的设置是什么(安全模式和/或启用了 open basedir?)以及您想要实现什么?

标签: curl php


【解决方案1】:

在您的 php.ini 文件中设置safe_mode = Off(它通常位于服务器上的 /etc/ 中)。如果已经关闭,则在 php.ini 文件中查找 open_basedir 内容,并相应地进行更改。

基本上,作为一种安全措施,关注位置选项已被禁用,但 PHP 的内置安全功能通常比安全更烦人。其实safe_modeis deprecated in PHP 5.3。

【讨论】:

  • @Flambine:Deprecated 表示它仍然存在并且按预期工作,但强烈建议不要这样做。它可以而且经常在较旧的共享托管平台上启用。我同意 safe_mode 充其量是烦人的。
  • @Wrikken:你是对的,对不起。但我相信,在 PHP 6 中 safe_mode 将完全消失。并不是说现在有任何帮助。尽管如此,我还是把两者搞混了。
  • 顺便说一句:根据php.net/releases/NEWS_5_4_0_alpha1.txt PHP 5.4 中删除了 safe_mode
  • 如果您在共享服务器上,这不是能够修改 php.ini 的选项,还有其他解决方案吗?
  • @JosephAstrahan 首先,我想说找一个更好的托管服务提供商。但是another answer to this question 有一个可能的解决方法。不过,不知道它是否会起作用
【解决方案2】:

简单地说,只要在 php.ini 文件中启用了open_basedir 或safe_mode,就不能使用 CURLOPT_FOLLOWLOCATION 配置。要更改这些设置,我只能给出一般说明:

  1. SSH 到服务器
  2. cd 到包含 php.ini 的目录(在 Linux 上通常是 /etc/php5,取决于您的发行版或操作系统)
  3. sudo 进行编辑(例如,sudo nano php.ini)。
  4. 编辑指定 open_basedir 或 safe_mode 的行并关闭它们。

之后记得重启你的httpd!

【讨论】:

    【解决方案3】:
    【解决方案4】:

    试试这个,如果需要重定向并且启用了安全模式,它将跟随基于标题的链接(如果您抓取图像虽然这将不起作用,因为它将标题添加到返回中),这是您的解决方法具体问题,当客户安装我的一个脚本时,我遇到了同样的问题,所以不得不想出这个..它还会将错误记录到:curl.error.log..有用的eh

    <?php 
    function geturl($url) {
        (function_exists('curl_init')) ? '' : die('cURL Must be installed for geturl function to work. Ask your host to enable it or uncomment extension=php_curl.dll in php.ini');
    
        $curl = curl_init();
        $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
        $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
        $header[] = "Cache-Control: max-age=0";
        $header[] = "Connection: keep-alive";
        $header[] = "Keep-Alive: 300";
        $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
        $header[] = "Accept-Language: en-us,en;q=0.5";
        $header[] = "Pragma: ";
    
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0 Firefox/5.0');
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_HEADER, true);
        curl_setopt($curl, CURLOPT_REFERER, $url);
        curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
        curl_setopt($curl, CURLOPT_AUTOREFERER, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        //curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); //CURLOPT_FOLLOWLOCATION Disabled...
        curl_setopt($curl, CURLOPT_TIMEOUT, 60);
    
        $html = curl_exec($curl);
    
        $status = curl_getinfo($curl);
        curl_close($curl);
    
        if ($status['http_code'] != 200) {
            if ($status['http_code'] == 301 || $status['http_code'] == 302) {
                list($header) = explode("\r\n\r\n", $html, 2);
                $matches = array();
                preg_match("/(Location:|URI:)[^(\n)]*/", $header, $matches);
                $url = trim(str_replace($matches[1],"",$matches[0]));
                $url_parsed = parse_url($url);
                return isset($url_parsed) ? geturl($url) : '';
            }
    
            $oline='';
            foreach ($status as $key => $eline) {
                $oline .= '['.$key.']'.$eline.' ';
            }
            $line = $oline." \r\n ".$url."\r\n-----------------\r\n";
    
            $handle = @fopen('./curl.error.log', 'a');
            fwrite($handle, $line);
            return false;
        }
        return $html;
    }
    

    【讨论】:

    • 您“可能”不想添加检查新 url 不是 file:// url。这可能会暴露“file:///myproject/database.php”等的内容。自 7.19.4 以来默认情况下 cURL 不允许这样做(请参阅 CURLOPT_REDIR_PROTOCOLS)。低版本的 curl 没有这个限制,这就是为什么 CURLOPT_FOLLOWLOCATION 在安全模式下是不允许的。
    • 未定义变量:referer
    • @2astalavista 感谢修复
    • 很好,我正在谷歌上搜索这样的解决方法!
    • 非常感谢您。我试图使用benalman.com/projects/php-simple-proxy 和//curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); //CURLOPT_FOLLOWLOCATION Disabled... 是在服务器上的方式。
    【解决方案5】:

    要解决这个问题,只需在php.ini文件中输入safe_mode = Off并清除open_base_dir即可。

    【讨论】:

    • 如果您在无法访问 php.ini 的共享服务器上,这不是一个选项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 2021-12-18
    • 2012-05-17
    • 2023-03-07
    相关资源
    最近更新 更多