首页 >后端开发 >php教程 >php模拟登录保存登录cookie信息的例子

php模拟登录保存登录cookie信息的例子

WBOY
WBOY原创
2016-07-25 08:53:111166浏览
  1. function write_cookie($cookie,$db)

  2. {
  3. $fp = fopen(‘/tmp/cookie.txt’, ‘w’);
  4. fwrite($fp, $cookie);
  5. fclose($fp);
  6. }
  7. function get_cookie($db)

  8. {
  9. $handle = @fopen(“/tmp/cookie.txt”, “r”);

  10. if ($handle) {
  11. while (($buffer = fgets($handle, 4096)) !== false) {
  12. $cookie = trim($buffer);
  13. }
  14. fclose($handle);
  15. }
  16. return $cookie;

  17. }
  18. function openhttp($url,$post=”,$cookie=”,$agent=”,$referfer=”, $origin=”", $host=”", $content_type = “”, $is_header=1){

  19. $ch = curl_init();
  20. curl_setopt($ch, CURLOPT_URL, $url);
  21. $header = array();

  22. if(!empty($host)) {
  23. $header[] = “Host:zzip.ip518.com:900″;
  24. }
  25. if(!empty($origin)) {

  26. $header[] = “Origin:http://zzip.ip518.com:900″;
  27. }
  28. if(!empty($content_type)) {
  29. $header[] = “Content-Type:application/x-www-form-urlencoded”;
  30. }
  31. if(count($header)) {

  32. curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
  33. }
  34. if(!empty($cookie)) curl_setopt($ch, CURLOPT_COOKIE,$cookie);

  35. //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  36. if(!empty($referfer)) curl_setopt($ch, CURLOPT_REFERER, $referfer);
  37. curl_setopt($ch, CURLOPT_USERAGENT, “Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31″);
  38. if(!empty($post)) curl_setopt($ch, CURLOPT_POST, 1);
  39. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  40. if(!empty($post)) curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  41. if($is_header) curl_setopt($ch, CURLOPT_HEADER, 1);
  42. $return = curl_exec($ch);

  43. curl_close($ch);
  44. return $return;
  45. }
  46. function login($username, $password, $login_url = “http://zzip.ip518.com:900/login.asp”)

  47. {
  48. $content=”;
  49. $cookies=”;
  50. $content = openhttp(“http://zzip.ip518.com:900/login.asp”, “name=$username&pass=$password&enter=”);
  51. $cookies = get_items($content, array(“Set-Cookie: “), array(“;”));
  52. $cookie = implode(“;”, $cookies);

  53. write_cookie($cookie);
  54. return $cookie;

  55. }
  56. login(“账号”, “密码”);

复制代码

说明: 登录成功后把cookie信息写入文件/tmp/cookie.txt,下次直接读取这个文件,以后操作此cookie文件,即可实现登录。

>>> 更多 php模拟登录 文章,专题链接:php模拟登录 php curl模拟登录教程大全



声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn