Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
366 views
in Technique[技术] by (71.8m points)

php - CURLOPT_FOLLOWLOCATION cannot be activated

im having some problems with curls and i dont know how to solve them.

the idea is to get a user's username and pw and post it into an external webpage.

Here is the code:

$ch = curl_init(); 
  curl_setopt( $ch, CURLOPT_URL, "https://sso.uc.cl/cas/login?service=https://portaluc.puc.cl/uPortal/Login"); // URL to post 
  curl_setopt ($ch, CURLOPT_POST, 1);
  curl_setopt ($ch, CURLOPT_POSTFIELDS,         "username=$usuario&password=$pw");
  curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
  $result = curl_exec( $ch ); // runs the post 
  curl_close($ch);
  echo "Reply Response: " . $result; // echo reply response 

here is the error:

Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in /home/th000862/public_html/encuesta/login2.php on line 10

After that error, the user is not logged in into the external webpage.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That error means that your PHP configuration is prohibiting you from following the location. There are a few ways you could work around the problem without installing additional libraries as suggested by @mario.

  • If you own the server or have root access, you could change the php.ini file to disable "safe_mode".
  • You could also create a .htaccess file in your document root with php_value safe_mode off in it.
  • You may be able to add ini_set('safe_mode', false); in your PHP file.

If none of the above works, you could also do something along these lines:

$ch = curl_init('https://sso.uc.cl/cas/login?service=https://portaluc.puc.cl/uPortal/Login');

curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'username=' . urlencode($usuario) . '&password=' . urlencode($pw));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

$result = curl_exec($ch);

curl_close($ch);

// Look to see if there's a location header.
if ( ! empty($result) )
  if ( preg_match('/Location: (.+)/i', $result, $matches) )
  {
    // $matches[1] will contain the URL.
    // Perform another cURL request here to retrieve the content.
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...