Home  >  Article  >  Backend Development  >  Implementation example of PHP simulated login function

Implementation example of PHP simulated login function

黄舟
黄舟Original
2017-09-16 09:35:511582browse

这篇文章主要介绍了PHP简单实现模拟登陆功能,涉及php使用curl实现模拟登陆的相关操作技巧,需要的朋友可以参考下

本文实例讲述了PHP简单实现模拟登陆功能。分享给大家供大家参考,具体如下:

在不考虑验证码的情况一下,php实现模拟登陆,网上给的办法一般是采用curl来模拟实现,但是curl实现的是服务器端与服务器端建立了会话,只能模拟登陆之后获取登陆之后的数据,无法将cookie信息种植到客户端上(至少目前本人查找没有找到办法)最后自己通过隐藏的iframe来实现。

1、curl实现模拟登陆的代码,(只是实现服务器与服务器建立会话,其实并没有在客户端与服务器之间建立会话)


<?php
$cookie_jar = tempnam(&#39;./tmp&#39;,&#39;cookie&#39;);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, &#39;http://192.168.0.22/logincheck.php&#39;);
curl_setopt($ch, CURLOPT_POST, 1);
$request = &#39;UNAME=admin&PASSWORD=123456&#39;;
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, &#39;http://192.168.0.22/general/&#39;);
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 &#39;<pre class="brush:php;toolbar:false">&#39;;
echo strip_tags($orders);
echo &#39;
'; 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>

The above is the detailed content of Implementation example of PHP simulated login function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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