首页 >后端开发 >php教程 >php模拟远程登录示例代码

php模拟远程登录示例代码

WBOY
WBOY原创
2016-07-25 08:53:38898浏览
  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