Home > Backend Development > PHP Tutorial > PHP uses CURL to simulate login method, phpcurl simulates login_PHP tutorial

PHP uses CURL to simulate login method, phpcurl simulates login_PHP tutorial

WBOY
Release: 2016-07-13 09:47:00
Original
943 people have browsed it

PHP uses CURL to simulate login, phpcurl simulates login

Many simulated login programs on the Internet are mostly run through service programs such as apache, and are displayed after obtaining the verification code. On the web page, fill it in and then POST it out. Although it looks very friendly, since the login is simulated, what you do after logging in is not necessarily completed in a short time, so this is subject to the maximum execution time of PHP. Moreover, some operations may have insufficient permissions.

This article provides a program example. The idea is to obtain the verification code and store the verification code as an image. Then the program sleeps for 20 seconds. After 20 seconds, the user manually views the image and fills in the verification code into code.txt. In the file, after the 20-second hibernation is completed, the program will read the verification code of code.txt, and then use the verification code to log in. The specific code is as follows:

/**
 * 模拟登录
 */
 
//初始化变量
$cookie_file = "tmp.cookie";
$login_url = "http://xxx.com/logon.php";
$verify_code_url = "http://xxx.com/verifyCode.php";
 
echo "正在获取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); //获取COOKIE并存储
$contents = curl_exec($curl);
curl_close($curl);
 
echo "COOKIE获取完成,正在取验证码...\n";
//取出验证码
$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 "验证码取出完成,正在休眠,20秒内请把验证码填入code.txt并保存\n";
//停止运行20秒
sleep(20);
 
echo "休眠完成,开始取验证码...\n";
$code = file_get_contents("code.txt");
echo "验证码成功取出:$code\n";
echo "正在准备模拟登录...\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);
 
//这一块根据自己抓包获取到的网站上的数据来做判断
if(substr_count($result,"登录成功")){
 echo "登录成功\n";
}else{
 echo "登录失败\n";
 exit;
}
 
//OK,开始做你想做的事吧。。。。。
 
Copy after login

The above is the entire content of this article, I hope you all like it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1028972.htmlTechArticlePHP uses CURL to simulate login. phpcurl simulates login to many simulated login programs on the Internet, most of which are through the service program apache After getting the verification code, it will be displayed on the webpage...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template