国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

用PHP模擬登陸

2024-05-04 21:56:48
字體:
來源:轉載
供稿:網友

經常會有人問模擬登陸的問題,其實原理很簡單,只要把SessionID保存下來就可以了,今天花了一個小時的時間寫了一個函數,供大家參考,網站返回的頭信息,具體網站具體分析,源代碼:

  1. <?php 
  2. /* 
  3. * 得到網頁內容 
  4. * 參數:$host [in] string 
  5. * 主機名稱(例如: www.etoow.com) 
  6. * 參數:$method [in] string 
  7. * 提交方法:POST, GET, HEAD ... 并加上相應的參數( 具體語法參見 RFC1945,RFC2068 ) 
  8. * 參數:$str [in] string 
  9. * 提交的內容 
  10. * 參數:$sessid [in] string 
  11. * PHP的SESSIONID 
  12. * 
  13. * @返回 網頁內容 string 
  14. */ 
  15. function GetWebContent($host$method$str$sessid = ''
  16. $ip = gethostbyname($host); 
  17. $fp = fsockopen($ip, 80); 
  18. if (!$fpreturn
  19. fputs($fp"$methodrn"); 
  20. fputs($fp"Host: $hostrn"); 
  21. if (!emptyempty($sessid)) 
  22. fputs($fp"Cookie: PHPSESSID=$sessid; path=/;rn"); 
  23. if ( substr(trim($method),0, 4) == "POST"
  24. fputs($fp"Content-Length: "strlen($str) . "rn"); // 別忘了指定長度 
  25. fputs($fp"Content-Type: application/x-www-form-urlencodedrnrn"); 
  26. if ( substr(trim($method),0, 4) == "POST"
  27. fputs($fp$str."rn"); 
  28. while(!feof($fp)) 
  29. $response .= fgets($fp, 1024); 
  30. $hlen = strpos($response," "); // LINUX下是 " " 
  31. $header = substr($response, 0, $hlen); 
  32. $entity = substr($response$hlen 4); 
  33. if ( preg_match('/PHPSESSID=([0-9a-z] );/i'$header$matches)) 
  34. $a['sessid'] = $matches[1]; 
  35. if ( preg_match('/Location: ([0-9a-z_?=&#.] )/i'$header$matches)) 
  36. $a['location'] = $matches[1]; 
  37. $a['content'] = $entity
  38. fclose($fp); 
  39. return $a
  40.  
  41. /* 構造用戶名,密碼字符串 */ 
  42. $str = ("username=test&password=test"); 
  43. $response = GetWebContent("localhost","POST /login.php HTTP/1.0"$str); 
  44. echo $response['location'].$response['content']."<br>"
  45. echo $response['sessid']."<br>"
  46. if ( preg_match('/error.php/i',$response['location'])) 
  47. echo "登陸失敗<br>"
  48. else { 
  49. echo "登陸成功<br>"
  50. // 不可以訪問user.php,因為不帶sessid參數 
  51. $response = GetWebContent("localhost","GET /user.php HTTP/1.0"''''); 
  52. echo $response['location']."<br>"// 結果:error.php?errcode=2 
  53.  
  54. // 可以訪問user.php 
  55. $response = GetWebContent("localhost","GET /user.php HTTP/1.0"''$response['sessid']); 
  56. echo $response['location']."<br>"// 結果:user.php 
  57. ?> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 曲麻莱县| 昌都县| 长白| 湟源县| 青阳县| 田林县| 宜黄县| 蓬溪县| 安平县| 松原市| 南岸区| 武夷山市| 丽水市| 茌平县| 林州市| 柳州市| 高台县| 独山县| 长垣县| 微博| 海丰县| 揭阳市| 武清区| 化隆| 外汇| 宁远县| 子长县| 方城县| 灯塔市| 福建省| 佳木斯市| 新巴尔虎右旗| 蓬安县| 扶绥县| 石城县| 阿鲁科尔沁旗| 荃湾区| 福清市| 霞浦县| 增城市| 开江县|