Rumah > php教程 > php手册 > teks badan

PHP获取Cookie模拟登录

WBOY
Lepaskan: 2016-06-13 11:31:48
asal
827 orang telah melayarinya

关键字: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";
Salin selepas log masuk

二、获取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>);
Salin selepas log masuk

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

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

取出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>);
Salin selepas log masuk

四、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>);
Salin selepas log masuk

五、到指定页面获取数据

<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>);
Salin selepas log masuk

 

 

 

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan