首頁 >後端開發 >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