To be more helpful on the topic of validation and php here is some example functions i built to help get you started. You can throw these in a simple include file
<?php
/**
* Checks if a email address is in valid format or not
* @[member="param"] string $email
* @[member="param"] string $flags
* @return bool
*/
if (!function_exists('is_email')) {
function is_email($email, $flags = "") {
return (filter_var($email, FILTER_VALIDATE_EMAIL, $flags)) ? true : false;
}
}
/**
* Returns true if a valid integer is passed.
* @[member="param"] int $int
* @[member="param"] string $flags
* @return bool
*/
if (!function_exists('is_int')) {
function is_int($int, $flags = "") {
return (filter_var($int, FILTER_VALIDATE_INT, $flags)) ? true : false;
}
}
/**
* Checks if a IP address is valid or not
* @[member="param"] string $ip
* @[member="param"] string $flags
* @return bool
*/
if (!function_exists('is_ip')) {
function is_ip($ip, $flags = "") {
return (filter_var($ip, FILTER_VALIDATE_IP, $flags)) ? true : false;
}
}
/**
* Checks to see if a url is valid or not
* the url must contain http:// for
* this check to return true
* @[member="param"] string $url
* @[member="param"] string $flags
* @return bool
*/
if (!function_exists('is_url')) {
function is_url($url, $flags = "") {
return (filter_var($url, FILTER_VALIDATE_URL, $flags) && strpos(strtolower($url), "http://") !== false) ? true : false;
}
}
Then call a function like this
if(is_url("test") === true)
{
//Url is valid
}else{
//Url is not valid
}
Another good method to use when validating data is to store the error messages in an array like such
$error = array();//Error array
if(!isset($_POST['name']))
{
$error[] = "Please enter your name";
}
//SOME ERROR DISPLAYING EXAMPLES
//Get first error in the error array
if(count($error) > 0)
{
echo $error[0];
}
//Loop through entire array
if(count($error) > 0)
{
foreach($error as $errors):
echo $errors."<br />";
endforeach;
}