PHP Tutorial #23 Filters Part 1 " FILTER_VALIDATE "

Image Source

Repository

https://github.com/php/php-src  

What Will I Learn?

I will learn the php filters this is the first part , we will see what's the filter and we will take a 6 important items from the list of " FILTER_VALIDATE ".

  • What's the filter in PHP.
  • FILTER_VALIDATE_EMAIL and FILTER_VALIDATE_FLOAT.
  • FILTER_VALIDATE_INT and FILTER_VALIDATE_IP.
  • FILTER_VALIDATE_REGEXP and FILTER_VALIDATE_URL.

Requirements

System Requirements:
  • Server support PHP , Xampp or Wamp for example
  • An IDE like Sublime text.
  • Browser (Chrome for example)  
OS Support for PHP
  • Windows
  • macOS
  • Linux

Required Knowledge

  • HTML language
  • CSS language

Difficulty

  •  Intermediate 

Description

In this tutorial we will learn filtering, why we need to use filters.

In HTML we can directly verify the value entered by the user, HTML5 has offered new types to test whether the value or the form of the value you have entered is correct or not, and we can use the "title" attribute to display a message directly for the user.

But the problem is that the user can modify our inputs, for that we need to check in the server not in the client side.

PHP gives a very good idea to verify the values is " the filtering " , it  has two type " FILTER_VALIDATE " and " FILTER_SINITIZE ".

Today we will learn the " FILTER_VALIDATE " , these elements, options and flags.

1-FILTER_VALIDATE_EMAIL

If we are in a registration form for example and we have an input for the email, to ensure that the user will enter an email in this input  we can use the email type in the attribute type in the tag "input ", and the value" on "for the attribute" required " and it will force the user to enter a correct email, but if we are with a developer he can easily change the type of email via" text "and the value that he entered will be stored in your database, it's a problem.

For that we have in PHP the " FILTER_VALIDATE_EMAIL " which will filter the email entered by the user, if the email contains the sign "@" and if has the form [email protected] or not and it will return a boolean value "true or false ".

In general this validates the email address according to the syntax defined by " RFC 822 ", with the exception of comments, padding with spaces, and dot-less domain names that are not supported.

We can then return an error message for the person to enter a correct email.

This filter has just one flag " FILTER_FLAG_EMAIL_UNICODE " and it hasn't an option.

The example that I want to do is , I have an input " Email " I will get the value of the input using the " $_POST " array and I will use the " FILTER_VALIDATE_EMAIL "

When I enter for example " [email protected] " this is the result

And when I enter just the word " alexendre " this is the result

2-FILTER_VALIDATE_FLOAT

If we need to do a mathematical calculation and we need only decimal numbers, for that we need to filter the value entered by the user, if he entered a value with a character for example or something else.

So the " FILTER_VALIDATE_FLOAT " validates a decimal number, and converts it to a decimal number if successful.

It has the "decimal" option to select the decimals and the " FILTER_FLAG_ALLOW_THOUSAND " flag to accept the "," comma as a thousands separator and point as a decimal separator.

To force the user to enter a float number I will use the " FILTER_VALIDATE_FLOAT " with the flag " FILTER_FLAG_ALLOW_THOUSAND " if he wants to enter comma as a thousands separator, this is the code

When I enter 12,000.25 this is the result

And when I enter a string or other characters , it will return an error

3-FILTER_VALIDATE_INT

As always we are not sure that the user will enter a correct value always we think in the worst case, imagine you have an input of age, the user will enter "25b" for example it will make a mistake, and for that we still need to filter all the inputs.

The " FILTER_VALIDATE_INT " validates an integer possibly in a given interval, and converts it to an integer on success.

It has two options "the max range" or the maximum number that can taken for this input, and "the min range" that is the lowest or the minimum number that the user can enter.

And by default it doesn't accept numbers in octal and hexadecimal representation, but we have two flags to allow the user to enter these representations in this input " FILTER_FLAG_ALLOW_OCTAL " and " FILTER_FLAG_ALLOW_HEX ".

The example that I want to do is the age , the minimum age is 18 and the maximum is 150 , I will do it with the " FILTER_VALIDATE_INT ", this is the code

And when I enter 20 for example this is the result

