php表单输入验证
2015-09-08 18:25
701 查看
当用户提交表单时,我们将做以下两件事情,:
使用 PHP trim() 函数去除用户输入数据中不必要的字符 (如:空格,tab,换行)。
使用PHP stripslashes()函数去除用户输入数据中的反斜杠 (\) 。
使用PHP htmlspecialchars() 把一些预定义的字符转换为 HTML 实体。
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
<?php
// 定义变量并默认设为空值
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["name"]))
{$nameErr = "Name is required";}
else
{$name = test_input($_POST["name"]);}
if (empty($_POST["email"]))
{$emailErr = "Email is required";}
else
{$email = test_input($_POST["email"]);}
if (empty($_POST["website"]))
{$website = "";}
else
{$website = test_input($_POST["website"]);}
if (empty($_POST["comment"]))
{$comment = "";}
else
{$comment = test_input($_POST["comment"]);}
if (empty($_POST["gender"]))
{$genderErr = "Gender is required";}
else
{$gender = test_input($_POST["gender"]);}
}
?>
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name))
{
$nameErr = "只允许字母及空格";
}
$email = test_input($_POST["email"]);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
$emailErr = "非法邮件地址";
}
$website = test_input($_POST["website"]);
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website))
{
$websiteErr = "不合法的 URL";
}
使用 PHP trim() 函数去除用户输入数据中不必要的字符 (如:空格,tab,换行)。
使用PHP stripslashes()函数去除用户输入数据中的反斜杠 (\) 。
使用PHP htmlspecialchars() 把一些预定义的字符转换为 HTML 实体。
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
<?php
// 定义变量并默认设为空值
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["name"]))
{$nameErr = "Name is required";}
else
{$name = test_input($_POST["name"]);}
if (empty($_POST["email"]))
{$emailErr = "Email is required";}
else
{$email = test_input($_POST["email"]);}
if (empty($_POST["website"]))
{$website = "";}
else
{$website = test_input($_POST["website"]);}
if (empty($_POST["comment"]))
{$comment = "";}
else
{$comment = test_input($_POST["comment"]);}
if (empty($_POST["gender"]))
{$genderErr = "Gender is required";}
else
{$gender = test_input($_POST["gender"]);}
}
?>
PHP - 验证名称
以下代码将通过简单的方式来检测 name 字段是否包含字母和空格,如果 name 字段值不合法,将输出错误信息:$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name))
{
$nameErr = "只允许字母及空格";
}
PHP - 验证邮件
以下代码将通过简单的方式来检测 e-mail 地址是否合法。如果 e-mail 地址不合法,将输出错误信息:$email = test_input($_POST["email"]);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
$emailErr = "非法邮件地址";
}
PHP - 验证 URL
以下代码将检测URL地址是否合法 (以下正则表达式运行URL中含有破折号:"-"), 如果 URL 地址不合法,将输出错误信息:$website = test_input($_POST["website"]);
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website))
{
$websiteErr = "不合法的 URL";
}
相关文章推荐
- 使用PHP顶替JS有趣DOM
- php的Judy Array
- ThinkPHP函数详解:M方法
- php模式设计之 中介者模式
- ThinkPHP函数详解:D方法
- ThinkPHP框架使用Smarty模板引擎
- ThinkPHP函数详解:I方法
- ThinkPHP3.2.3 使用常量BUILD_CONTROLLER_LIST无法生成Controller
- Yii Framework 开发教程(3) 为应用添加日志
- php判断数据库是否连接成功的测试例子
- 调试Crypt_GPG的过程
- PHP 中session_start函数
- [php5.6]用mysqli连接SAE数据库
- ThinkPHP函数详解:C方法
- PHP执行exec调用svn up时候失败问题
- php中addslashes(),htmlspecialchars()
- The PHP cURL extension must be installed to use Guzzle 错误: ubuntu 安装 php-curl 拓展
- PHP调用另一文件中的类
- php处理字符串函数
- php的curl快速入门