使用perl过滤器

<html>
	<head>
    	<meta http-equiv="content-type" content="text/html" charset="utf-8">
        <title>register form</title>
        <style>
			.error{
				color:red;	
			}
		</style>
    </head>
    <body>    	 
<?php
	if(isset($_POST['submitted'])){
		/*
		*通过filter_input()设置FILTER_SANITIZE_FILTER过滤可以去除html标记
		*最后一个参数FILTER_FLAG_NO_ENCODE_QUOTES,它表示姓名里面的任何引号都不应该转换为相应的html实体		
		*/
		$name = filter_input(INPUT_POST,'name',FILTER_SANITIZE_STRING,FILTER_FLAG_NO_ENCODE_QUOTES);	
		if($name){
			echo "<p>Name:$name<br>\$_POST['name']:{$_POST['name']}</p>";
		}else{
			echo '<p class="error">Please enter your name</p>';
		}
		
		/*
		*检验邮箱
		*/
		$email = filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL);
		if($email){
			echo "<p>Email Address: $email</p>"	;			
		}else{
			echo '<p class="error">please enter your email address</p>';
		}
		
		/*
		*检验是否为整数
		*/
		$icq = filter_input(INPUT_POST,'icq',FILTER_VALIDATE_INT);
		if($icq){
			echo "<p>icq number:$icq</p>";
		}else{
			echo '<p class="error">please enter your icq </p>';	
		}
		
		/*
		*去除html标签,包括里面的引号
		*/
		$comments = filter_input(INPUT_POST,'comments',FILTER_SANITIZE_STRING);
		if($comments){
			echo "<p>Name:$comments<br>\$_POST['comments']:{$_POST['comments']}</p>";	
		}else{
			echo '<p class="error">please enter your email comments</p>';	
		}
	}
?>
	<form method="post" action="">
    	<fieldset>
    		<legend>注册表格</legend>
            <p>Name:<input type="text" name="name"></p>
            <p>Email Address:<input type="text" name="email"></p>
            <p>ICQ num:<input type="text" name="icq"></p>
            <p>Comments:<textarea name="comments" rows="5" cols="40"></textarea></p>
            <input type="submit" value="提交" name="submit">
            <input type="hidden" name="submitted" value="true">
    	</fieldset>
    </form>
  </body>
</html>


  • 函数filter_has_var()可以查看一组变量里面是否包含特定名称的变量,在这个脚本里,我们可以使用如下代码判断表单是否被提交。

    if(filter_has_var(INPUT_POST,'submitted')){

  • filter_input_array()可以过滤变量数组
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值