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";
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);
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);
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);
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);
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!