PHP obtains cookie simulated login implementation method

WBOY
Release: 2016-07-25 08:53:12
Original
1281 people have browsed it
  1. $cookie_jar = dirname(__file__)."/pic.cookie";
Copy code

2. Get cookies Save cookies to file

  1. $url = "http://1.2.3.4/";
  2. $ch = curl_init();
  3. curl_setopt($ch, curlopt_url, $url);
  4. curl_setopt($ch , curlopt_header, 0);
  5. curl_setopt($ch, curlopt_returntransfer, true);
  6. curl_setopt($ch, curlopt_cookiejar, $cookie_jar);
  7. $content = curl_exec($ch);
  8. curl_close($ch);
Copy Code

3. Simulate browser to obtain verification code The server verification code has loopholes and you can specify it yourself. Take out the cookie and submit it to the server together, so that the server thinks it is the browser opening the login page.

  1. $ch = curl_init();
  2. curl_setopt($ch, curlopt_url, 'http://1.2.3.4/getcheckpic.action?rand=6836.185874812305');
  3. curl_setopt($ch , curlopt_cookiefile, $cookie_jar);
  4. curl_setopt($ch, curlopt_header, 0);
  5. curl_setopt($ch, curlopt_returntransfer, 1);
  6. $ret = curl_exec($ch); // bbs.it-home.org
  7. curl_close ($ch);
Copy code

4. Post submission

  1. $post = "name=2&usertype=1&passwd=asdf&logintype=1&rand=6836&imagefield.x=25&imagefield.y=7";
  2. $ch = curl_init();
  3. curl_setopt($ch, curlopt_url, "http://1.2.3.4/loginstudent.action");
  4. curl_setopt($ch, curlopt_header, false);
  5. curl_setopt($ch, curlopt_returntransfer,1);
  6. curl_setopt($ch, curlopt_postfields, $post) ;
  7. curl_setopt($ch, curlopt_cookiefile, $cookie_jar);
  8. $result=curl_exec($ch);
  9. curl_close($ch);
Copy code

5. Get data from the specified page

  1. $ch = curl_init();
  2. curl_setopt($ch, curlopt_url, "http://1.2.3.4/accountcarduser.action");
  3. curl_setopt($ch, curlopt_header, false );
  4. curl_setopt($ch, curlopt_header, 0);
  5. curl_setopt($ch, curlopt_returntransfer,0);
  6. curl_setopt($ch, curlopt_cookiefile, $cookie_jar);
  7. $html=curl_exec($ch);
  8. // var_dump ($html);
  9. curl_close($ch);
Copy code

The above introduces the method of php curl to simulate login by obtaining cookies, and several examples of php simulated login. I hope it will be helpful to everyone.

>>> More php simulated login articles, topic links: php simulated login php curl simulated login tutorials



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