Home  >  Article  >  Backend Development  >  PHP simulated remote login sample code

PHP simulated remote login sample code

WBOY
WBOYOriginal
2016-07-25 08:53:38841browse
  1. !extension_loaded('curl') && die('the curl extension is not loaded.');
  2. $baseurl = 'http://127.0.0.1';//Root address
  3. $login_url = $baseurl .'/login.php?act=login'; //Login page address
  4. $get_url = $baseurl .'/index.php'; //Page that needs to be collected
  5. $post_fields = array( );
  6. //The following two items need to be modified
  7. $post_fields['name'] = 'admin';
  8. $post_fields['pass'] = '123456';
  9. //Get the form formhash
  10. $ch = curl_init( $login_url);
  11. curl_setopt($ch, curlopt_header, 0);
  12. curl_setopt($ch, curlopt_returntransfer, 1);
  13. $contents = curl_exec($ch);
  14. curl_close($ch);
  15. //post data, Get cookies
  16. $cookie_file = dirname(__file__) . '/cookie.txt';
  17. $ch = curl_init($login_url);
  18. curl_setopt($ch, curlopt_header, 0);
  19. curl_setopt($ch, curlopt_returntransfer, 1);
  20. curl_setopt($ch, curlopt_post, 1);
  21. curl_setopt($ch, curlopt_postfields, $post_fields);
  22. curl_setopt($ch, curlopt_cookiejar, $cookie_file);
  23. curl_exec($ch);
  24. curl_close($ch);
  25. //The content of the page can only be viewed after logging in using the cookie obtained above
  26. $ch = curl_init($get_url);
  27. curl_setopt($ch, curlopt_header, 0);
  28. curl_setopt($ch, curlopt_returntransfer, 0);
  29. curl_setopt( $ch, curlopt_cookiefile, $cookie_file);
  30. $mycontents = curl_exec($ch);
  31. curl_close($ch);
  32. var_dump($mycontents);
Copy code


Statement:
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