>  기사  >  php教程  >  PHP获取Cookie模拟登录

PHP获取Cookie模拟登录

WBOY
WBOY원래의
2016-06-13 11:31:48820검색

关键字:CURL Cookie CURLOPT_COOKIEJAR CURLOPT_COOKIEFILE 模拟登录 PHP
作者:方倍工作室
原文:http://www.cnblogs.com/txw1958/p/php-cookie-login.html

 

一、定义Cookie存储路径

必须使用绝对路径

<span $cookie_jar</span> = <span dirname</span>(<span __FILE__</span>)."/pic.cookie";

 

二、获取Cookie

将cookie存入文件

<span $url</span> = "http://1.2.3.4/"<span ;
</span><span $ch</span> =<span  curl_init();
curl_setopt(</span><span $ch</span>, CURLOPT_URL, <span $url</span><span );
curl_setopt(</span><span $ch</span>, CURLOPT_HEADER, 0<span );
curl_setopt(</span><span $ch</span>, CURLOPT_RETURNTRANSFER, <span true</span><span );
curl_setopt(</span><span $ch</span>, CURLOPT_COOKIEJAR, <span $cookie_jar</span><span );
</span><span $content</span> = curl_exec(<span $ch</span><span );
curl_close(</span><span $ch</span>);

 

三、模拟浏览器获取验证码

该服务器验证码有漏洞,可以自己指定

取出cookie,一起提交给服务器,让服务器以为是浏览器打开登陆页面

<span $ch</span> =<span  curl_init();
curl_setopt(</span><span $ch</span>, CURLOPT_URL, 'http://1.2.3.4/getCheckpic.action?rand=6836.185874812305'<span );
curl_setopt(</span><span $ch</span>, CURLOPT_COOKIEFILE, <span $cookie_jar</span><span );
curl_setopt(</span><span $ch</span>, CURLOPT_HEADER, 0<span );
curl_setopt(</span><span $ch</span>, CURLOPT_RETURNTRANSFER, 1<span );
</span><span $ret</span> = curl_exec(<span $ch</span><span );
curl_close(</span><span $ch</span>);

 

四、POST提交

<span $post</span> = "name=2&userType=1&passwd=asdf&loginType=1&rand=6836&imageField.x=25&imageField.y=7"<span ;    
</span><span $ch</span> =<span  curl_init();
curl_setopt(</span><span $ch</span>, CURLOPT_URL, "http://1.2.3.4/loginstudent.action"<span );
curl_setopt(</span><span $ch</span>, CURLOPT_HEADER, <span false</span><span );
curl_setopt(</span><span $ch</span>, CURLOPT_RETURNTRANSFER,1<span );
curl_setopt(</span><span $ch</span>, CURLOPT_POSTFIELDS, <span $post</span><span );
curl_setopt(</span><span $ch</span>, CURLOPT_COOKIEFILE, <span $cookie_jar</span><span );
</span><span $result</span>=curl_exec(<span $ch</span><span );
curl_close(</span><span $ch</span>);

 

五、到指定页面获取数据

<span $ch</span> =<span  curl_init();
curl_setopt(</span><span $ch</span>, CURLOPT_URL, "http://1.2.3.4/accountcardUser.action"<span );
curl_setopt(</span><span $ch</span>, CURLOPT_HEADER, <span false</span><span );
curl_setopt(</span><span $ch</span>, CURLOPT_HEADER, 0<span );
curl_setopt(</span><span $ch</span>, CURLOPT_RETURNTRANSFER,0<span );        
curl_setopt(</span><span $ch</span>, CURLOPT_COOKIEFILE, <span $cookie_jar</span><span );
</span><span $html</span>=curl_exec(<span $ch</span><span );
</span><span //</span><span  var_dump($html);</span>
curl_close(<span $ch</span>);

 

 

 

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.