tags:

views:

35

answers:

1

i got an error

CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in

i google many solution, but with this site they don't work. only need CURLOPT_FOLLOWLOCATION. stupid hoster don't want to enable safe_mode or an open_basedir . can i enable them by myself, may be create htaccess with some parameters?

A: 

The error means safe_mode or open_basedir ARE enabled (probably open_basedir) you probably can't override either if your host has any decent security settings.

So you have a choice

1) change host (not really desirable I imagine)

2) use a function similar to ones found on the php curl_setopt page, i.e. http://www.php.net/manual/en/function.curl-setopt.php#95027

The following is a fixed version of the function specified in point 2

function curl_redirect_exec($ch, &$redirects, $curlopt_header = false) {
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 301 || $http_code == 302) {
    list($header) = explode("\r\n\r\n", $data, 2);
    $matches = array();
    //this part has been changes from the original
    preg_match("/(Location:|URI:)[^(\n)]*/", $header, $matches);
    $url = trim(str_replace($matches[1],"",$matches[0]));
    //end changes
    $url_parsed = parse_url($url);
    if (isset($url_parsed)) {
        curl_setopt($ch, CURLOPT_URL, $url);
        $redirects++;
        return curl_redirect_exec($ch, $redirects);
    }
}
if ($curlopt_header)
    return $data;
else {
    list(,$body) = explode("\r\n\r\n", $data, 2);
    return $body;
}
}
Obsidian
funny, but script works well on my host (cheap and virtual), but boss use stupid host and don't have desire to change. about this function -i know it, when i start to write script i manually call each url, but last url don't wont to work, and only using CURLOPT_FOLLOWLOCATION helps me
kusanagi
Eek sorry I didn't actually test the function, you're right it doesn't work (it assumes the Location header isn't the last one) I've added a corrected version I tested to my answer.
Obsidian