Strange redirecting problem when attempting to login using curl
Trying to login using curl on this site: http://www.indianvideogamer.com.
This is my complete script:
$ref = "http://www.indianvideogamer.com/" ;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.indianvideogamer.com/forums/index.php?app=core&module=global&section=login&do=process") ; // Target site
curl_setopt ($ch, CURLOPT_COOKIEFILE, "C:/cookie2-techenclave.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "C:/cookie2-techenclave.txt");
curl_setopt($ch, CURLOPT_REFERER, $ref);
curl_setopt($ch, CURLOPT_TIMEOUT, CURL_TIMEOUT); // Timeout
curl_setopt($ch, CURLOPT_USERAGENT, WEBBOT开发者_运维百科_NAME);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "referer=http://www.indianvideogamer.com/forums/index.php&username=cute.bandar&password=mypass");
// curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // Follow redirects
curl_setopt($ch, CURLOPT_MAXREDIRS, 4); // Limit redirections to four
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Return in string
$webpage['FILE'] = curl_exec($ch);
curl_close($ch);
echo $webpage['FILE'] ;
the result of running this script on localhost/script.php is that instead of echoing the page it redirects to http://www.localhost/script.php . Why ?
I admit it's not an obvious one.
But finally I made it working. I skipped the URL encoding of ampersand signs in the URL argument. So my line was:
curl_setopt($ch, CURLOPT_URL,"http://www.indianvideogamer.com/forums/index.php?app=core&module=global§ion=login&do=process") ;
BTW, are you sure it's the whole script? My PHP complains about lack of definition of CURL_TIMEOUT
and WEBBOT_NAME
, but that doesn't seem to affect the login.
Try using
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
This will follow redirects.
精彩评论