...

View Full Version : Problem with email validation code



johnnybananas
09-22-2005, 08:50 AM
I am having a hard time figuring out how to make my email validation code work and then submit user email input. In other words, if I type in the email johnnysmith@, the code tells me "You did not enter a valid e-mail address.
Please return to the form." so the validation code works perfect, right? However, if I press the "return to the form" link and correct the email to johnnysmith@yahoo.com, somehow the code keeps on looping and sends me back and forth between the subscribe.php file and the validation process. I tried inserting this code between the head and body tags of subscribe.php file, into vars.php, and the news.php file to fix it to no avail. I would really appreciate some help. Here's the code to validate the email.

<?
//This line checks the email validation
#The function

function checkemail($news_email){ //First, we check $news_email, validating that there's one @ symbol, and that the lengths are right
return preg_match("/^[^\s()<>@,;:\"\/\[\]?=]+@\w[\w-]*(\.\w[\w-]*)*\.[a-z]{2,}$/i",$news_email);
}

#The Error-Checker

$news_email = $_POST['email'];
if((!$news_email) OR (!checkemail($news_email))){
echo 'You did not enter a valid e-mail address.<br> Please <a href=javascript:history.back(-1)>return to the form</a>.';
die; //stop the PHP from continuing

?>

I have the following files:
--------------
header.php
footer.php
news.php - this contains all the functions to run the newsletter including adding subscribers, deleting subscribers and the administration to send letters.
subscribe.php - contains the subscriber form
subscribers.txt - this is the data file that will contain all email addresses of subscribers.
unsubscribe.php - contains the unsubscribe form
vars.php - this file contains the variables for the newsletter
--------------

header.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>e-Newsletter</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<div align="center">
<p><font size="3" face="Arial, Helvetica, sans-serif"><strong>DowneyLanding.com Newsletter</strong></font></p>
<table width="400" border="0" cellspacing="0" cellpadding="4">
<tr>
<td>

footer.php
</td>\0\0
</tr>
</table>
<p><font size="2" face="Arial, Helvetica, sans-serif">Copyright 2005. somedomain.com. All
rights reserved.</font></p>
<p><strong><font size="3" face="Arial, Helvetica, sans-serif"></font></strong></p>
</div>
</body>
</html>

news.php
<? // This line includes the "vars.php" file
include("vars.php");

//This line starts the email subscription process
if (!file_exists($filelocation)) {

$newfile = fopen($filelocation,"w+");

fclose($newfile);

}

$newfile = fopen($filelocation,"r");

$content = fread($newfile, filesize($filelocation));

fclose($newfile);

$content=stripslashes($content);

$out="";

$lines = explode("%",$content);

for ($key=1;$key<sizeof($lines);$key++){

if ($lines[$key] != $news_email){

$out .= "%".$lines[$key];

}

else {

$found=1;

}

}







if ($action=="sign"){

if ($found==1){

include ("header.php");

echo "<div align=\"center\"><font face=\"Arial,Helvetica,Geneva,Swiss,SunSans-Regular\" size=\"2\"><b>".$sorrysignmessage.$news_email."</b></font><br><br>".$back."</DIV>";

include ("footer.php");

$disp="no";

}

else {

$disp="no";

$newfile = fopen($filelocation,"a+");

$add = "%".$news_email;

fwrite($newfile, $add);

fclose($newfile);

include ("header.php");

echo "<div align=\"center\"><font face=\"Arial,Helvetica,Geneva,Swiss,SunSans-Regular\" size=\"2\"><b>".$subscribemessage.$news_email."</b></font><br><br>".$back."</DIV>";

include ("footer.php");

$submailheaders = "From: $lettername\n";

$submailheaders .= "Reply-To: $youremail\n";

mail ($news_email,$lettername." subscription",$subscribemail,$submailheaders);

}

}





if ($action=="delete"){

$disp="no";

if ($found == 1){

$newfile = fopen($filelocation,"w+");

fwrite($newfile, $out);

fclose($newfile);

include ("header.php");

echo "<div align=\"center\"><font face=\"Arial,Helvetica,Geneva,Swiss,SunSans-Regular\" size=\"2\"><b>".$unsubscribemessage.$news_email."</b></font><br><br>".$back."</DIV>";

include ("footer.php");

$disp="no";

}

if ($found != 1){

include ("header.php");

echo "<div align=\"center\"><font face=\"Arial,Helvetica,Geneva,Swiss,SunSans-Regular\" size=\"2\"><b>".$failedunsubscriptionmessage.$news_email."</b></font><br><br>".$back."</DIV>";

include ("footer.php");

$disp="no";

}

}





if ($admin == $pass){



if ($send != "yes" && $send != "test"){

print'<form method="post"><input type="hidden" name=admin value='.$pass.'><input type="hidden" name=send value=yes>

<br><font face=\"Arial,Helvetica,Geneva,Swiss,SunSans-Regular\" size=\"2\"><b>Newsletter editor:</b></font><br><br>

Subject:<br>

<input type="text" name="subject" size=20><br>

Message:<br>

<textarea cols=50 rows=10 wrap="virtual" name="message"></textarea><br><br>

<input type="submit" value="send">

</form>';

}



$mailheaders = "From: $lettername\n";

$mailheaders .= "Reply-To:$youremail\n";



if ($send == "yes"){

$message=stripslashes($message);

$subject=stripslashes($subject);

$lines = explode("%",$content);

for ($key=1;$key<sizeof($lines);$key++){

mail ($lines[$key],$subject,$message,$mailheaders);

}

print "<font face=\"Arial,Helvetica,Geneva,Swiss,SunSans-Regular\" size=\"2\"><b>The following email has been sent!</b></font>";

print "<pre>$mailheaders\n$subject\n$message</pre>";

}

}





if ($admin != $pass && $disp != "no"){



print'

<table width="337" border="1" cellspacing="0" cellpadding="0" align="center" bordercolor="#000000">

<tr>

<td>



<table cellpadding="2" cellspacing="0" width="335" bgcolor="#D3E3E7" align="center" bordercolor="#000000">

<form method="post" action="news.php">

<tr>

<td colspan="3">

<div align="center">

<p>&nbsp;</p><p>&nbsp;</p>

<p><font face="Arial, Helvetica, sans-serif" size="2">Authorization

Failed</font></p>

</div>

</td>

</tr>

<tr>

<td colspan="3">

<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>

</td>

</form>

</table>

</td>

</tr>

</table>

';

}



?>

subscribe.php
<html>
<head>
<title>e-Newsletter</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF">
<div align=center">
<table width="337" border="1" cellspacing="0" cellpadding="0" align="center" bordercolor="#666666">
<tr>
<td> <table cellpadding="4" cellspacing="0" width="335" bgcolor="#EEEEEE" align="center" bordercolor="#000000">
<form method="post" action="news.php">
<tr>
<td colspan="3"> <div align="center"><font face="Verdana" size="3" color="#990000"><strong>Get
great offers, delivered to you!</strong></font></div></td>
</tr>
<tr>
<td width="141"> <div align="right"><font face="Verdana" size="2">Email
address: </font></div></td>
<td colspan="2"> <input type="text" name="news_email" size="25">
</td>
</tr>
<tr>
<td width="141">&nbsp;</td>
<td width="24"> <div align="right">
<input type="radio" name="action" value="sign" checked>
</div></td>
<td width="184"><font face="Verdana" size="2">subscribe</font></td>
</tr>
<tr>
<td colspan="3"> <center>
<input type="submit" value="send it!" name="submit">
</center></td>
</form>
</table></td>
</tr>
</table>
</div>
</body>
</html>

subscribers.txt
Blank file for now until subscribers sign-up.

unsubscribe.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Unsubscribe</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF">
<div align="center">
<table width="337" border="1" cellspacing="0" cellpadding="0" align="center" bordercolor="#666666">
<tr>
<td> <table cellpadding="4" cellspacing="0" width="335" bgcolor="#EEEEEE" align="center" bordercolor="#000000">
<form method="post" action="news.php">
<tr>
<td colspan="3">&nbsp;</td>
</tr>
<tr>
<td width="141"> <div align="right"><font face="Verdana" size="2">Email
address: </font></div></td>
<td colspan="2"> <input type="text" name="news_email" size="25">
</td>
</tr>
<tr>
<td width="141">&nbsp;</td>
<td width="24"> <div align="right">
<input type="radio" name="action" value="delete">
</div></td>
<td width="184"><font face="Verdana" size="2">unsubscribe<br>
</font></td>
</tr>
<tr>
<td colspan="3"> <center>
<input type="submit" value="send it!" name="submit">
</center></td>
</form>
</table></td>
</tr>
</table>
</div>
</body>
</html>

vars.php
<?
//Edit these four items for your website information

$pass="adminpass";
$filelocation="subscribers.txt";
$lettername="e-Newsletter";
$youremail="someEmail@yahoo.com";


//Newsletter messages. Be careful not to affect coding or newsletter will not work

$welcomemessage = "<CENTER><font face=\"Verdana\" size=\"2\">Subscribe to our Newsletter</CENTER></font>";
$sorrysignmessage = "<CENTER>Sorry, a listing in our database already exists for that email address</CENTER>";
$subscribemessage = "<CENTER>Congratulations, you have successfully subscribed!</CENTER>";
$subscribemail = "Thank you for subscribing to our newsletter.";
$unsubscribemessage = "<CENTER>Thank you. You have been successfully unsubscribed from our Newsletter. The email address that was successfully removed is: </CENTER>";
$failedunsubscriptionmessage = "<CENTER>Sorry your request cannot be completed. Our database found no subscription information for that email address </CENTER>";

?>

Fou-Lu
09-22-2005, 09:17 AM
Please try to wrap your code in php or code tags to make it more legible. Fortunatly, yours is already nicely structured.
Try this:


function checkemail($email)
{
if (preg_match('/^[a-z0-9_-]+(\.[a-z0-9_-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.[a-z]{2,4}$/i', $email))
{
return true;
}
else
{
return false;
}
}

$news_email = $_POST['email'];
if(empty($news_email) OR (checkemail($news_email) === false))
{
echo 'You did not enter a valid e-mail address.<br> Please <a href=javascript:history.back(-1)>return to the form</a>.';
die; //stop the PHP from continuing
}

If it failes, it will be due to $news_email being empty, or not a valid address.


After scanning more, $_POST['email'] isn't set from any forms. The forms appear to be sending a value of $_POST['news_email']. Unless I'm missing something, your problem is that $news_email is never being sent.
BTW, change this:
<input type="hidden" name=admin value='.$pass.'>
Assuming that $pass is a password of some type, you do not want to send that through a hidden field. Send it with a session or another method that cannot be seen.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum