>백엔드 개발 >PHP 튜토리얼 >PHP CURL模拟登录保存Cookie

PHP CURL模拟登录保存Cookie

WBOY
WBOY원래의
2016-06-20 13:02:322576검색

一、定义Cookie存储路径

必须使用绝对路径

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

二、获取Cookie

将cookie存入文件

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


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

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

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

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

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

五、到指定页面获取数据

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


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