Published on: 2005-08-25 - Views: 27403

Digg! del.icio.us Furl reddit spurl bloglines ma.gnolia.com Yahoo MyWeb technorati blogmarks blinklist pixelgroovy Share this tutorial on tutorialicio.us! simpy
In this tutorial we'll see how to validate several different form fields, and if the verification fails, how to redirect users to custom error pages.

This code must be put into a file named "livereg.php" to work with the form we have just created.

To see the previous part of this tutorial (to create the HTML form used to send the values) click HERE

The code is explained line by line:
<?php 
$url = \'http://www.yourdomain.com/accountok.php\'; // Where to redirect 
//the user after that form has been processed successfully. Now we will 
//get the values sent by the form: to get a value you can generally access 
//it with $_POST[\'valuename\'] where valuename is the string you put into 
//the value of the the field you are accessing
$user = $_POST[\'username\'];//get username from form
$pass = $_POST[\'password\'];//get password from form
$pass2 = $_POST[\'password2\'];//get password2 from form
$name = $_POST[\'name\'];//get name from form
$domain = $_POST[\'domain\'];//get domain from form
$zip = $_POST[\'zip\'];//get zip from form
$city = $_POST[\'city\'];//get city from form
$email = $_POST[\'email\'];//get email from form
$state = $_POST[\'state\'];//get state from form
$country = $_POST[\'country\'];//get country from form
$address = $_POST[\'address\'];//get address from form
$phone = $_POST[\'phone\'];//get phone from form

//now that we have picked all the values we need from the form we can 
//start checking them one at a time

//PASSWORD VERIFICATION
if (($pass)!=($pass2)) //if the values stored in the 2 variables are 
//different we redirect the users to a previously created error page
{
	header("Location: error-pwverify.php"); //the user will be sent to this page
	Die();
}
//EMAIL VERIFICATION
function CheckMail($email) // this function checks if the email format is ok, 
//if the chars are allowed, if there are enough chars in the TLD learn more 
//about eregi function here: http://www.php.net/manual/en/function.eregi.php
{
if(eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\.[a-z]{2,4}$",$email)) 
	{ 
		return true;
	}
	else 
	{ 
		return false; 
	}
}
if ((empty($email)) || (!CheckMail($email))) //here we check if the email is 
//empty and if the previous function controls are passed, if there are 
//errors the user is sent to a previously prepared custom error page
{
	header("Location: error-email.php"); //the user will be sent to this page
	Die();
}
//ZIP CODE VERIFICATION
function CheckZip($zip) 
{
	if (eregi("[0-9]", $zip)) //here we check if the ZIP code contains only numbers
	{
		return true;
	}
	else 
	{ 
		return false; 
	}
}
if ((empty($zip)) || (!CheckZip($zip)) || (strlen($zip)!=5))
//if the ZIP code is empty, the CheckZip function fails or the code length is 
//different from 5 chars the user is sent to a previously prepared custom error page
{
	header("Location: error-zip.php"); //the user will be sent to this page
	Die();
}
//NAME VERIFICATION
//the name and the following fields don\'t allow us to make strict 
//verifications by the way the user has to have a name so if the name 
//is empty the user is sent to a previously prepared custom error page
if (empty($name))
{
	header("Location: error-name.php"); //the user will be sent to this page
	Die();
}
//CITY VERIFICATION
//exactly the same as the name verification
if (empty($city))
{
	header("Location: error-city.php"); //the user will be sent to this page
	Die();
}
//STATE/PROVINCE VERIFICATION
//exactly the same as the name verification
if (empty($state))
{
	header("Location: error-state.php"); //the user will be sent to this page
	Die();
}
//COUNTRY VERIFICATION
//exactly the same as the name verification
if (empty($country))
{
	header("Location: error-country.php"); //the user will be sent to this page
	Die();
}
//ADDRESS VERIFICATION
//exactly the same as the name verification
if (empty($address))
{
	header("Location: error-address.php"); //the user will be sent to this page
	Die();
}
//USER AND PASSWORD LENGTH CHECK
$min_lenngth = 6; //this value is the minimal length that we desire our passwords 
//to be if the username or the password is shorter than 6 chars the user is sent 
//to a previously prepared custom error page
if(strlen($user) < $min_lenngth || strlen($pass) < $min_lenngth)
{
	header("Location: error-pwshort.php"); //the user will be sent to this page
	Die();
}
//NOTE: insert here the code between //****************// and //****************// 
//in the following part of the tutorial if you want to add these values to your 
//database
//NOTE:don\'t use the following code line (erase it) if you inserted here the code 
//to add the values to the database
//if all our checks are passed we go to the url assigned at the top to the variable url
echo <META HTTP-EQUIV=Refresh CONTENT="0; URL=.$url.">;
?>
This tutorial continues HERE, see how to insert these values into your database.