Rumah >pembangunan bahagian belakang >tutorial php >PHP获取Cookie模拟登录_PHP教程
关键字: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>);