本文實例講述了PHP簡單實現(xiàn)模擬登陸功能。分享給大家供大家參考,具體如下:
在不考慮驗證碼的情況一下,php實現(xiàn)模擬登陸,網(wǎng)上給的辦法一般是采用curl來模擬實現(xiàn),但是curl實現(xiàn)的是服務(wù)器端與服務(wù)器端建立了會話,只能模擬登陸之后獲取登陸之后的數(shù)據(jù),無法將cookie信息種植到客戶端上(至少目前本人查找沒有找到辦法)最后自己通過隱藏的iframe來實現(xiàn)。
1、curl實現(xiàn)模擬登陸的代碼,(只是實現(xiàn)服務(wù)器與服務(wù)器建立會話,其實并沒有在客戶端與服務(wù)器之間建立會話)
<?php $cookie_jar = tempnam('./tmp','cookie'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://192.168.0.22/logincheck.php'); curl_setopt($ch, CURLOPT_POST, 1); $request = 'UNAME=admin&PASSWORD=123456'; curl_setopt($ch, CURLOPT_POSTFIELDS, $request); //把返回來的cookie信息保存在$cookie_jar文件中 curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar); //設(shè)定返回的數(shù)據(jù)是否自動顯示 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //設(shè)定是否顯示頭信息 curl_setopt($ch, CURLOPT_HEADER, false); //設(shè)定是否輸出頁面內(nèi)容 curl_setopt($ch, CURLOPT_NOBODY, false); curl_exec($ch); curl_close($ch); //get data after login $ch2 = curl_init(); curl_setopt($ch2, CURLOPT_URL, 'http://192.168.0.22/general/'); curl_setopt($ch2, CURLOPT_HEADER, false); curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch2, CURLOPT_COOKIEFILE, $cookie_jar); $orders = curl_exec($ch2); echo $orders; exit; echo '<pre>'; echo strip_tags($orders); echo '</pre>'; curl_close($ch2); ?>
2、通過隱藏的iframe實現(xiàn)客戶端與服務(wù)器端的通信(肯能帶來一定的安全隱患)
<html> <title></title> <body> <? $goURL="http://192.168.0.22/general/email/"; ?> <iframe name="hiddenLoginFrame" onload="get_pass()" src="http://www.gimoo.net/t/1712/ceshi1.php" id="hiddenLoginFrame" width=0 height=0 frameborder=0 scrolling=no style="display:none;"> </iframe> <script Language="JavaScript"> function get_pass() { window.open("<?=$goURL ?>"); window.close(); } </script> </body> </html>
ceshi1.php
<html> <head> <title>ceshi</title> </head> <body onload="get_pass1();"> <form name="form1" method="post" target="hiddenLoginFrame" action="http://192.168.0.22/logincheck.php"> <input type="text" value="admin" name="UNAME"> <input type="text" value="123456" name="PASSWORD"> </form> </body> <script Language="JavaScript"> function get_pass1() { //document.form1.action=u_url; document.form1.submit(); } </script> </html>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php curl用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。