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

首頁 > 開發 > PHP > 正文

用PHP模擬登陸

2024-05-04 23:04:20
字體:
來源:轉載
供稿:網友
注冊會員,創建你的web開發資料庫,

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

  源代碼:

<?php
/* 
* 得到網頁內容 
* 參數:$host [in] string
*      主機名稱(例如: www.imsorry.com.cn
* 參數:$method [in] string
*      提交方法:post, get, head ... 并加上相應的參數( 具體語法參見 rfc1945,rfc2068 )
* 參數:$str [in] string
*      提交的內容
* 參數:$sessid [in] string
*      php的sessionid
*
* @返回 網頁內容 string
*/
function getwebcontent($host, $method, $str, $sessid = '')
{
    $ip = gethostbyname($host);
    $fp = fsockopen($ip, 80);
    if (!$fp) return;
    fputs($fp, "$method/r/n");
    fputs($fp, "host: $host/r/n");
    if (!empty($sessid))
    {
        fputs($fp, "cookie: phpsessid=$sessid; path=/;/r/n");
    }
    if ( substr(trim($method),0, 4) == "post")
    {
        fputs($fp, "content-length: ". strlen($str) . "/r/n"); //  別忘了指定長度
    }
    fputs($fp, "content-type: application/x-www-form-urlencoded/r/n/r/n");
    if ( substr(trim($method),0, 4) == "post")
    {
        fputs($fp, $str."/r/n");
    }
    while(!feof($fp))
    {
        $response .= fgets($fp, 1024);
    }
    $hlen = strpos($response,"/r/n/r/n"); // linux下是 "/n/n"
    $header = substr($response, 0, $hlen);
    $entity = substr($response, $hlen + 4);
    if ( preg_match('/phpsessid=([0-9a-z]+);/i', $header, $matches))
    {
        $a['sessid'] = $matches[1];
    }
    if ( preg_match('/location: ([0-9a-z/_/?/=/&/#/.]+)/i', $header, $matches))
    {
        $a['location'] = $matches[1];
    }
    $a['content'] = $entity;    
    fclose($fp);
    return $a;
}

/* 構造用戶名,密碼字符串 */
$str = ("username=test&password=test");
$response = getwebcontent("localhost","post /login.php http/1.0", $str);
echo $response['location'].$response['content']."<br>";
echo $response['sessid']."<br>";
if ( preg_match('/error/.php/i',$response['location']))
{
    echo "登陸失敗<br>";
} else {
    echo "登陸成功<br>";
    // 不可以訪問user.php,因為不帶sessid參數
    $response = getwebcontent("localhost","get /user.php http/1.0", '', '');
    echo $response['location']."<br>"; // 結果:error.php?errcode=2

    // 可以訪問user.php
    $response = getwebcontent("localhost","get /user.php http/1.0", '', $response['sessid']);
    echo $response['location']."<br>"; // 結果:user.php
}
?>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黑山县| 随州市| 环江| 荃湾区| 准格尔旗| 常州市| 商河县| 理塘县| 洪湖市| 广汉市| 张掖市| 玉田县| 宝清县| 娱乐| 全南县| 裕民县| 博客| 洪泽县| 昔阳县| 牡丹江市| 衢州市| 阜平县| 娄底市| 巴彦县| 南丹县| 穆棱市| 卫辉市| 珠海市| 大足县| 四川省| 灯塔市| 青铜峡市| 阿鲁科尔沁旗| 尼玛县| 怀集县| 阳曲县| 南乐县| 宜川县| 卢湾区| 仙居县| 怀安县|