本文主要和大家分享PHP取得Cookie實現模擬登入程式碼,希望能幫助大家。
一、定義Cookie儲存路徑
必須使用絕對路徑
$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);
三、模擬瀏覽器取得驗證碼
此伺服器驗證碼有漏洞,可以自行指定
取出cookie,一起提交給伺服器,讓伺服器以為是瀏覽器開啟登陸頁面
$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);
五、到指定頁面取得資料
$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);
相關推薦:
以上是PHP取得Cookie實作模擬登入程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!