Home > Backend Development > PHP Tutorial > PHP Gets Cookie to Simulate Login_PHP Tutorial

PHP Gets Cookie to Simulate Login_PHP Tutorial

WBOY
Release: 2016-07-20 11:16:06
Original
801 people have browsed it

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";
Copy after login

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>);
Copy after login

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>);
Copy after login

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>);
Copy after login

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>);
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/440144.htmlTechArticleKeywords: CURL CookieCURLOPT_COOKIEJARCURLOPT_COOKIEFILE Simulation login PHP Author: Fangbei Studio Original text: http://www.cnblogs. com/txw1958/p/php-cookie-login.html 1. Definition Co...
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