PHP如何獲取Cookie并實(shí)現(xiàn)模擬登錄
一、定義Cookie存儲(chǔ)路徑
必須使用絕對(duì)路徑
$cookie_jar = dirname(__FILE__).'/pic.cookie';
二、獲取Cookie
將cookie存入文件
$url = 'http://1.2.3.4/';$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);$content = curl_exec($ch);curl_close($ch);
三、模擬瀏覽器獲取驗(yàn)證碼
該服務(wù)器驗(yàn)證碼有漏洞,可以自己指定
取出cookie,一起提交給服務(wù)器,讓服務(wù)器以為是瀏覽器打開(kāi)登陸頁(yè)面
$ch = curl_init();curl_setopt($ch, CURLOPT_URL, ’http://1.2.3.4/getCheckpic.action?rand=6836.185874812305’);curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);$ret = curl_exec($ch);curl_close($ch);
四、POST提交
$post = 'name=2&userType=1&passwd=asdf&loginType=1&rand=6836&imageField.x=25&imageField.y=7'; $ch = curl_init();curl_setopt($ch, CURLOPT_URL, 'http://1.2.3.4/loginstudent.action');curl_setopt($ch, CURLOPT_HEADER, false);curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);curl_setopt($ch, CURLOPT_POSTFIELDS, $post);curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);$result=curl_exec($ch);curl_close($ch);
五、到指定頁(yè)面獲取數(shù)據(jù)
$ch = curl_init();curl_setopt($ch, CURLOPT_URL, 'http://1.2.3.4/accountcardUser.action');curl_setopt($ch, CURLOPT_HEADER, false);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_RETURNTRANSFER,0); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);$html=curl_exec($ch);// var_dump($html);curl_close($ch);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 解決ajax的delete、put方法接收不到參數(shù)的問(wèn)題方法2. ASP.NET MVC通過(guò)勾選checkbox更改select的內(nèi)容3. UDDI FAQs4. ASP基礎(chǔ)入門(mén)第三篇(ASP腳本基礎(chǔ))5. html清除浮動(dòng)的6種方法示例6. JSP之表單提交get和post的區(qū)別詳解及實(shí)例7. JavaScript實(shí)現(xiàn)組件化和模塊化方法詳解8. css進(jìn)階學(xué)習(xí) 選擇符9. PHP循環(huán)與分支知識(shí)點(diǎn)梳理10. PHP字符串前后字符或空格刪除方法介紹
