php 模拟登录系统,php模拟登录的两种方法

这篇博客介绍了如何在PHP中使用CURL和fsockopen函数模拟登录并获取SESSIONID。首先,通过开启php_curl.dll来启用CURL,然后设置相关选项进行登录和读取页面。接着,利用fsockopen函数进行连接,通过发送HTTP请求头和内容来实现模拟登录。这两个方法都可以有效地在PHP中处理网站登录和会话管理。
摘要由CSDN通过智能技术生成

php模拟登录 获取SESSIONID,并则在读取网页的时候发送

php模拟登录的两种方法

方法1用curl:

一.先开启php curl函数库的步骤

1).去掉windows/php.ini 文件里;extension=php_curl.dll前面的; ? ?/*用 echo phpinfo();查看php.ini的路径*/

2).把php5/libeay32.dll,ssleay32.dll复制到系统目录windows/下

二.例子

例子:

$_jar = tempnam('./tmp','');

$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);

//把返回来的信息保存在$_jar文件中

curl_setopt($ch, CURLOPT_JAR, $_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_FILE, $_jar);

$orders = curl_exec($ch2);

echo '';

echo strip_tags($orders);

echo '';

curl_close($ch2);?

?>

方法2用fsockopen:

?GetWebContent($host, $method, $str, $sessid = '')

{?

? ? $ip = gethostbyname($host);?

//echo "ip=$ip
";

? ? [email=$fp=@fsockopen($ip,80]$fp=@fsockopen($ip,80[/email]);?

? ? if (!$fp) return;?

? ? fputs($fp, "$methodrn");?

? ? fputs($fp, "Host: $hostrn");?

? ? if (!empty($sessid))?

? ? {?

? ? ? ? fputs($fp, ": 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");?

fputs($fp, "Content-Type: application/x-www-form-urlencodedrn");?

fputs($fp, "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1)rn)");//add by Ew 071012

fputs($fp, "Connection: Keep-Alivernrn");

? ? if ( substr(trim($method),0, 4) == "POST")?

? ? {?

? ? ? ? fputs($fp, $str."rn");?

? ? }?

? ? while(!feof($fp))?

? ? {?

? ? ? ? $response .= fgets($fp);?

? ? }?

? ? $hlen = strpos($response,"rnrn"); // LINUX下是 "nn"?

? ? $header = substr($response, 0, $hlen);?

//echo "header=$header



";

? ? $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']."
";?

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值