Register by Email with PHP
PHP can do just about anything. The script below will concentrate on registering people by email.
Sometimes you need to get people to register for something that you are putting on, giving away, offering, allowing, or whatever. You do not need CGI or CGI bins to do this. A simple HTML form and a simply PHP script will do the job just fine. We are assuming you don't need to store the person's registration information in a MySQL database. There are other scripts on this website that deal with such issues—just search with our search box. This script does not store anything. It just sends the site owner a simple email giving the info provided by the registrant.
Note that a simple captcha is used to prevent spam. The image login-question.png is used for this purpose. It contains a text question that is an image so the spammers have a nasty time trying to read it. But they must also answer the question correctly. Here it is:
A thinking spambot is probably a pretty rare creature!
There is nothing special about the form. It uses the captcha question and its action is to use the PHP file form.php.
The PHP file is used to validate the submitted form info. It gets the name, email, phone and zip POSTed to it and the captcha answer as well. If the submitted captcha answer is right and the phone or email info looks like it might be okay, the rest of the script is run. If the captcha is wrong or the submitted email or phone info look insincere (not long enough), most of the script is skipped and the user gets a message that "Wrong captcha answer or inadequate info. Try again."
The PHP substr() function makes sure no input has over 62 characters. The PHP strip_tags() function dumps tags found in input. Next the PHP preg_replace() function throws out all characters in the inputs that are unacceptable. For everything except email, that means dumping everything but letters, numbers, space, - and _. For email input, that means anything not allowed in emails. The 047 in the second regular expression pattern is the single quote—which is in this form because single quotes surround the regular expression.
The website owner's email address goes into the $e variable. This is in the "To" field of the email. If the input passes the smell test, the registrant sees the message "Thanks for registering. I will get back to you ASAP." If the input "stinks," the would-be-registrant sees "Wrong captcha answer or inadequate info. Try again."
<html>
<head>
</head>
<body>
<center>
<P><b>Limited Offer: </b>If you'd like to have a FREE Consultation, simply Register, using the form below.<BR><BR><BR>
<table width="570" border="1" align="center" cellpadding="0" cellspacing="1" bgcolor="#ffffff">
<tr>
<form id="form" name="form" method="post" action="form.php">
<td>
<table width="570" border="0" cellpadding="3" cellspacing="1" bgcolor="#ffffff">
<tr><td colspan='3' align='center'>FREE Consultation<BR><BR></td><tr>
<td align="right" width="150">Name: </td>
<td width="360"><input name="name" type="text" id="name" size="62" maxlength="62"></td>
<td width="50"> </td>
</tr>
<tr>
<td align="right">Email: </td>
<td><input name="email" type="text" id="email" size="62" maxlength="62"></td>
<td width="50"> </td>
</tr>
<tr>
<td align="right">Phone: </td>
<td><input name="phone" type="text" id="websiteURL" size="62" maxlength="62"></td>
<td width="50"> </td>
</tr>
<tr>
<td align="right">Zip: </td>
<td><input name="zip" type="text" id="websiteURL" size="62" maxlength="62"></td>
<td width="50"> </td>
</tr>
<tr>
<td align="right">Captcha: </td>
<td><IMG SRC="login-question.png" WIDTH=294 HEIGHT=36 BORDER=0></td>
<td width="50"> </td>
</tr>
<tr>
<td align="right">Answer: </td>
<td><input name="answer" type="text" id="answer" size="20" maxlength="20"></td>
<td width="50"> </td>
</tr>
<tr>
<td valign="top"> </td>
<td><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit" value="Reset"></td>
<td width="50"> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</p>
</center>
</body>
</html>
<html>
<head>
</head>
<body>
<?php
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$zip=$_POST['zip'];
$answer=$_POST['answer'];
$answer=strip_tags($answer);
if($answer=="of" && (strlen($email)>6 || strlen($phone)>6)){
$name=substr($name,0,62);
$email=substr($email,0,62);
$phone=substr($phone,0,62);
$zip=substr($zip,0,62);
$pattern1 = '/[^a-zA-Z0-9\\_\\s\\-]/i';
$pattern2 = '/[^a-zA-Z0-9\\.\\,\\!\\;\\-\\_\\*\\@\\=\\+\\$\\/\\&\\[\\]\\#\\?\\047\\:\\(\\)]/i';
$replacement = '';
$phone=strip_tags($phone);
$name=strip_tags($name);
$email=strip_tags($email);
$zip=strip_tags($zip);
$zip=preg_replace($pattern1, $replacement, $zip);
$phone=preg_replace($pattern1, $replacement, $phone);
$email=preg_replace($pattern2, $replacement, $email);
$name=preg_replace($pattern1, $replacement, $name);
$e="user-name@domain-name.com";
$to = $e;
$subject = "Registrants for FREE Consultation";
$message = "Name: ".$name."\n\nEmail: ".$email."\n\nPhone: ".$phone."\n\nZip: ".$zip."\n\n";
$headers = "From: ".$email."\r\nReply-To: ".$email;
$mail_sent = mail($to, $subject, $message, $headers);
echo '<script language="javascript">alert("Thanks for registering. I will get back to you ASAP."); window.location="Contact-Me.html";</script>';
}else{echo '<script language="javascript">alert("Wrong captcha answer or inadequate info. Try again."); window.location="Contact-Me.html";</script>';}
?>
</body>
</html>