


PHP uses CURL to implement simulated login to a website with verification code, curl verification code_PHP tutorial
PHP uses CURL to implement a simulated login method for a website with a verification code, curl verification code
Many simulated login programs on the Internet are mostly run through the service program apache. After obtaining the verification code, it is displayed on the web page, and then filled in and then POSTed out. Although this looks very friendly, since the simulated login, 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, and some operations may not have enough 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,开始做你想做的事吧。。。。。
You can check to see if there are other http headers that are not simulated, such as whether Referer and User-Agent can simulate browser values. A complete request is similar to this:
GET /home/pack/ Data/Content? ID = 31,2399,13,30 & asyn = 1 & T = 0.03439752989200834 &_Req_seqid = 0xa982225F06378A HTTP/1.1
*/ *
Accept -Language: zh-cn
Referer: www.baidu. com/
x-requested-with: XMLHttpRequest
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; BTRS123401; InfoPath. 2; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; MS-RTC LM 8)
Host: www.baidu.com
Connection: Keep -Alive
Cookie: XCXXXXX
$url = "localhost/post_output.php";
$post_data = array (
"foo" => "bar",
"query" => "Nettuts",
"action " => "Submit"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// We are POSTing data!
curl_setopt($ch, CURLOPT_POST, 1);
// Add the post variable to
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
result

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
