Php Security filtering and instance tutorial

Source: Internet
Author: User
The PHP filter is an extension of PHP, which helps you easily and reliably verify strings and variables. in this way, you will never be afraid of the following statements :? Phpinclude ($ _ GET [#39; fil... the PHP filter is a PHP extension that helps you easily and reliably verify strings and variables. in this way, you will never be afraid of the following statements:

 

Even worse:

 

Variable filtering:Filter Variables. you can use the filter_var () function to verify whether a variable is an integer. for example:

$variable = 1122;echo filter_var($variable,FILTER_VALIDATE_INT);

Because this variable stores an integer, the code shows 1122. if our variable value is "a344", nothing will be printed on the screen because the verification fails.

Maybe you say this is nothing more than an interesting trick, but there are more. let's take a look. when we want to confirm that our variable is an integer, it ranges from 5 to 10. how can we do this:

 array("min_range"=>$minimum_value,"max_range"=>$maximum_value))); ?>

In this way, the variable must be within the range-as in the above example-the number 6 will be displayed on the screen.

Php provides a really good way to detect floating point values-the most useful thing for us is to check whether the values have two decimal points when building a shopping cart, for example, "31.53" is a valid floating point number.

$ Num = 31.53; if (filter_var ($ num, FILTER_VALIDATE_FLOAT) === false) {echo $ num. "is legal";} else {echo $ num "is an invalid floating point number ";}

How can I verify the format of a URL? You may need to check the RFC1738 specification, and then open your PHP text editor to write a class to verify it. This may require thousands of lines of code, right?

Well, in fact, PHP can use the URL filter to automatically help you do this:

$ Url = 'http: // www.phprm.com '; if (filter_var ($ url, FILTER_VALIDATE_URL) === FALSE) {echo $ url. "is not a legal URL
";} Else {echo $ url." is a legal URL.
";}

Now, you may say that what you said is useless to me: email verification. you must pass the regular expression to verify it, right? Wrong. PHP's FILTER_VALIDATE_EMAIL can make it very simple, even without any effort:

$ Email = 'wxysky @ phprm.com '; if (filter_var ($ email, FILTER_VALIDATE_EMAIL) === false) {echo $ email. "invalid";} else {echo $ email. "valid ";}

Now, do you think you can take the time to understand it? Email verification is a big challenge, especially for beginners. in my opinion, we should feel happy with them...

However, this is not enough. let's do something else, such as how to delete the HTML tag of a string:

 text

"; echo filter_var($string,FILTER_SANITIZE_STRING); ?>

In this way, we will get a "text" without tags ";

Conclusion: here we can see several examples of PHP filters. of course, the most important thing is to verify your code-as we all know, however, I just want to give it to you and the code-writing friends, whether you are a newbie or an expert-a positive verification method, to help you gradually travel from bad code to good code, your code must be the best, you can do it.


Address:

Reprinted at will, but please attach the article address :-)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.