最适合网络开发者的网站

PHP 教程

PHP 主页 PHP 简介 PHP 安装 PHP 语法 PHP 注释 PHP 变量 PHP 回显/打印 PHP 数据类型 PHP 字符串 PHP 数字 PHP 数学 PHP 常量 PHP 运算符 PHP 如果...否则...Elseif PHP 开关 PHP 循环 PHP 函数 PHP 数组 PHP 超全局变量 PHP 正则表达式

PHP 形式

PHP 表单处理 PHP 表单验证 需要 PHP 表单 PHP 表单 URL/电子邮件 PHP 表单完成

PHP 先进的

PHP 日期和时间 PHP 包含 PHP 文件处理 PHP 文件打开/读取 PHP 文件创建/写入 PHP文件上传 PHP 饼干 PHP 会话 PHP 过滤器 PHP 过滤器高级 PHP 回调函数 PHP 的 JSON PHP 异常

PHP 面向对象

PHP 什么是OOP PHP 类/对象 PHP 构造函数 PHP 析构函数 PHP 访问修饰符 PHP 继承 PHP 常量 PHP 抽象类 PHP 接口 PHP 特征 PHP 静态方法 PHP 静态属性 PHP 命名空间 PHP 可迭代对象

MySQL 数据库

MySQL 数据库 MySQL 连接 MySQL 创建数据库 MySQL 创建表 MySQL 插入数据 MySQL 获取最后一个 ID MySQL 插入多个 MySQL 已准备 MySQL 选择数据 MySQL 哪里 MySQL 排序依据 MySQL 删除数据 MySQL 更新数据 MySQL 限制数据

PHP XML

PHP XML 解析器 PHP SimpleXML 解析器 PHP SimpleXML - 获取 PHP XML 扩展 PHP XML DOM

PHP - 阿贾克斯

AJAX 简介 AJAX PHP AJAX 数据库 AJAX XML AJAX实时搜索 AJAX 民意调查

PHP 例子

PHP 示例 PHP 编译器 PHP 测验 PHP 练习 PHP 证书

PHP 参考

PHP 概述 PHP 数组 PHP 日历 PHP 日期 PHP 目录 PHP 错误 PHP 异常 PHP 文件系统 PHP 过滤器 PHP FTP PHP 的 JSON PHP 关键字 PHP 库 PHP 邮件 PHP 数学 PHP 杂项 PHP MySQLi PHP 网络 PHP 输出控件 PHP 正则表达式 PHP 简单 XML PHP 流 PHP 字符串 PHP 变量处理 PHP XML解析器 PHP 压缩文件 PHP 时区

PHP。初学者课程

尿素

PHP Filters


Validating data = Determine if the data is in proper form.

Sanitizing data = Remove any illegal character from the data.


The PHP Filter Extension

PHP filters are used to validate and sanitize external input.

The PHP filter extension has many of the functions needed for checking user input, and is designed to make data validation easier and quicker.

filter_list() function can be used to list what the PHP filter extension offers:

例子

<table>
  <tr>
    <td>Filter Name</td>
    <td>Filter ID</td>
  </tr>
  <?php
  foreach (filter_list() as $id =>$filter) {
    echo '<tr><td>' . $filter . '</td><td>' . filter_id($filter) . '</td></tr>';
  }
  ?>
</table>
Try it Yourself »

Why Use Filters?

Many web applications receive external input. External input/data can be:

  • User input from a form
  • Cookies
  • Web services data
  • Server variables
  • Database query results

You should always validate external data!
Invalid submitted data can lead to security problems and break your webpage!
By using PHP filters you can be sure your application gets the correct input!


PHP filter_var() Function

filter_var() function both validate and sanitize data.

filter_var() function filters a single variable with a specified filter. It takes two pieces of data:

  • The variable you want to check
  • The type of check to use

Sanitize a String

The following example uses the filter_var() function to remove all HTML tags from a string:

例子

<?php
$str = "<h1>Hello World!</h1>";
$newstr = filter_var($str, FILTER_SANITIZE_STRING);
echo $newstr;
?>
Try it Yourself »

Validate an Integer

The following example uses the filter_var() function to check if the variable $int is an integer. If $int is an integer, the output of the code below will be: "Integer is valid". If $int is not an integer, the output will be: "Integer is not valid":

例子

<?php
$int = 100;

if (!filter_var($int, FILTER_VALIDATE_INT) === false) {
  echo("Integer is valid");
} else {
  echo("Integer is not valid");
}
?>
Try it Yourself »

Tip: filter_var() and Problem With 0

In the example above, if $int was set to 0, the function above will return "Integer is not valid". To solve this problem, use the code below:

例子

<?php
$int = 0;

if (filter_var($int, FILTER_VALIDATE_INT) === 0 || !filter_var($int, FILTER_VALIDATE_INT) === false) {
  echo("Integer is valid");
} else {
  echo("Integer is not valid");
}
?>
Try it Yourself »

Validate an IP Address

The following example uses the filter_var() function to check if the variable $ip is a valid IP address:

例子

<?php
$ip = "127.0.0.1";

if (!filter_var($ip, FILTER_VALIDATE_IP) === false) {
  echo("$ip is a valid IP address");
} else {
  echo("$ip is not a valid IP address");
}
?>
Try it Yourself »

Sanitize and Validate an Email Address

The following example uses the filter_var() function to first remove all illegal characters from the $email variable, then check if it is a valid email address:

例子

<?php
$email = "john.doe@example.com";

// Remove all illegal characters from email
$email = filter_var($email, FILTER_SANITIZE_EMAIL);

// Validate e-mail
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
  echo("$email is a valid email address");
} else {
  echo("$email is not a valid email address");
}
?>
Try it Yourself »

Sanitize and Validate a URL

The following example uses the filter_var() function to first remove all illegal characters from a URL, then check if $url is a valid URL:

例子

<?php
$url = "https://www.w3schools.com";

// Remove all illegal characters from a url
$url = filter_var($url, FILTER_SANITIZE_URL);

// Validate url
if (!filter_var($url, FILTER_VALIDATE_URL) === false) {
  echo("$url is a valid URL");
} else {
  echo("$url is not a valid URL");
}
?>
Try it Yourself »

Complete PHP Filter Reference

For a complete reference of all filter functions, go to our complete PHP Filter Reference. Check each filter to see what options and flags are available.

The reference contains a brief description, and examples of use, for each function!