Click here to get back home

CURL redirect not working even with CURLOPT_FOLLOWLOCATION set to true

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
CURL redirect not working even with CURLOPT_FOLLOWLOCATION set to true Sarah 07-24-2008
Posted by Sarah on July 24, 2008, 5:30 pm
Please log in for more thread options
I was wondering if someone might be able to help me with this issue.
I have a feeling this has something to do with my host's server
settings as I used to be able to get CURL to follow redirects by
setting CURLOPT_FOLLOWLOCATION set to true. I had a problem with my
host's updating something in the past that gave me the error
"CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an
open_basedir is set". My host worked on the issue and the error went
away, but I still can't seem to follow redirects, but I don't receive
the error. When I turn on the headers it show the location redirect,
but it won't follow it. Any ideas or is this an issue again with my
host? One code example of this issue is below. I have been able to
manually redirect to a point, but it would be a lot easier if the CURL
option worked. Thanks for any help you can provide.


<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U;
Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
curl_setopt($ch, CURLOPT_URL, "http://www.flickr.com/signin/");
curl_setopt($ch, CURLOPT_REFERER, "http://www.flickr.com/");
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type:
application/x-www-form-urlencoded"));
//curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_exec ($ch);

curl_close($ch);
?>

Posted by Jerry Stuckle on July 25, 2008, 7:10 am
Please log in for more thread options
Sarah wrote:
> I was wondering if someone might be able to help me with this issue.
> I have a feeling this has something to do with my host's server
> settings as I used to be able to get CURL to follow redirects by
> setting CURLOPT_FOLLOWLOCATION set to true. I had a problem with my
> host's updating something in the past that gave me the error
> "CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an
> open_basedir is set". My host worked on the issue and the error went
> away, but I still can't seem to follow redirects, but I don't receive
> the error. When I turn on the headers it show the location redirect,
> but it won't follow it. Any ideas or is this an issue again with my
> host? One code example of this issue is below. I have been able to
> manually redirect to a point, but it would be a lot easier if the CURL
> option worked. Thanks for any help you can provide.
>
>
> <?php
> $ch = curl_init();
> curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
> curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
> curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U;
> Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
> curl_setopt($ch, CURLOPT_URL, "http://www.flickr.com/signin/");
> curl_setopt($ch, CURLOPT_REFERER, "http://www.flickr.com/");
> curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type:
> application/x-www-form-urlencoded"));
> //curl_setopt($ch, CURLOPT_HEADER, TRUE);
> curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
> curl_exec ($ch);
>
> curl_close($ch);
> ?>

Did the error go away, or did they just hide it?

Look at phpinfo(). Is either safe_mode or open_basedir set?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Posted by Sarah on July 26, 2008, 11:47 am
Please log in for more thread options
> Sarah wrote:
> > I was wondering if someone might be able to help me with this issue.
> > I have a feeling this has something to do with my host's server
> > settings as I used to be able to get CURL to follow redirects by
> > setting CURLOPT_FOLLOWLOCATION set to true. I had a problem with my
> > host's updating something in the past that gave me the error
> > "CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an
> > open_basedir is set". My host worked on the issue and the error went
> > away, but I still can't seem to follow redirects, but I don't receive
> > the error. When I turn on the headers it show the location redirect,
> > but it won't follow it. Any ideas or is this an issue again with my
> > host? One code example of this issue is below. I have been able to
> > manually redirect to a point, but it would be a lot easier if the CURL
> > option worked. Thanks for any help you can provide.
>
> > <?php
> > $ch = curl_init();
> > curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
> > curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
> > curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U;
> > Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
> > curl_setopt($ch, CURLOPT_URL, "http://www.flickr.com/signin/");
> > curl_setopt($ch, CURLOPT_REFERER, "http://www.flickr.com/");
> > curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type:
> > application/x-www-form-urlencoded"));
> > //curl_setopt($ch, CURLOPT_HEADER, TRUE);
> > curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
> > curl_exec ($ch);
>
> > curl_close($ch);
> > ?>
>
> Did the error go away, or did they just hide it?
>
> Look at phpinfo(). Is either safe_mode or open_basedir set?
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================

When I did phpinfo() is says safe mode is off, but there after
open_basedir is looks like it lists a path. When I mentioned to my
host that I thought that having the open_basedir set was the problem
they said replied with "I have again checked your curl redirect issue
and found that there is problem to the curl redirect code. You need
contact to your programmer, he will assist you in a better war
regarding this issue."

The code I used to show them that CURL redirects were not working is:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U;
Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
curl_setopt($ch, CURLOPT_URL, "http://www.flickr.com/signin/");
curl_setopt($ch, CURLOPT_REFERER, "http://www.flickr.com/");
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-
Type:application/x-www-form-urlencoded"));
//curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_exec ($ch);

curl_close($ch);

?>


They told me that they created a redirect page and it worked fine.
There code is:
<?php
// create a new CURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.flickr.com/");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

echo "<pre>";
print_r(curl_getinfo($ch,CURLINFO_EFFECTIVE_URL));
echo "</pre>";

// grab URL and pass it to the browser
curl_exec($ch);

// close CURL resource, and free up system resources
curl_close($ch);
?>


Problem with there code that I can tell is that they used the wrong
URL. http://www.flickr.com does not redirect it is http://www.flickr.com/signin/
that redirects. If they had used the correct URL shouldn't
CURINFO_EFFECTIVE_URL produce the new redirection URL which is
https://login.yahoo.com/config/login?.src=flickr&.pc=5134&.scrumb=0&.pd=c%3DE0.GahOp2e4MjkX.5l2HgAoLkpmyPvccpVM-&.intl=us&.done=https%3A%2F%2Flogin.yahoo.com%2Fconfig%2Fvalidate%3F.src%3Dflickr%26.pc%3D5134%26.scrumb%3D0%26.pd%3Dc%253DE0.GahOp2e4MjkX.5l2HgAoLkpmyPvccpVM-%26.intl%3Dus%26.done%3Dhttp%253A%252F%252Fwww.flickr.com%252Fsignin%252Fyahoo%252F
and not http://www.flickr.com/signin?

If I did something wrong in my code to not properly form it to allow
redirects I would be happy to take the blame, but as far as I can see
I didn't and the only think I can think that would be causing this is
that they have the open_basedir set to a path instead of it being null
like it used to be.

Any help or advice to make sure I am not losing my mind would be
greatly appreciated.

