首页 > 后端开发 > php教程 > PHP获取Cookie模拟登录_PHP教程

PHP获取Cookie模拟登录_PHP教程

WBOY
发布: 2016-07-20 11:16:06
原创
801 人浏览过

关键字: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>);
登录后复制

 

 

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/440144.htmlTechArticle关键字: CURL CookieCURLOPT_COOKIEJARCURLOPT_COOKIEFILE模拟登录 PHP 作者: 方倍工作室 原文:http://www.cnblogs.com/txw1958/p/php-cookie-login.html 一、定义Co...
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板