And when I enter another number 10 for example less then the min range value it will return an error

4- FILTER_VALIDATE_IP

For a network an IP address or (internet protocol) is a unique number that allows a computer to communicate in a network.

This number has two versions , ipv4 and ipv6, to manually check the value that the user entered is very difficult and for that PHP offers a very important type of filtering 'FILTER_VALIDATE_IP'.

This filter validates an IP address possibly IPv4 or IPv6, and outside private or reserved ranges.

It has no options and it has 4 flags :

FILTER_FLAG_IPV4 : to filter an IPV4, the IPv4 refers to version 4 of the Internet Protocol (IP), this is the version currently the most used in the world to attach an IP address to a computer. The latter takes the form of a succession of decimal digits (4 with IPv4) as 182.23.178.44.

FILTER_FLAG_IPV6 : to filter an IPV6, the IPv6 addresses are 128-bit coded.

The 128 bits of the address are divided into 8 groups of 16 bits represented by 4 hexadecimal digits and separated by ":" two points.

And for the last two optional flags, for doesn't allow private and non-restricted addresses. 

FILTER_FLAG_NO_PRIV_RANGE and FILTER_FLAG_NO_RES_RANGE.

I will enter an IP version 4 and another IP version 6 with the flags , this is the code

I will enter the IP : 172.1.1.1 , it's a correct IP and this is the result

And when I enter an IP version 6 this is the result, because I have allowed the two versions 

5- FILTER_VALIDATE_REGXP

The regular expressions makes it possible to search for occurrences (that's to say a sequence of characters corresponding to what one is looking for) thanks to a series of special characters.

To filter that the user entered correctly a regular expression, we needs to use " FILTER_VALIDATE_REGEXP ".

This filter validates a value with a regular expression, it has an option that is the regular expression to compare it with the string entered by the user and it has no flag.

The example that I want to do is , to compare the regular expression " [0-9] " with the value entered by the user

I have passed the regular expression " [0-9] " to the filter_var() method as an option , and when I enter it this is the result

And when I enter any other value or other regular expression this is the result

6- FILTER_VALIDATE_URL

As we have a type "Email" we also have a type "URL" in HTML, we can use it and will check approximate  the URL entered by the user, but to correct the URL  we will use " FILTER_VALIDATE_URL ".

This filter validates a URL possibly with required components, be careful a valid URL may not specify the http: // protocol, so further validation will be required to determine if the URL uses an expected protocol " ssh: // " or " mailto :. " .

This filter will only find valid ASCII URLs, it has no option but it has two flags :

FILTER_FLAG_PATH_REQUIRED : By default the URL doesn't accept the form of path, but by using this flag it will be able to accept this type of URL.

FILTER_FLAG_QUERY_REQUIRED : often we send information with the method " GET " , in the URL will contain a request with the form ' ? name = "alexendre" ' and that is not acceptable, to allow it we will use this flag.

I will check the URL entered by the user , if it's correct or no , for that I will use the " FILTER_VALIDATE_URL " with the two flags

And when I enter the URL of facebook with a PATH , this is the result

And if I enter the URL of facebook again with a query , this is the result

And if I entered something without a protocol " HTTP " or any other protocol , it will return an error.

Video Tutorial

Curriculum

Proof of Work Done

https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/filterC.php

https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/filter.html

Sort:  

Hi @alexendre-maxim,

Thank you for your contribution to the video category.

I can see you making great effort in the delivery of the tutorial. You are spending time with correct diction and making emphasis where it is needed in the tutorial.

As for the text posting, try to post in short simple sentences. In some of the longer sentences, they become run on.

Looking forward to your next contribution.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

I will try to minimize the maximum to be clear and easy to follow, thank you @rosatravels

Thank you for your review, @rosatravels! Keep up the good work!

Congratulations @alexendre-maxim! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You received more than 1000 as payout for your posts. Your next target is to reach a total payout of 2000

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Do not miss the last post from @steemitboard:

Carnival Challenge - Here are the winners
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Hi @alexendre-maxim!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @alexendre-maxim!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.24
TRX 0.24
JST 0.038
BTC 95135.46
ETH 3281.35
USDT 1.00
SBD 3.37