澳门新葡萄京官网首页php模拟登录 获取SESSIONID,并则在读取网页的时候发送

方法1用curl:一.先开启php curl函数库的步骤1).去掉windows/php.ini
文件里;extension=php_curl.dll前面的; /*用 echo
phpinfo();查看php.ini的路径*/2).把php5/libeay32.dll,ssleay32.dll复制到系统目录windows/下3).重启apache二.例子例子:?php$cookie_jar=tempnam(”./tmp”,”cookie”);$ch=curl_init();curl_setopt($ch,CURLOPT_URL,”******”);curl_setopt($ch,CURLOPT_POST,1);$request=”email_address=password=action=”;curl_setopt($ch,CURLOPT_POSTFIELDS,$request);//把返回来的cookie信息保存在$cookie_jar文件中curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_jar);//设定返回的数据是否自动显示curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//设定是否显示头信息curl_setopt($ch,CURLOPT_HEADER,false);//设定是否输出页面内容curl_setopt($ch,CURLOPT_NOBODY,false);curl_exec($ch);curl_close($ch);//getdataafterlogin$ch2=curl_init();curl_setopt($ch2,CURLOPT_URL,”*****”);curl_setopt($ch2,CURLOPT_HEADER,false);curl_setopt($ch2,CURLOPT_RETURNTRANSFER,1);curl_setopt($ch2,CURLOPT_COOKIEFILE,$cookie_jar);$orders=curl_exec($ch2);echo””;echostrip_tags($orders);echo””;curl_close($ch2);?方法2用fsockopen:?phpfunctionGetWebContent($host,$method,$str,$sessid=””){$ip=gethostbyname($host);//echoip=$ipbr;[email=$fp=@fsockopen($ip,80]$fp=@fsockopen($ip,80[/email]);if(!$fp)return;fputs($fp,$method
);fputs($fp,Host:$host
);if(!empty($sessid)){fputs($fp,Cookie:PHPSESSID=$sessid;path=/;
);}if(substr(trim($method),0,4)==POST){fputs($fp,Content-Length:.strlen($str).
);//别忘了指定长度}//fputs($fp,Content-Type:application/x-
);fputs($fp,Content-Type:application/x-
);fputs($fp,User-Agent:Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.1;SV1;InfoPath.1)
));//addbyEw071012fputs($fp,Connection:Keep-Alive
);if(substr(trim($method),0,4)==POST){fputs($fp,$str.
);}while(!feof($fp)){$response.=fgets($fp);}$hlen=strpos($response,
);//LINUX下是
$header=substr($response,0,$hlen);//echoheader=$headerhrhr;$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;}$response=GetWebContent($host,POST/$login_pageHTTP/1.0,$str);//登入得到新的session_id//…可以在这里先保存session_id$response=GetWebContent($host,GET/$somepageHTTP/1.0,””,$response[”sessid”]);//使用session_id访问页面echo$response[”location”].$response[”content”].br;?<

经常会有人问模拟登陆的问题,其实原理很简单,只要把SessionID保存下来就可以了,今天花了一个小时的时间写了一个函数,供大家参考,网站返回的头信息,具体网站具体分析。

  源代码:

<?php
/*
* 得到网页内容
* 参数:$host [in] string
* 主机名称(例如: www.etoow.com)
* 参数:$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, “$methodrn”);
fputs($fp, “Host: $hostrn”);
if (!empty($sessid))
{
fputs($fp, “Cookie: PHPSESSID=$sessid; path=/;rn”);
}
if ( substr(trim($method),0, 4) == “POST”)
{
fputs($fp, “Content-Length: “. strlen($str) . “rn”); // 别忘了指定长度
}
fputs($fp, “Content-Type: application/x-www-form-urlencodedrnrn”);
if ( substr(trim($method),0, 4) == “POST”)
{
fputs($fp, $str.”rn”);
}
while(!feof($fp))
{
$response .= fgets($fp, 1024);
}
$hlen = strpos($response,” “); // LINUX下是 ” “
$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
}
?>

发表评论

电子邮件地址不会被公开。 必填项已用*标注