Keywords: CURL Cookie CURLOPT_COOKIEJAR CURLOPT_COOKIEFILE Simulation login PHP
Author: Fangbei Studio
Original text: http://www.cnblogs.com/txw1958/p/php-cookie-login.html
1. Define Cookie storage path
Must use absolute path
<span $cookie_jar</span> = <span dirname</span>(<span __FILE__</span>)."/pic.cookie";
2. Obtain Cookie
Save cookies to file
<span $url</span> = "http://1.2.3.4/"<span ; </span><span $ch</span> =<span curl_init(); curl_setopt(</span><span $ch</span>, CURLOPT_URL, <span $url</span><span ); curl_setopt(</span><span $ch</span>, CURLOPT_HEADER, 0<span ); curl_setopt(</span><span $ch</span>, CURLOPT_RETURNTRANSFER, <span true</span><span ); curl_setopt(</span><span $ch</span>, CURLOPT_COOKIEJAR, <span $cookie_jar</span><span ); </span><span $content</span> = curl_exec(<span $ch</span><span ); curl_close(</span><span $ch</span>);
3. Simulate the browser to obtain the verification code
The server verification code has loopholes, you can specify it yourself
Take out the cookie and submit it to the server together, making the server think it is a browser opening the login page
<span $ch</span> =<span curl_init(); curl_setopt(</span><span $ch</span>, CURLOPT_URL, 'http://1.2.3.4/getCheckpic.action?rand=6836.185874812305'<span ); curl_setopt(</span><span $ch</span>, CURLOPT_COOKIEFILE, <span $cookie_jar</span><span ); curl_setopt(</span><span $ch</span>, CURLOPT_HEADER, 0<span ); curl_setopt(</span><span $ch</span>, CURLOPT_RETURNTRANSFER, 1<span ); </span><span $ret</span> = curl_exec(<span $ch</span><span ); curl_close(</span><span $ch</span>);
4. POST submission
<span $post</span> = "name=2&userType=1&passwd=asdf&loginType=1&rand=6836&imageField.x=25&imageField.y=7"<span ; </span><span $ch</span> =<span curl_init(); curl_setopt(</span><span $ch</span>, CURLOPT_URL, "http://1.2.3.4/loginstudent.action"<span ); curl_setopt(</span><span $ch</span>, CURLOPT_HEADER, <span false</span><span ); curl_setopt(</span><span $ch</span>, CURLOPT_RETURNTRANSFER,1<span ); curl_setopt(</span><span $ch</span>, CURLOPT_POSTFIELDS, <span $post</span><span ); curl_setopt(</span><span $ch</span>, CURLOPT_COOKIEFILE, <span $cookie_jar</span><span ); </span><span $result</span>=curl_exec(<span $ch</span><span ); curl_close(</span><span $ch</span>);
5. Go to the designated page to obtain data
<span $ch</span> =<span curl_init(); curl_setopt(</span><span $ch</span>, CURLOPT_URL, "http://1.2.3.4/accountcardUser.action"<span ); curl_setopt(</span><span $ch</span>, CURLOPT_HEADER, <span false</span><span ); curl_setopt(</span><span $ch</span>, CURLOPT_HEADER, 0<span ); curl_setopt(</span><span $ch</span>, CURLOPT_RETURNTRANSFER,0<span ); curl_setopt(</span><span $ch</span>, CURLOPT_COOKIEFILE, <span $cookie_jar</span><span ); </span><span $html</span>=curl_exec(<span $ch</span><span ); </span><span //</span><span var_dump($html);</span> curl_close(<span $ch</span>);