PHP Filters

🔍 PHP Filters Tutorial

PHP filters are used to validate and sanitize user input. Filters ensure that the data received from users is safe and in the expected format, preventing errors or security vulnerabilities.


1️⃣ Types of Filters

Type Description
Validation Check if input is valid (e.g., email, URL, int)
Sanitization Clean input by removing unwanted characters

2️⃣ Using filter_var()

The filter_var() function validates or sanitizes a single variable.

Syntax:


 


a) Validate Email


 


b) Validate URL


 


c) Validate Integer


 

Optional Range Check:


 


d) Sanitize String


 

Note: FILTER_SANITIZE_STRING removes HTML tags and special characters.


e) Sanitize Email


 


f) Sanitize URL


 


3️⃣ Using filter_input()

filter_input() validates or sanitizes form input or query parameters directly.


 

  • INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, INPUT_ENV are possible sources.


4️⃣ Key Points

  1. Validation filters → check if data is correct.

  2. Sanitization filters → clean data for safe use.

  3. filter_var() → works on single variable.

  4. filter_input() → works on input sources like POST/GET.

  5. Always validate and sanitize user input to prevent security issues.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *