PHP Form URL and E-mail

PHP has been found to be very useful in handling the data received from the form built using HTML. PHP has provided various schemes to validate the URL and E-mail.

Validate Email:

The best way to check and validateĀ an email address is using PHP’s in-built filter_var() function. If the email address is not in its proper form i.e. “xyz@abc.com”, then this function returns false.

For example look at the following code for better understanding:

<html>
<head>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $emailErr = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echohtmlspecialchars($_SERVER["PHP_SELF"]);?>"
>
Name: <input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
?>
</body>
</html>

As we can see in the above code that the email and name is being checked for validity using failter_var() and preg_match().

The preg_match() checks whether the string is valid or not whereas the filter_val() checks for the validity of the email.


Validate URL:

For checking the validity of URL(uniform resource locator) we must pass the following regular expression to the preg_match() function:

“/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i”

Look at the following for better example:

<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
 $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {

if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
// check if URL address syntax is valid
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-
9+&@#\/%=~_|]/i",$website)) {
$websiteErr = "Invalid URL";
}
}

<h2>PHP URL Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echohtmlspecialchars($_SERVER["PHP_SELF"]);?>"
>

Website: <input type="text" name="website">
<span class="error"><?php echo $websiteErr;?></span>

<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<h2>Your Input:</h2>";

echo $website;

?>
</body>
</html>

As we can see in the above code that how a URL can be checked for its validity.

This how we can exploit the various inbuilt functions of PHP for our use.

Leave a Comment

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