当前位置:编程学习 > > 正文

php 模拟登录验证码demo(PHP 模拟登陆功能实例详解)

时间:2021-10-28 10:44:58类别:编程学习

php 模拟登录验证码demo

PHP 模拟登陆功能实例详解

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

说明:该模拟登陆实例仅适用于没有验证码的模拟登陆实例

该程序基本功能是,模拟登陆高校图书馆网站,并获取读者信息和借书信息。

程序截图:

php 模拟登录验证码demo(PHP 模拟登陆功能实例详解)

php 模拟登录验证码demo(PHP 模拟登陆功能实例详解)

目录结构:

php 模拟登录验证码demo(PHP 模拟登陆功能实例详解)

login.php

  1. <form method="post" class="am-form" action="judge.php">
  2. <label for="sid">学号:</label>
  3. <input type="text" name="number" id="sid" value="">
  4. <br>
  5. <label for="password">密码:</label>
  6. <input type="password" name="passwd" id="password" value="">
  7. <br>
  8. <li class="am-cf">
  9. <input type="submit" name="" value="登 录" class="am-btn am-btn-primary am-btn-sm am-fl">
  10. <input type="submit" name="" value="忘记密码 ^_^? " class="am-btn am-btn-default am-btn-sm am-fr">
  11. </li>
  12. </form>

judge.php

  1. session_start();
  2. require_once 'curl.php';
  3. $url = "http://opac.lib.ustc.edu.cn/reader/redr_info.php";
  4. $number = $_POST['number'];
  5. $passwd = $_POST['passwd'];
  6. $res = run_curl($url,$number,$passwd);
  7. $pattern = '/<TD><span class=\"bluetext\">姓名:<\/span>(.*)<\/TD>/';
  8. preg_match($pattern, $res,$arr1);
  9. if(is_array($arr1)){
  10. $_SESSION['number'] = $number;
  11. $_SESSION['passwd'] = $passwd;
  12. $_SESSION['name'] = $arr1[1];
  13. echo "<script>window.location.href='index.php';</script>";
  14. }else{
  15. echo "<script>history.go(-1);</script>";
  16. }

curl.php

  1. <?php
  2. function run_curl($content_url,$number='',$passwd=''){
  3. $cookie_file = tempnam('./temp','cookie');
  4. $url = "http://opac.lib.ustc.edu.cn/reader/redr_verify.php";
  5. $ch = curl_init($url);
  6. curl_setopt($ch, CURLOPT_HEADER, 0);
  7. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  8. curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
  9. $post_fileds = "number=$number&passwd=$passwd&select=bar_no";
  10. curl_setopt($ch,CURLOPT_POST,1);
  11. curl_setopt($ch,CURLOPT_POSTFIELDS,$post_fileds);
  12. curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie_file);
  13. curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file);
  14. $content = curl_exec($ch);
  15. curl_close($ch);
  16. $ch = curl_init($content_url);
  17. curl_setopt($ch, CURLOPT_HEADER, 0);
  18. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  19. curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
  20. curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie_file);
  21. curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file);
  22. $res = curl_exec($ch);
  23. curl_close($ch);
  24. return $res;
  25. }

index.php

  1. <?php
  2. session_start();
  3. $number = $_SESSION['number'];
  4. $passwd = $_SESSION['passwd'];
  5. $username = $_SESSION['name'];
  6. if($number==''||$passwd==''||$username==''){
  7. echo "<script>window.location.href='login.php';</script>";
  8. exit();
  9. }
  10. require_once 'curl.php';
  11. header('Content-type:text/html;charset=utf-8');
  12. $url = "http://opac.lib.ustc.edu.cn/reader/book_lst.php";
  13. $res = run_curl($url,$number,$passwd); //通过curl抓取数据
  14. $pattern = '/<td class="whitetext" width="35%"><a class="blue" href="(.*)" rel="external nofollow" >(.*)<\/a>/';//正则匹配获得图书信息
  15. preg_match_all($pattern, $res,$book_arr);
  16. $booklist = array();
  17. $booklist = $book_arr[2]; //借阅图书列表
  18. //var_dump($booklist);
  19. $pattern = '/<font color=(red|)>(.*)<\/font>/';//正则匹配获得还书信息
  20. preg_match_all($pattern, $res,$date_arr);
  21. $datelist = array();
  22. $datelist = $date_arr[2]; //应归还日期列表
  23. $taglist = array();
  24. $taglist = $date_arr[1]; //标记是否超期
  25. //var_dump($date_arr);
  26. ?>

logout.php

  1. <?php
  2. session_start();
  3. $_SESSION = array();
  4. session_destroy();
  5. ?>
  6. <script>window.location.href='login.php'</script>

希望本文所述对大家PHP程序设计有所帮助。

原文链接:https://blog.csdn.net/koastal/article/details/50273997

标签:
上一篇下一篇

猜您喜欢

热门推荐