Posted by Jerry Stuckle on July 26, 2008, 4:10 pm
Please log in for more thread options
Sarah wrote:
>> Sarah wrote:
>>> I was wondering if someone might be able to help me with this issue.
>>> I have a feeling this has something to do with my host's server
>>> settings as I used to be able to get CURL to follow redirects by
>>> setting CURLOPT_FOLLOWLOCATION set to true. I had a problem with my
>>> host's updating something in the past that gave me the error
>>> "CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an
>>> open_basedir is set". My host worked on the issue and the error went
>>> away, but I still can't seem to follow redirects, but I don't receive
>>> the error. When I turn on the headers it show the location redirect,
>>> but it won't follow it. Any ideas or is this an issue again with my
>>> host? One code example of this issue is below. I have been able to
>>> manually redirect to a point, but it would be a lot easier if the CURL
>>> option worked. Thanks for any help you can provide.
>>> <?php
>>> $ch = curl_init();
>>> curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
>>> curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
>>> curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U;
>>> Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
>>> curl_setopt($ch, CURLOPT_URL, "http://www.flickr.com/signin/");
>>> curl_setopt($ch, CURLOPT_REFERER, "http://www.flickr.com/");
>>> curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type:
>>> application/x-www-form-urlencoded"));
>>> //curl_setopt($ch, CURLOPT_HEADER, TRUE);
>>> curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
>>> curl_exec ($ch);
>>> curl_close($ch);
>>> ?>
>> Did the error go away, or did they just hide it?
>>
>> Look at phpinfo(). Is either safe_mode or open_basedir set?
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> ==================
>
> When I did phpinfo() is says safe mode is off, but there after
> open_basedir is looks like it lists a path. When I mentioned to my
> host that I thought that having the open_basedir set was the problem
> they said replied with "I have again checked your curl redirect issue
> and found that there is problem to the curl redirect code. You need
> contact to your programmer, he will assist you in a better war
> regarding this issue."
>
> The code I used to show them that CURL redirects were not working is:
>
> <?php
> $ch = curl_init();
> curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
> curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
> curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U;
> Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
> curl_setopt($ch, CURLOPT_URL, "http://www.flickr.com/signin/");
> curl_setopt($ch, CURLOPT_REFERER, "http://www.flickr.com/");
> curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-
> Type:application/x-www-form-urlencoded"));
> //curl_setopt($ch, CURLOPT_HEADER, TRUE);
> curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
> curl_exec ($ch);
>
> curl_close($ch);
>
> ?>
>
>
> They told me that they created a redirect page and it worked fine.
> There code is:
> <?php
> // create a new CURL resource
> $ch = curl_init();
>
> // set URL and other appropriate options
> curl_setopt($ch, CURLOPT_URL, "http://www.flickr.com/");
> curl_setopt($ch, CURLOPT_HEADER, false);
> curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
>
> echo "<pre>";
> print_r(curl_getinfo($ch,CURLINFO_EFFECTIVE_URL));
> echo "</pre>";
>
> // grab URL and pass it to the browser
> curl_exec($ch);
>
> // close CURL resource, and free up system resources
> curl_close($ch);
> ?>
>
>
> Problem with there code that I can tell is that they used the wrong
> URL. http://www.flickr.com does not redirect it is
http://www.flickr.com/signin/
> that redirects. If they had used the correct URL shouldn't
> CURINFO_EFFECTIVE_URL produce the new redirection URL which is
>
https://login.yahoo.com/config/login?.src=flickr&.pc=5134&.scrumb=0&.pd=c%3DE0.GahOp2e4MjkX.5l2HgAoLkpmyPvccpVM-&.intl=us&.done=https%3A%2F%2Flogin.yahoo.com%2Fconfig%2Fvalidate%3F.src%3Dflickr%26.pc%3D5134%26.scrumb%3D0%26.pd%3Dc%253DE0.GahOp2e4MjkX.5l2HgAoLkpmyPvccpVM-%26.intl%3Dus%26.done%3Dhttp%253A%252F%252Fwww.flickr.com%252Fsignin%252Fyahoo%252F
> and not http://www.flickr.com/signin?
>
> If I did something wrong in my code to not properly form it to allow
> redirects I would be happy to take the blame, but as far as I can see
> I didn't and the only think I can think that would be causing this is
> that they have the open_basedir set to a path instead of it being null
> like it used to be.
>
> Any help or advice to make sure I am not losing my mind would be
> greatly appreciated.
>

So if the url they used doesn't direct, it's not a valid test, is it?

You have the error message. You have the phpinfo(), which verifies what
the error message says.

But you probably won't get them to turn off open_basedir for security
reasons.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================


Similar ThreadsPosted
CURL and PHP Redirect March 16, 2006, 7:30 pm
Use curl to find redirect url of http://www.disney.com June 1, 2005, 3:31 pm
cURL not always working August 11, 2006, 11:24 am
cURL + OpenSSL + PHP not working April 1, 2005, 9:13 am
SSL curl connection not working April 18, 2007, 11:23 am
Re: PHP XML-XML curl function not working. Please help April 11, 2008, 8:37 am
CURL Problem.. Comparing output from IE/CURL differs November 7, 2005, 1:51 pm
redirect / new website how to redirect old (google) links to new site ? November 7, 2006, 3:22 am
redirect to directory - php redirect or htaccess? June 25, 2006, 7:17 pm
why does this if () always evaluate to true? September 6, 2004, 7:31 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap