PHP uses CURL to realize the method of simulating login to the website with verification code

  • 2021-07-09 07:37:33
  • OfStack

Many simulated login programs on the Internet, Mostly through the service program apache and so on, After obtaining the verification code, it will be displayed on the webpage, and then filled in and then POST will go out. Although it looks friendly, since the simulated login, what you do after login will not be completed in a short time, so it will be limited by the maximum execution time of php, and some operations may have insufficient permissions.

This paper provides a program example. The idea is to store the verification code as a picture after obtaining the verification code, and then sleep for 20 seconds. After 20 seconds, the user manually views the picture and fills the verification code into the code.txt file. After 20 seconds sleep, the program will read the verification code of code.txt, and then log in with the verification code. The specific code is as follows:


/**
 *  Simulated login 
 */

// Initialization variable 
$cookie_file = "tmp.cookie";
$login_url = "http://xxx.com/logon.php";
$verify_code_url = "http://xxx.com/verifyCode.php";

echo " Getting COOKIE...\n";
$curlj = curl_init();
$timeout = 5;
curl_setopt($curl, CURLOPT_URL, $login_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($curl,CURLOPT_COOKIEJAR,$cookie_file); // Get COOKIE And store 
$contents = curl_exec($curl);
curl_close($curl);

echo "COOKIE Getting complete, fetching verification code ...\n";
// Take out the verification code 
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $verify_code_url);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$img = curl_exec($curl);
curl_close($curl);

$fp = fopen("verifyCode.jpg","w");
fwrite($fp,$img);
fclose($fp);
echo " The verification code has been taken out and is sleeping. 20 Please fill in the verification code within seconds code.txt And save \n";
// Stop running 20 Seconds 
sleep(20);

echo " Hibernation is completed, and the verification code is taken ...\n";
$code = file_get_contents("code.txt");
echo " The verification code was successfully taken out: $code\n";
echo " Preparing to impersonate login ...\n";

$post = "username=maben&pwd=hahahaha&verifycode=$code";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
$result=curl_exec($curl);
curl_close($curl);

// This 1 Block according to the data on the website obtained by its own capture package to make a judgment 
if(substr_count($result," Login Successful ")){
 echo " Login Successful \n";
}else{
 echo " Login failed \n";
 exit;
}

//OK Start doing what you want to do. . . . . 


Related articles: