muffiin;10987046 wrote:
$firstname = stripslashes($_POST['firstname']);
$lastname = stripslashes($_POST['lastname']);
$email = stripslashes($_POST['email']);
Generally, you shouldn't use stripslashes. If the user does enter a backslash and you don't allow backslashes, inform them of this rather than just changing the data they entered. The same goes for any other characters you do not allow.
At this point it's also worth noting that a valid email address can actually contain backslashes, for example
"Abc\@def"@example.com
which you turn into another email address. The other email address is also valid, so in worst case you are spamming someone who didn't sign up for it.
muffiin;10987046 wrote:
function validate_email($email){
$exp = "^[a-z\'0-9]+([._-][a-z\'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$";
if(eregi($exp,$email)){
First off, the reg exp pattern will complain about perfectly email addresses. If you want to build an email address validator, I recommend doing it by going through the lexical tokens as shown here.
Also, the entire family or ereg functions have been deprecated since PHP 5.3 and should not be used. If you are using an older PHP version than 5.3, it's high time to upgrade. For pattern matching you should instead use the preg_ family, Pearl Compatible Regular Expressions.
The first thing to note about them is that the patterns supplied to preg_* need a starting and ending delimiter.
# example of ereg pattern
$ep = '[a-z]*';
# example of its preg counterpart, using # as delimiter
$pp = '#[a-z]*#';
# example of its preg counterpart, using / as delimiter
$pp = '/[a-z]*/';
There is a multitude of characters allowed as pattern delimiters. Since I quite often have / in the actual patterns, I prefer not using / as delimiter since I'd then have to escape those that are inside the pattern.
The second thing to note is that there are no pregi_ versions. If you want a case insensitive match, you add an i after the ending pattern delimiter where the options go
# case insensitive pattern
$p = '#[a-z]*#i
You should also be careful to make your functions do what they're supposed to, and nothing else. validate_email() should validate email addresses, not write files and perform redirects.
function validate_email($email)
{
# all logic stripped away, this is just an example for structure
if ($invalid_domain)
{
return -1;
}
elseif ($invalid_email)
{
return 0;
}
else
{
return 1;
}
}
If you havn't allready followed the link above on email address validation you might want to do so now. That page has a link to an updated version which also deals with newer RFCs and Errata, and you can download it for free.
The only thing left to add is the MX handling.
How to write to the file on one line? Well, wrap the code in a function and it's a one liner where you use the function.
function save_email($file, $firstname, $lastname, $email)
{
# Don't just die with a blank page if you don't have to
$f = fopen($file, 'a'); /* or die('An unexpected error occurred. Please try again') */
if (!$f)
{
return false;
}
fwrite($f, "\n");
fwrite($f, $firstname);
fwrite($f, " ");
fwrite($f, $lastname);
fwrite($f, " ");
fwrite($f, $email);
fclose($f);
return true;
}
And now that the building blocks are there
$feedback = array();
$errors = array();
# All characters should be a-zA-Z
$name_pattern = '#[a-z]+#i';
if (!preg_match($name_pattern, $firstname))
{
$errors[] = 'First name can only contain characters A-Z and a-z';
}
if (!preg_match($name_pattern, $lastname))
{
$errors[] = 'Last name can only contain characters A-Z and a-z';
}
if (($validation_result = validate_email($email)) == -1)
{
$errors[] = 'Invalid domain';
}
elseif ($validation_result == 0)
{
$errors[] = 'Invalid email';
}
# No errors at all? Go ahead and write the file
if (count($errors) == 0)
{
if (!save_email('names.txt', $firstname, $lastname, $email))
{
$errors[] = 'Your contact information could not be saved';
}
else
{
$feedback[] = 'Thank you!';
}
}
# Put this after <html><head>...</head><body>..., when you get to the content part of the page,
# that is, somewhere before the form the user fill out.
# With css style rules, you can make feedback and errors look different
foreach ($errors as $e)
echo '<div class="error">'.$e.'</div>';
foreach ($feedback as $f)
echo '<div class="feedback">'.$e.'</div>';
muffiin;10987046 wrote:
I would just put this in a database, but I haven't gotten to that point in my education yet. I know that would make organization and displaying much easier.
Now that the code to save an email address is contained in one function, it doesn't matter where and how you call it. There's only one place to change it, which makes it easy to go from file storage to database storage.