Home > Backend Development > PHP Tutorial > PHP CURL模拟登录保存Cookie

PHP CURL模拟登录保存Cookie

WBOY
Release: 2016-06-20 13:02:32
Original
2567 people have browsed it

一、定义Cookie存储路径

必须使用绝对路径

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

二、获取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>
Copy after login


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

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

取出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>
Copy after login

四、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>
Copy after login

五、到指定页面获取数据

<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>
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template