PHP obtains Cookie to implement simulated login code

小云云
Release: 2023-03-20 15:22:02
Original
2165 people have browsed it

This article mainly shares with you PHP to obtain cookies to implement simulated login code. I hope it can help everyone.

1. Define Cookie storage path

Must use absolute path

$cookie_jar = dirname(__FILE__)."/pic.cookie";
Copy after login

2. Get Cookie

Save cookie to file

$url = "http://1.2.3.4/";$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);$content = curl_exec($ch);
curl_close($ch);
Copy after login

3. Simulate the browser to obtain the verification code

The server verification code has a loophole. You can specify

to take out the cookie and submit it to the server together, so that the server thinks it is browsing Open the login page

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://1.2.3.4/getCheckpic.action?rand=6836.185874812305');
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);$ret = curl_exec($ch);
curl_close($ch);
Copy after login

4. POST submission

$post = "name=2&userType=1&passwd=asdf&loginType=1&rand=6836&imageField.x=25&imageField.y=7";    
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://1.2.3.4/loginstudent.action");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);$result=curl_exec($ch);
curl_close($ch);
Copy after login

5. Get data from the specified page

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://1.2.3.4/accountcardUser.action");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,0);        
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);$html=curl_exec($ch);// var_dump($html);curl_close($ch);
Copy after login

Related recommendations:

PHP is simple Example sharing of simulated login function

A small program to simulate login to the educational system to calculate GPA

PHP simulates login and obtains data

The above is the detailed content of PHP obtains Cookie to implement simulated login code. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!