Blogger Information
Blog 32
fans 2
comment 2
visits 23308
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php$_SERVER和过滤器学习(0113)
暴风战斧
Original
963 people have browsed it

编程思路

这次作业我主要是翻手册看案例理解$_SERVER和过滤器函数。
开始不知道如何运用过滤器来练习,看了几次手册才找到语法解释。
filter_has_var(type, variable)
filter_input(input_type, variable, filter, options)
filter_var(variable, filter, options)

作业总结

本次作业主要是理解运用,查阅了php手册很多相关内容,通过手册的讲解和案例去理解过滤器函数。常用的是filter_input()、filter_var()和filter_has_var()函数,所谓的过滤实际上指的是FILTER_SANTIZE_EMAIL、FILTER_VALIDATE_EMAIL等,它们是“滤芯”。

1.$_SERVER练习

  • 演示代码
  1. <?php
  2. // $_SERVER变量练习
  3. print_r($_SERVER);
  4. echo '<hr>';
  5. echo $_SERVER['PHP_SELF'].'<br>';
  6. echo $_SERVER['GATEWAY_INTERFACE'].'<br>';
  7. echo $_SERVER['SERVER_ADDR'].'<br>';
  8. echo $_SERVER['SERVER_NAME'].'<br>';
  9. echo $_SERVER['SERVER_SOFTWARE'].'<br>';
  10. echo $_SERVER['SERVER_PROTOCOL'].'<br>';
  11. echo $_SERVER['REQUEST_METHOD'].'<br>';
  12. echo $_SERVER['REQUEST_TIME'].'<br>';
  13. echo $_SERVER['QUERY_STRING'].'<br>';
  14. echo $_SERVER['HTTP_ACCEPT'].'<br>';
  15. echo $_SERVER['HTTP_ACCEPT_CHARSET'].'<br>';
  16. echo $_SERVER['HTTP_HOST'].'<br>';
  17. echo $_SERVER['HTTP_REFERER'].'<br>';
  18. echo $_SERVER['HTTPS'].'<br>';
  19. echo $_SERVER['REMOTE_ADDR'].'<br>';
  20. echo $_SERVER['REMOTE_HOST'].'<br>';
  21. echo $_SERVER['REMOTE_PORT'].'<br>';
  22. echo $_SERVER['SCRIPT_FILENAME'].'<br>';
  23. echo $_SERVER['SERVER_ADMIN'].'<br>';
  24. echo $_SERVER['SERVER_PORT'].'<br>';
  25. echo $_SERVER['SERVER_SIGNATURE'].'<br>';
  26. echo $_SERVER['PATH_TRANSLATED'].'<br>';
  27. echo $_SERVER['SCRIPT_NAME'].'<br>';
  28. echo $_SERVER['SCRIPT_URI'].'<br>';
  29. echo '<hr>';
  30. ?>
  • 效果图

2.过滤器练习

  • 演示代码
  1. <?php
  2. // 常用的过滤器
  3. //print_r(filter_list());
  4. //echo '<hr>';
  5. //验证邮箱格式
  6. $email = 'admin@qq.com';
  7. $email1 = 'admin';
  8. echo filter_var($email1,FILTER_VALIDATE_EMAIL) ? '正确' : '错误';
  9. echo '<hr>';
  10. //验证IP格式
  11. $ip = '127.0.0.1';
  12. echo filter_var($ip,FILTER_VALIDATE_IP) ? '正确' : '错误';
  13. echo '<hr>';
  14. //验证url格式
  15. $url = 'http://www.baidu....';
  16. echo filter_var($url,FILTER_VALIDATE_URL) ? '正确' : '错误';
  17. echo '<hr>';
  18. //验证整数格式
  19. $int = 1;
  20. echo filter_var($int,FILTER_VALIDATE_INT) ? '正确' : '错误';
  21. echo '<hr>';
  22. //验证浮点数格式
  23. $float = 1.1;
  24. echo filter_var($float,FILTER_VALIDATE_FLOAT) ? '正确' : '错误';
  25. echo '<hr>';
  26. //检查是否存在指定输入类型的变量
  27. echo filter_has_var(INPUT_GET,'id');
  28. echo '<hr>';
  29. //净化输入:删除所有字符,除了字母、数字以及 !#$%&'*+-/=?^_`{|}~@.[]
  30. $email = 'admin@我的qq.com';
  31. echo filter_var($email,FILTER_SANITIZE_EMAIL);
  32. echo '<hr>';
  33. $email = 'a d mi n @ qq. c om';
  34. echo filter_var($email,FILTER_SANITIZE_EMAIL);
  35. echo '<hr>';
  36. ?>
  • 效果图

3.数组的序列化和反序列化

  • 演示代码
  1. <?php
  2. // 数组的序列化和反序列化操作
  3. //序列化
  4. $var = 666;
  5. echo serialize($var). '<br>';
  6. $arr = [
  7. 555,
  8. 999,
  9. 888,
  10. 777
  11. ];
  12. echo serialize($arr).'<br>';
  13. //反序列化
  14. $a = unserialize('a:4:{i:0;i:555;i:1;i:999;i:2;i:888;i:3;i:777;}');
  15. print_r($a);
  16. ?>
  • 效果图

4.手写$_SERVER常用变量

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:总结的不错
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments