FOLLOWLOCATION and open_basedir



  • There's a mistake like using CURL.

    Warning: curl_setopt(): CURLOPT_FOLLOWLOCATION cannot be activated when an open_basedir is set in /var/www/*******/data/www/*******/script/Threads.php

    Clearly, the problem with some directive open_basedirbut you can't just turn it off for certain reasons. Modify. Is it possible to write an exception to php.ini?

    Or alternative solutions CURL FOLLOWLOCATION?



  • I'm suggesting redirecting not curl, but php code. This is an example of the $url data method:

    private function _Request($url, $timeout = 10, $max_redirects = 10)
    {
        $allow_url_fopen = strtolower(ini_get('allow_url_fopen'));
        $ua = 'Mozilla/5.0 (Windows NT 6.1; rv:48.0) Gecko/20100101 Firefox/48.0';
    
    // 1
    if (extension_loaded('curl'))
    {
        $ch = curl_init();
    
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_NOBODY, false);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_USERAGENT, $ua);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
        $content = curl_exec($ch);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
        curl_close($ch);
    
        if ($content !== false)
        {
            // переадресация
            if (($http_code == '301' || $http_code == '302') && $max_redirects > 0)
            {
                if (preg_match('%Location:\s(http[^\n\r]+)%i', $content, $matches))
                    return $this->_Request($matches['1'], $timeout, $max_redirects - 1);
            }
    
            // отдаем ответ
            else if ($http_code == '200')
            {
                $content_start = strpos($content, "\r\n\r\n");
                if ($content_start !== false)
                    return substr($content, $content_start + 4);
            }
    
            // прерываем выполнение
            else
                return false;
        }
    }
    
    // 2
    else if (function_exists('file_get_contents') && in_array($allow_url_fopen, array('on', 'true', '1')))
    {
        $stream_context = stream_context_create(
            array(
                'http' => array(
                    'method'        => 'GET',
                    'user_agent'    => $ua,
                    'max_redirects' => $max_redirects + 1,  // PHP >=5.1.0 only
                    'timeout'       => $timeout // PHP >=5.2.1 only
                )
            )
        );
    
        return file_get_contents($url, false, $stream_context);
    }
    
    return false;
    

    }


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2