New PHP User trying to implement E-mail Address Validation

212 views
Skip to first unread message

Keith Stiles

unread,
Jul 31, 2009, 3:47:40 PM7/31/09
to php-email-address-validation
Group,

I'm attempting to use the e-mail validation on a form post processing
page. But, I'm such a newbie that I'm not sure how to get this to
work. My php code is included below. How would I actually validate
the $email variable because everything I've tried has failed.

?php

session_start();
include("address to database connection here");

$db_name = "ContEd";
$table_name = "Students";

$SSN = check_input($_POST[SSN]);
$Fname = check_input($_POST[Fname],"Please enter your first name.");
$Mname = check_input($_POST[Mname],"Please enter your middle name.");
$Lname = check_input($_POST[Lname],"Please enter your last name.");
$OtherNames = check_input($_POST[OtherNames]);
$Street = check_input($_POST[Street],"Please enter your mailing
address.");
$City = check_input($_POST[City],"Please enter your city.");
$State = check_input($_POST[State]);
$Zip = check_input($_POST[Zip],"Please enter your 6-digit zip code.");
$DOB_month = check_input($_POST[DOB_month]);
$DOB_day = check_input($_POST[DOB_day]);
$DOB_year = check_input($_POST[DOB_year]);
$Gender = check_input($_POST[Gender]);
$Hispanic = check_input($_POST[Hispanic]);
$Ethnic = check_input($_POST[Ethnic]);
$Hphone = check_input($_POST[Hphone],"Please enter a phone number.");
$Wphone = check_input($_POST[Wphone]);
$CellPhone = check_input($_POST[CellPhone]);
$email = ($_POST[email]);
$EducLevel = check_input($_POST[EducLevel]);
$EmpStatus = check_input($_POST[EmpStatus]);
$LEStatus = check_input($_POST[LEStatus]);
$LEOName = check_input($_POST[LEOName]);
$Comments = check_input($_POST[Comments]);

$connection = mysql_connect ($web_name, $web_user,$web_pass) or die
("Couldn't connect.");
$db = mysql_select_db ($db_name, $connection) or die ("Couldn't select
database.");

$query = "
INSERT INTO Students (SSN, Fname, Mname, Lname, OtherNames, Street,
City, State, Zip, DOB_month, DOB_day, DOB_year, Gender, Hispanic,
Ethnic, Hphone, Wphone, CellPhone, email, EducLevel,
EmpStatus, LEStatus, LEOName, Comments)
VALUES ('$SSN', '$Fname', '$Mname', '$Lname', '$OtherNames',
'$Street', '$City', '$State', '$Zip', '$DOB_month', '$DOB_day',
'$DOB_year',
'$Gender', '$Hispanic', '$Ethnic', '$Hphone', '$Wphone',
'$CellPhone', '$email', '$EducLevel', '$EmpStatus', '$LEStatus',
'$LEOName', '$Comments')
";

$result = @mysql_query($query, $connection) or die("Couldn't add to:
$db_name, $table_name.");


header('Location: http://www.blueridge.edu/files/coned_thank_you.php');

function check_input($data, $problem = '')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
die($problem);
}
return $data;
}


?>

Chris (Jesdisciple)

unread,
Aug 2, 2009, 2:30:38 PM8/2/09
to php-email-address-validation
First of all, always quote your strings... This:

$email = ($_POST[email]);

should be this:

$email = ($_POST['email']);

or this (not sure what the parens are for):

$email = $_POST['email'];

As explained in the comments at
http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing

// Works, but PHP looks for a constant named banana first, as
described below.
echo "A banana is {$fruits[banana]}.";

// This is wrong for the same reason as $foo[bar] is wrong outside a
string.
// In other words, it will still work, but only because PHP first
looks for a
// constant named foo; an error of level E_NOTICE (undefined constant)
will be
// thrown.
echo "This is wrong: {$arr[foo][3]}";

Now for your question... See http://code.google.com/p/php-email-address-validation/wiki/Download_and_Usage
for instructions on usage. A pretty simple example is available
there, so I don't know what I can add to that. Asking a specific
question is a good way to get me going... An interesting question can
get me typing a whole article.

Chris (Jesdisciple)

unread,
Aug 2, 2009, 2:37:57 PM8/2/09
to php-email-address-validation
Here's a more relevant explanation of constants versus strings... I
thought they had deleted it at first.

http://www.php.net/manual/en/language.constants.syntax.php
If you use an undefined constant, PHP assumes that you mean the name
of the constant itself, just as if you called it as a string (CONSTANT
vs "CONSTANT"). An error of level E_NOTICE will be issued when this
happens. See also the manual entry on why $foo[bar] is wrong (unless
you first define() bar as a constant). If you simply want to check if
a constant is set, use the defined() function.

On Aug 2, 1:30 pm, "Chris (Jesdisciple)" <jesdisci...@gmail.com>
wrote:
> First of all, always quote your strings...  This:
>
> $email = ($_POST[email]);
>
> should be this:
>
> $email = ($_POST['email']);
>
> or this (not sure what the parens are for):
>
> $email = $_POST['email'];
>
> As explained in the comments athttp://www.php.net/manual/en/language.types.string.php#language.types...
>
> // Works, but PHP looks for a constant named banana first, as
> described below.
> echo "A banana is {$fruits[banana]}.";
>
> // This is wrong for the same reason as $foo[bar] is wrong  outside a
> string.
> // In other words, it will still work, but only because PHP first
> looks for a
> // constant named foo; an error of level E_NOTICE (undefined constant)
> will be
> // thrown.
> echo "This is wrong: {$arr[foo][3]}";
>
> Now for your question...  Seehttp://code.google.com/p/php-email-address-validation/wiki/Download_a...
Reply all
Reply to author
Forward
0 new messages