网管联盟 | 网管论坛 | 网管u家 | 网管博客 | 网管软件 | 网管求职 | 小游戏 | 网管搜索 | 网管原创 | 网管聚合 | 网管读摘 | 网管焦点 | 世界素材 | 会员投稿 | 会员中心 
中国网管联盟
Windows Linux Cisco 网络技术 数据库 黑客攻防 DotNet Java PHP 认证 新闻资讯 服务器 存储资讯 网络设备 网管学堂 技术专题 焦点 网吧频道
 当前位置: > bitsCN.com > PHP > PHP编程代码 > 用PHP模拟登陆  

用PHP模拟登陆

2006-04-03  作者:bitsCN整理  来源:中国网管联盟  点评 投稿 收藏

<?php
/* 
* 得到网页内容 
* 参数:$host [in] string
*      主机名称(例如: www.imsorry.com.cn
* 参数:$method [in] string
*      提交方法:POST, GET, HEAD ... 并加上相应的参数( 具体语法参见 RFC1945,RFC2616 )
* 参数:$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");

网管网www_bitscn_com


    }
    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))
    { 中国网管联盟bitsCN.com
        $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;
}

网管联盟bitsCN@com

/* 构造用户名,密码字符串 */
$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

网管下载dl.bitscn.com

    // 可以访问user.php
    $response = GetWebContent("localhost","GET /user.php HTTP/1.0", '', $response['sessid']);
    echo $response['location']."<br>"; // 结果:user.php
}
?>

中国网管联盟bitsCN.com


TAGs   登陆   模拟   response   fp   sessid   if   echo   fputs   br   //   参数      
 上一篇:php 验证码程序   下一篇:访问日志统计
用PHP模拟登陆 评论:
loading.. 评论加载中…
评论:请自觉遵守互联网相关政策法规,评论不得超过250字。

验证码: 注册用户
本类热门排行:
最新推荐文章:
网管论坛交流: