PHP Form Data Validation and Filtering Techniques

This article explains how to use PHP functions to validate required fields, email and phone formats, and to filter HTML tags, special characters, and SQL injection risks, providing code examples that help ensure secure and reliable form data handling.

php Courses
php Courses
php Courses
PHP Form Data Validation and Filtering Techniques

As the internet evolves, forms are essential for user interaction, and ensuring their data security and validity is crucial for developers.

1. Data Validation

Data validation checks whether submitted data meets predefined rules. Common methods include:

1. Required field validation

Use empty() or isset() to determine if a field is empty.

Example code:

if (empty($_POST['username']) || empty($_POST['password'])) {
    echo "Username and password cannot be empty!";
    exit;
}

2. Email format validation

Validate email using filter_var() with FILTER_VALIDATE_EMAIL.

Example code:

$email = $_POST['email'];

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "Invalid email format!";
    exit;
}

3. Phone number format validation

Use a regular expression to verify phone numbers.

Example code:

$phone = $_POST['phone'];

if (!preg_match("/^1[3456789]\d{9}$/", $phone)) {
    echo "Invalid phone number format!";
    exit;
}

2. Data Filtering

Data filtering removes unwanted characters. Common methods include:

1. Strip HTML tags

Use strip_tags() to remove HTML.

Example code:

$content = $_POST['content'];
$filteredContent = strip_tags($content);

2. Escape special characters

Use htmlspecialchars() to escape special characters.

Example code:

$content = $_POST['content'];
$filteredContent = htmlspecialchars($content);

3. Prevent SQL injection

Use mysqli_real_escape_string() to escape input before database queries.

Example code:

$username = $_POST['username'];
$password = $_POST['password'];

$username = mysqli_real_escape_string($con, $username);
$password = mysqli_real_escape_string($con, $password);

These are common validation and filtering techniques; developers should choose methods based on specific business and security requirements to improve user experience and protect site security.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backenddata-filteringform-validation
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.