php模拟登录并获取sessionid多种方法
方法1,使用php curl实现模拟登录。
一,先开启php curl函数库的步骤
1),去掉windows/php.ini 文件里;extension=php_curl.dll前面的; /*用 echo phpinfo();查看php.ini的路径*/
2),把php5/libeay32.dll,ssleay32.dll复制到系统目录windows/下
3),重启apache/ target=_blank class=infotextkey>apache服务器。
完整代码:
<?php
$cookie_jar = tempnam('./tmp','cookie');
$ch = curl_init(); curl_setopt($ch,curlopt_url,'http://******');
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); //get data after login
$ch2 = curl_init();
curl_setopt($ch2, curlopt_url, 'http://*****');
curl_setopt($ch2, curlopt_header, false);
curl_setopt($ch2, curlopt_returntransfer, 1);
curl_setopt($ch2, curlopt_cookiefile, $cookie_jar);
$orders = curl_exec($ch2);
echo '';
echo strip_tags($orders);
echo '';
curl_close($ch2);
?>
方法2,用fsockopen实现模拟登录。
完整代码:
<?php
function getwebcontent($host, $method, $str, $sessid = '')
{
$ip = gethostbyname($host);
//echo "ip=$ip<br>";
[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-www-form-urlencoded ");
fputs($fp, "content-type: application/x-www-form-urlencoded ");
fputs($fp, "user-agent: mozilla/4.0 (compatible; msie 6.0; windows nt 5.1; sv1; infopath.1) )");//add by ew 071012
fputs($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);
//echo "header=$header<hr><hr>";
$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_page http/1.0", $str);//登入得到新的session_id
//...可以在这里先保存session_id
$response = getwebcontent("$host","get /$somepage http/1.0", '', $response['sessid']);//使用session_id访问页面
echo $response['location'].$response['content']."<br>";
?>
>>> 更多 php模拟登录 文章,专题链接:php模拟登录 php curl模拟登录教程大全