I'm once again in need of your help. I decided to write a newsletter subscription script, I know there are plenty of them
out there, but if I keep using other peoples' scripts all the time
than I'm never going to learn PHP.
I want the script to do the following
Validate the email address
Not to allow any email address with our domain
Check for the existence of MX record
Check for duplicates
I have problem with checking for duplicates, it will only read the
first line in the flat text file and if that is the same it will say "email
already in db" but if the same email address was further in the
file i.e. line 20 it would just add the email address and it will just
keep adding, it's like it's only comparing to the first line and then
it's exiting the
loop
$email = trim(strip_tags($_POST['email']));
#Validate email address
if(isset($_POST['Submit']) && (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))){
print "Your e-mail address appears to be in
an invalid format, or is not entered. Please close this window,
check your e-mail address in the subscribe box and try again.";
exit;
}
#Make sure email address is not from our domain
if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@ourdomain(\.[a-z]{2,3})$", $email)){
print ("<B> This email address doesn't seem to exist on ourdomain.com Servers </B>");
exit;
}
#Check for MX record
if(isset($_POST['Submit'])) {
list( $Username, $Domain ) = split ("@",$email);
if (!getmxrr($Domain, $MXHost)){
print("Email address domain cannot be found on the internet ");
exit;
}
}
// Check if email address is in the db
$fp = fopen("emailst.txt", "a+");
if(!$fp) {
print "Error! The file could not be opened";
exit; }
//Read the file and explode the string into array
if(!empty($email)){
$temp_emails = fread($fp, 1024);
$array_emails = explode("\r", $temp_emails);
$numb_array = count($array_emails);
foreach($array_emails as $list_emails) {
if(eregi("^$email(\r|\n)*", "$list_emails")){
print("<B>$email Exists in db</B>");
exit;
}
}
fwrite($fp, "$email"."\r\n");
if(!$fp) {
print "error! Couldn't write your email address to the db";
exit; }
else{
print "Your email address is added to our db";
exit;
}
fclose($fp);
}