PHP Validation Code...
Posted: Thu Nov 04, 2010 2:06 pm
I am having an issue (bug :/) with my validation for a contact form....when I try to enter the information into my form located here:
http://www.dcmedia-design.com/contact-us.html
I get an error telling me that there is a validation problem:
Warning: main(http://www.dcmedia-design.com/functions.php) [function.main]: failed to open stream: HTTP request failed! in /home3/danielp0/public_html/contact.php on line 15
Warning: main() [function.include]: Failed opening 'http://www.dcmedia-design.com/functions.php' for inclusion (include_path='.:/usr/lib64/php:/usr/lib/php') in /home3/danielp0/public_html/contact.php on line 15
Fatal error: Call to undefined function: validateemail() in /home3/danielp0/public_html/contact.php on line 38
..so with those, I have a contact.php form that has this:
<?php
/*
Credits: Bit Repository
URL: http://www.bitrepository.com/
*/
include 'http://www.dcmedia-design.com/config.php';
error_reporting (E_ALL ^ E_NOTICE);
$post = (!empty($_POST)) ? true : false;
if($post)
{
include 'http://www.dcmedia-design.com/functions.php';
$name = stripslashes($_POST['name']);
$email = trim($_POST['email']);
$subject = stripslashes($_POST['subject']);
$message = stripslashes($_POST['message']);
$error = '';
// Check name
if(!$name)
{
$error .= 'Please enter your name.<br />';
}
// Check email
if(!$email)
{
$error .= 'Please enter an e-mail address.<br />';
}
if($email && !ValidateEmail($email))
{
$error .= 'Please enter a valid e-mail address.';
}
// Check message (length)
if(!$message || strlen($message) < 1)
{
$error .= "Please enter your message.<br />";
}
if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $subject, $message,
"From: ".$name." <".$email.">\r\n"
."Reply-To: ".$email."\r\n"
."X-Mailer: PHP/" . phpversion());
if($mail)
{
echo 'OK';
}
}
else
{
echo '<div class="notification_error">'.$error.'</div>';
}
}
?>
and then a called file called function.php that shows this:
<?php
function ValidateEmail($email)
{
/*
(Name) Letters, Numbers, Dots, Hyphens and Underscores
(@ sign)
(Domain) (with possible subdomain(s) ).
Contains only letters, numbers, dots and hyphens (up to 255 characters)
(. sign)
(Extension) Letters only (up to 10 (can be increased in the future) characters)
*/
$regex = '/([a-z0-9_.-]+)'. # name
'@'. # at
'([a-z0-9.-]+){2,255}'. # domain & possibly subdomains
'.'. # period
'([a-z]+){2,10}/i'; # domain extension
if($email == '') {
return false;
}
else {
$eregi = preg_replace($regex, '', $email);
}
return empty($eregi) ? true : false;
}
?>
So another set of eyes on this would be helpful and GREAT!
Thank you!!!
http://www.dcmedia-design.com/contact-us.html
I get an error telling me that there is a validation problem:
Warning: main(http://www.dcmedia-design.com/functions.php) [function.main]: failed to open stream: HTTP request failed! in /home3/danielp0/public_html/contact.php on line 15
Warning: main() [function.include]: Failed opening 'http://www.dcmedia-design.com/functions.php' for inclusion (include_path='.:/usr/lib64/php:/usr/lib/php') in /home3/danielp0/public_html/contact.php on line 15
Fatal error: Call to undefined function: validateemail() in /home3/danielp0/public_html/contact.php on line 38
..so with those, I have a contact.php form that has this:
<?php
/*
Credits: Bit Repository
URL: http://www.bitrepository.com/
*/
include 'http://www.dcmedia-design.com/config.php';
error_reporting (E_ALL ^ E_NOTICE);
$post = (!empty($_POST)) ? true : false;
if($post)
{
include 'http://www.dcmedia-design.com/functions.php';
$name = stripslashes($_POST['name']);
$email = trim($_POST['email']);
$subject = stripslashes($_POST['subject']);
$message = stripslashes($_POST['message']);
$error = '';
// Check name
if(!$name)
{
$error .= 'Please enter your name.<br />';
}
// Check email
if(!$email)
{
$error .= 'Please enter an e-mail address.<br />';
}
if($email && !ValidateEmail($email))
{
$error .= 'Please enter a valid e-mail address.';
}
// Check message (length)
if(!$message || strlen($message) < 1)
{
$error .= "Please enter your message.<br />";
}
if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $subject, $message,
"From: ".$name." <".$email.">\r\n"
."Reply-To: ".$email."\r\n"
."X-Mailer: PHP/" . phpversion());
if($mail)
{
echo 'OK';
}
}
else
{
echo '<div class="notification_error">'.$error.'</div>';
}
}
?>
and then a called file called function.php that shows this:
<?php
function ValidateEmail($email)
{
/*
(Name) Letters, Numbers, Dots, Hyphens and Underscores
(@ sign)
(Domain) (with possible subdomain(s) ).
Contains only letters, numbers, dots and hyphens (up to 255 characters)
(. sign)
(Extension) Letters only (up to 10 (can be increased in the future) characters)
*/
$regex = '/([a-z0-9_.-]+)'. # name
'@'. # at
'([a-z0-9.-]+){2,255}'. # domain & possibly subdomains
'.'. # period
'([a-z]+){2,10}/i'; # domain extension
if($email == '') {
return false;
}
else {
$eregi = preg_replace($regex, '', $email);
}
return empty($eregi) ? true : false;
}
?>
So another set of eyes on this would be helpful and GREAT!
Thank you!!!