php 过滤和验证PHP

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 过滤和验证PHP相关的知识,希望对你有一定的参考价值。

<?php
/*
	// Check for posted data
	if(filter_has_var(INPUT_POST, 'data')){
		echo 'Data Found';
	} else {
		echo 'No Data';
	}

	if(filter_has_var(INPUT_POST, 'data')){
		$email = $_POST['data'];

		// Remove illegal chars
		$email = filter_var($email, FILTER_SANITIZE_EMAIL);
		echo $email.'<br>';

		if(filter_var($email, FILTER_VALIDATE_EMAIL)){
			echo 'Email is valid';
		} else {
			echo 'Email is NOT valid';
		}
	}

		#FILTER_VALIDATE_BOOLEAN
		#FILTER_VALIDATE_EMAIL		
		#FILTER_VALIDATE_FLOAT		
		#FILTER_VALIDATE_INT			
		#FILTER_VALIDATE_IP			
		#FILTER_VALIDATE_REGEXP		
		#FILTER_VALIDATE_URL

		#FILTER_SANITIZE_EMAIL	
		#FILTER_SANITIZE_ENCODED	
		#FILTER_SANITIZE_NUMBER_FLOAT	
		#FILTER_SANITIZE_NUMBER_INT	
		#FILTER_SANITIZE_SPECIAL_CHARS	 
		#FILTER_SANITIZE_STRING		
		#FILTER_SANITIZE_URL

	
		$var = 'kdkejdked';

		if(filter_var($var, FILTER_VALIDATE_INT)){
			echo $var. ' is a number';
		} else {
			echo $var. ' is NOT a number';
		}

		$var = '<script>alert(1)</script>';
		//echo $var;
		echo filter_var($var, FILTER_SANITIZE_SPECIAL_CHARS);
		//var_dump(filter_var($var, FILTER_SANITIZE_NUMBER_INT));
	

	$filters = array(
		"data" => FILTER_VALIDATE_EMAIL,
		"data2" => array(
			"filter" => FILTER_VALIDATE_INT,
			"options" => array(
				"min_range" => 1,
				"max_range" => 100
			)
		)
	);

	print_r(filter_input_array(INPUT_POST, $filters));

	*/

	$arr = array(
		"name" => "brad traversy",
		"age" => "133",
		"email" => "brad@gmail..com"
	);

	$filters = array(
		"name" => array(
			"filter" => FILTER_CALLBACK,
			"options" => "ucwords"
		),
		"age" => array(
			"filter" => FILTER_VALIDATE_INT,
			"options" => array(
				"min_range" => 1,
				"max_range" => 120
			)
		),
		"email" => FILTER_VALIDATE_EMAIL
	);

	print_r(filter_var_array($arr, $filters));

?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
	<input type="text" name="data">
	<input type="text" name="data2">
	<button type="submit">Submit</button>
</form>

以上是关于php 过滤和验证PHP的主要内容,如果未能解决你的问题,请参考以下文章

使用 PHP 过滤器(Filter)进行严格表单验证

2017.8.14

php [使用操作和过滤器自定义结帐字段]验证WooCommerce自定义结帐字段

php 基础复习 2018-06-20

php安全 过滤验证转义

PHP 使用过滤器将HTTP身份验证添加到应用程序