首頁 >後端開發 >php教程 >php模擬遠端登入範例程式碼

php模擬遠端登入範例程式碼

WBOY
WBOY原創
2016-07-25 08:53:38910瀏覽
  1. !extension_loaded('curl') && die('the curl extension is not loaded.');
  2. $baseurl = 'http://127.0.0.1';//根地址
  3. $login_url = $baseurl .'/login.php?act=login';//登录页地址
  4. $get_url = $baseurl .'/index.php'; //需要采集的页面
  5. $post_fields = array();
  6. //以下两项需要修改
  7. $post_fields['name'] = 'admin';
  8. $post_fields['pass'] = '123456';
  9. //获取表单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数据,获取cookie
  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. //使用以上获取的cookie登录后才能查看的页面内容
  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);
复制代码


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn