PHP简单模拟登录功能实例分享
1、curl实现模拟登录的代码,(只是实现服务器与服务器建立会话,其实并没有在客户端与服务器之间建立会话)
<?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); //设定返回的数据是否自动显示 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //设定是否显示头信息 curl_setopt($ch, CURLOPT_HEADER, false); //设定是否输出页面内容 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实现客户端与服务器端的通信(肯能带来一定的安全隐患)
<html> <title></title> <body> <? $goURL="http://192.168.0.22/general/email/"; ?> <iframe name="hiddenLoginFrame" onload="get_pass()" src="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>
你对人生迷茫吗?
那就背起行囊,起步远行吧