I am asking a user to enter a name and email address. When the form is submitted, a file is read and I compare this to the users name and email to see if they match part of the string.
$fname=$_POST['fname'];
//validate name field
function namecheck ($fname)
{
$regexp ="/^[A-Za-z]+$/";
if (! preg_match($regexp, $fname))
{
return false;
}
return true;
}
$message = "name is :" . $fname. "\n";
if (isset($_POST['submit'])) {
if (!namecheck ($fname))
{
echo'<script type="text/javascript">alert("Enter your name.")</script>';
return false;
}
else
{
if ( ! file_exists("users.txt"))
{
echo'<script type="text/javascript">alert("file does not exist")</script>';
}
else
{
$userStr = readfile("users.txt");
if (preg_match($userStr, $fname))
{
$fp = fopen ("users.txt", "a");
fwrite($fp, $message);
fclose($fp);
}
}
}
My question here, is: Will this check to see if the $fname
is a part of the users.txt
file?
Just change this line :
if (preg_match($userStr, $fname))
by this one:
if (preg_match("/$fname/", $userStr))
Your regex to check the name is very restrictive, names like O'Connors
or Jean-François
won't pass .