<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Learn PHP Online &#187; PHP Scripts</title>
	<atom:link href="http://www.learnphponline.com/category/scripts/feed" rel="self" type="application/rss+xml" />
	<link>http://www.learnphponline.com</link>
	<description>Learn PHP Online</description>
	<lastBuildDate>Wed, 06 May 2009 01:02:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>A Simple Yet Elegant Contact Form In PHP</title>
		<link>http://www.learnphponline.com/scripts/a-simple-yet-elegant-contact-form-in-php</link>
		<comments>http://www.learnphponline.com/scripts/a-simple-yet-elegant-contact-form-in-php#comments</comments>
		<pubDate>Sun, 05 Apr 2009 00:29:27 +0000</pubDate>
		<dc:creator>Zachary Schuessler</dc:creator>
				<category><![CDATA[PHP Scripts]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php captcha]]></category>
		<category><![CDATA[php contact form]]></category>
		<category><![CDATA[php email]]></category>
		<category><![CDATA[php simple contact form]]></category>
		<category><![CDATA[php tutorial]]></category>

		<guid isPermaLink="false">http://www.learnphponline.com/?p=97</guid>
		<description><![CDATA[A guide that teaches developers how to create a simple yet secure contact form.]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><img class="size-full wp-image-96 aligncenter" title="php-simple-contact-form" src="http://www.learnphponline.com/wp-content/uploads/2009/04/php-simple-contact-form.jpg" alt="PHP Simple Contact Form" width="450" height="325" /></p>
<p style="text-align: center;"><span style="text-decoration: underline;">3 Files Used:<br />
</span></p>
<ol style="text-align: center;">
<li>contact.php</li>
<li>captcha.php</li>
<li>captcha.png</li>
</ol>
<p style="text-align: center;"><span style="text-decoration: underline;">Requirements:</span></p>
<ol style="text-align: center;">
<li>GD Library Installed (<em>For Captacha- it&#8217;s 99% likely it&#8217;s already installed</em>)<span style="text-decoration: underline;"><br />
</span></li>
</ol>
<hr />Simplicity is bliss. Unfortunately for webmasters, a simple and elegant contact form is hard to come by. Sure, it may be nice to include nifty AJAX validation techniques and a Captcha system that only a supercomputer could crack, but if you are anything like the LearnPHPOnline.com development team, you prefer a minimalistic approach to web development over a cluttered PHP script.</p>
<p>In this tutorial you will learn how to create a stylized form in HTML, code the backend in PHP to send an email to a specific address, and finally add a moderate Captcha system to thwart spammers from maliciously using your form.</p>
<h3>Getting Started – Building The HTML Form And Style</h3>
<p>We&#8217;ll start this off with a simple HTML template, that you are likely all too familiar with:</p>
<pre><span style="color: #0000ff;">&lt;!</span><span style="color: #ff00ff;">DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">html </span><span style="color: #ff0000;">xmlns</span><span style="color: #0000ff;">="http://www.w3.org/1999/xhtml"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">html</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">head</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">title</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">Contact Form - YourWebsite.com</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">title</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">head</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">body</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">body</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">html</span><span style="color: #0000ff;">&gt;</span></pre>
<p>We will be using a CSS layout to organize the information. To help keep things simple, we have used inline styling- but do feel free to make your own remote CSS file to further cut down on code count.</p>
<pre><span style="color: #000000;">
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;html&gt;
&lt;head&gt;

&lt;title&gt;Contact Form - YourWebsite.com&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;</span>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="width:650px;border-left:1px solid black;border-right:1px solid gray;margin:0px auto;overflow:hidden;"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
  </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">h1 </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="background-color:#1c5665;color:white;padding:5px;text-align:center;margin-top:0px;"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">Contact Form - YourWebsite.com</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">h1</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

  </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="float:left;width:214px;border-right:1px solid gray;padding:5px;background-color:#f6f8f9;height:100%;"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

    </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">p </span><span style="color: #ff0000;">align</span><span style="color: #0000ff;">="right"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="padding:5px;"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">Name:</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">p</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">p </span><span style="color: #ff0000;">align</span><span style="color: #0000ff;">="right"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="padding:5px;"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">Email Address:</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">p</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

    </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">p </span><span style="color: #ff0000;">align</span><span style="color: #0000ff;">="right"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="padding:5px;"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">Comment/Suggestion:</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">p</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
  </span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

  </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="float:left;width:415px;padding:5px;"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

    </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">p</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="name"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="border:1px solid #1c5665;padding:3px;margin-top:5px;"</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">p</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">p</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="email"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="border:1px solid #1c5665;padding:3px;margin-top:5px;"</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">p</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

    </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">p</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;">textarea </span><span style="color: #ff0000;">cols</span><span style="color: #0000ff;">="40"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="comment"</span><span style="color: #ff0000;"> rows</span><span style="color: #0000ff;">="4"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="border:1px solid #1c5665;padding:3px;margin-top:5px;"</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">textarea</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">p</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

  </span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

  </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="clear:both;"</span><span style="color: #0000ff;">&gt;</span><span style="color: #ff0000;">&amp;nbsp;</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
  </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">hr </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="color:gray"</span><span style="color: #ff0000;"> </span><span style="color: #0000ff;">/&gt;</span><span style="color: #000000;">

  </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="submit"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="Submit Form"</span><span style="color: #ff0000;"> </span><span style="color: #0000ff;">/&gt;</span><span style="color: #000000;">
 </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="hidden"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="form_submitted"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="1"</span><span style="color: #0000ff;">/&gt;</span><span style="color: #000000;">
      </span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">form</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="width:650px;height:10px;background-color:#1c5665;"</span><span style="color: #0000ff;">&gt;</span><span style="color: #ff0000;">&amp;nbsp</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #000000;">
&lt;/body&gt;
&lt;/html&gt;</span></pre>
<p>So far things should be fairly simple if you already have some knowledge about HTML and basic forms. One thing that seems to stump a lot of developers is the float property in CSS. To properly create a structured float, you will need four components: the wrapper, two or more sections of content that are using the float property, and finally a DIV that clears the float. You can see these, respectively, below:</p>
<pre><span style="color: #008000;">&lt;!--</span><span style="color: #008000;">WRAPPER </span><span style="color: #008000;">--&gt;</span><span style="color: #000000;">
</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="width:650px;border-left:1px solid black;border-right:1px solid gray;margin:0px auto;overflow:hidden;"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

</span><span style="color: #008000;">&lt;!--</span><span style="color: #008000;">FLOATED CONTENT 1 </span><span style="color: #008000;">--&gt;</span><span style="color: #000000;">
  </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="float:left;width:214px;border-right:1px solid gray;padding:5px;background-color:#f6f8f9;height:100%;"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

(content)
  </span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

</span><span style="color: #008000;">&lt;!--</span><span style="color: #008000;">FLOATED CONTENT 2 </span><span style="color: #008000;">--&gt;</span><span style="color: #000000;">
</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="float:left;width:415px;padding:5px;"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
(content 2)
</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

</span><span style="color: #008000;">&lt;!--</span><span style="color: #008000;">CLEAR FLOAT </span><span style="color: #008000;">--&gt;</span><span style="color: #000000;">
  </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="clear:both;"</span><span style="color: #0000ff;">&gt;</span><span style="color: #ff0000;">&amp;nbsp;</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span></pre>
<p>And that&#8217;s it! The HTML form is done for now. Now let&#8217;s go on with sending the form contents to a specified email address.</p>
<h3>Sending Mail In PHP</h3>
<p>We like to keep file count as small as possible, so we will be recycling the same file we just created to act as both the form and the file that processes the data to email the contents to your email address. We can do this with a simple IF structure in PHP.</p>
<p>You may have noticed this hidden value in the form we created:</p>
<ul>
<li>&lt;input type=&#8221;hidden&#8221; name=&#8221;form_submitted&#8221; value=&#8221;1&#8243;/&gt;</li>
</ul>
<p>This is going to allow us to see whether or not the user is submitting the form, or if they are filling it out. Since this is only true once the &#8220;Submit Form&#8221; button is clicked, we can use the following IF structure in conjunction:</p>
<pre>&lt;?php
<span style="color: #007700;">if (</span><span style="color: #0000bb;">$_POST</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'form_submitted'</span><span style="color: #007700;">] != </span><span style="color: #dd0000;">'1'</span><span style="color: #007700;">) {
</span><span style="color: #ff8000;"># Form not submitted, show form

</span><span style="color: #007700;">} else {
</span><span style="color: #ff8000;"># Process script, form was submitted
</span><span style="color: #007700;">}
</span><span style="color: #0000bb;">?&gt;</span></pre>
<p>To save you hassle, you can easily implement this selection structure by nesting in around the HTML:</p>
<pre>&lt;?php
    <span style="color: #007700;">if (</span><span style="color: #0000bb;">$_POST</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'form_submitted'</span><span style="color: #007700;">] != </span><span style="color: #dd0000;">'1'</span><span style="color: #007700;">) {
</span><span style="color: #ff8000;"># Form was not submitted, show form

</span><span style="color: #0000bb;">?&gt;
</span>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;html&gt;
&lt;head&gt;

&lt;title&gt;Contact Form - YourWebsite.com&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div style="width:650px;border-left:1px solid black;border-right:1px solid gray;margin:0px auto;overflow:hidden;"&gt;

<em>...[form contents]...</em>

&lt;/div&gt;

<span style="color: #0000bb;">&lt;?php </span><span style="color: #007700;">} else if (</span><span style="color: #0000bb;">$_POST</span><span style="color: #007700;">[</span><span style="color: #0000bb;">form_submitted</span><span style="color: #007700;">] == </span><span style="color: #0000bb;">1</span><span style="color: #007700;">) { </span><span style="color: #0000bb;">?&gt;

</span>
<span style="color: #0000bb;">&lt;?php
</span><span style="color: #ff8000;"># Code to send email goes here, since form was submitted
</span><span style="color: #0000bb;">?&gt;
</span>
<span style="color: #0000bb;">&lt;?php </span><span style="color: #007700;">}  </span><span style="color: #0000bb;">?&gt;

</span>
&lt;/div&gt;
&lt;/body&gt;

&lt;/html&gt;</pre>
<p>Now, onto the email function! PHP developers can make use of the mail() function already included in the PHP language. This function takes four main parameters to function correctly. As you&#8217;ll see below, we can simply plug in the information we just got from the submitted form&#8211; and the function takes care of the rest.</p>
<pre>// Send

<span style="color: #0000bb;">$to      </span><span style="color: #007700;">= </span><span style="color: #dd0000;">'yourname@yourdomain.com'</span><span style="color: #007700;">;
</span><span style="color: #0000bb;">$subject </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"Message from " </span><span style="color: #007700;">. </span><span style="color: #0000bb;">$_POST</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'name'</span><span style="color: #007700;">];

</span><span style="color: #0000bb;">$message </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$_POST</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'comment'</span><span style="color: #007700;">];
</span><span style="color: #0000bb;">$headers </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"From: " </span><span style="color: #007700;">. </span><span style="color: #0000bb;">$_POST</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'email'</span><span style="color: #007700;">] . </span><span style="color: #dd0000;">"\r\n" </span><span style="color: #007700;">.

    </span><span style="color: #dd0000;">"Reply-To: "</span><span style="color: #007700;">.</span><span style="color: #0000bb;">$_POST</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'name'</span><span style="color: #007700;">] . </span><span style="color: #dd0000;">"\r\n" </span><span style="color: #007700;">.
    </span><span style="color: #dd0000;">'X-Mailer: PHP/' </span><span style="color: #007700;">. </span><span style="color: #0000bb;">phpversion</span><span style="color: #007700;">();

</span><span style="color: #0000bb;">mail</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$to</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$subject</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$message</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$headers</span><span style="color: #007700;">);

echo </span><span style="color: #dd0000;">"&lt;html&gt;&lt;body style='background-color:#ececec;'&gt;&lt;div style='width:300px;border:1px dashed black;text-align:center;margin:0px auto;margin-top:200px;padding:20px;font-size:20px;background-color:white;'&gt;Your comment has been sent! Thanks!&lt;/div&gt;&lt;/html&gt;"</span><span style="color: #007700;">;

</span></pre>
<p>It looks like we would be done. But, wait- what about those <em>dastardly </em>spammers?</p>
<h3>Creating A Simple Captcha For PHP Forms</h3>
<p>Some Captcha scripts are quite confusing; making use of many different technologies to provide the best possible protection against spam robots. Even though Captchas have been cracked, they are hard to do so- and certainly will cut out almost all of your spam, if not all of it completely.</p>
<p style="text-align: center;"><span style="color: #000000;">In this example we are using a fairly secure Captcha system. First we are going to need a background image for our Captcha, which you  can download via &#8220;Save As..&#8221; here:</span><br />
<img class="size-full wp-image-98 aligncenter" title="captcha background" src="http://www.learnphponline.com/wp-content/uploads/2009/04/captcha.png" alt="Captcha Background Image" width="87" height="35" /></p>
<p>Now we will have to create a <strong>captcha.php</strong> file to build the image we will be using via the GD Library. Follow the comments in the script to help get an understanding of how it works:</p>
<pre>&lt;?php
session_start<span style="color: #007700;">();

</span><span style="color: #ff8000;">// Generate a Random String, Based On Time
</span><span style="color: #0000bb;">$md5 </span><span style="color: #007700;">= </span><span style="color: #0000bb;">md5</span><span style="color: #007700;">(</span><span style="color: #0000bb;">microtime</span><span style="color: #007700;">() * </span><span style="color: #0000bb;">mktime</span><span style="color: #007700;">());

</span><span style="color: #ff8000;"> //We don't need a 32 character long string, let's trim it
 <span style="color: #0000ff;">$string = substr($md5,0,5);</span></span><span style="color: #007700;">

</span><span style="color: #ff8000;">// Use GD Library to make a PNG from a file
</span><span style="color: #0000bb;">$captcha </span><span style="color: #007700;">= </span><span style="color: #0000bb;">imagecreatefrompng</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"captcha.png"</span><span style="color: #007700;">);

</span><span style="color: #ff8000;">// Set colors of lines with RGB colors
</span><span style="color: #0000bb;">$black </span><span style="color: #007700;">= </span><span style="color: #0000bb;">imagecolorallocate</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$captcha</span><span style="color: #007700;">, </span><span style="color: #0000bb;">0</span><span style="color: #007700;">, </span><span style="color: #0000bb;">0</span><span style="color: #007700;">, </span><span style="color: #0000bb;">0</span><span style="color: #007700;">);

</span><span style="color: #0000bb;">$line </span><span style="color: #007700;">= </span><span style="color: #0000bb;">imagecolorallocate</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$captcha</span><span style="color: #007700;">,</span><span style="color: #0000bb;">233</span><span style="color: #007700;">,</span><span style="color: #0000bb;">239</span><span style="color: #007700;">,</span><span style="color: #0000bb;">239</span><span style="color: #007700;">);

</span><span style="color: #ff8000;">// The following creates random lines to help throw off a spam robot's ability to guess the string

</span><span style="color: #0000bb;">imageline</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$captcha</span><span style="color: #007700;">,</span><span style="color: #0000bb;">0</span><span style="color: #007700;">,</span><span style="color: #0000bb;">10</span><span style="color: #007700;">,</span><span style="color: #0000bb;">50</span><span style="color: #007700;">,</span><span style="color: #0000bb;">16</span><span style="color: #007700;">,</span><span style="color: #0000bb;">$black</span><span style="color: #007700;">);
</span><span style="color: #0000bb;">imageline</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$captcha</span><span style="color: #007700;">,</span><span style="color: #0000bb;">40</span><span style="color: #007700;">,</span><span style="color: #0000bb;">11</span><span style="color: #007700;">,</span><span style="color: #0000bb;">64</span><span style="color: #007700;">,</span><span style="color: #0000bb;">29</span><span style="color: #007700;">,</span><span style="color: #0000bb;">$black</span><span style="color: #007700;">);

</span><span style="color: #0000bb;">imageline</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$captcha</span><span style="color: #007700;">,</span><span style="color: #0000bb;">0</span><span style="color: #007700;">,</span><span style="color: #0000bb;">60</span><span style="color: #007700;">,</span><span style="color: #0000bb;">90</span><span style="color: #007700;">,</span><span style="color: #0000bb;">0</span><span style="color: #007700;">,</span><span style="color: #0000bb;">$black</span><span style="color: #007700;">);

</span><span style="color: #ff8000;">//Write the string to the image

</span><span style="color: #0000bb;">imagestring</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$captcha</span><span style="color: #007700;">, </span><span style="color: #0000bb;">5</span><span style="color: #007700;">, </span><span style="color: #0000bb;">20</span><span style="color: #007700;">, </span><span style="color: #0000bb;">10</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$string</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$black</span><span style="color: #007700;">);

</span><span style="color: #ff8000;">// Use MD5 encryption on the key, and store it for a comparison test later

</span><span style="color: #0000bb;">$_SESSION</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'key'</span><span style="color: #007700;">] = </span><span style="color: #0000bb;">md5</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$string</span><span style="color: #007700;">);

</span><span style="color: #ff8000;">// Print out the image
</span><span style="color: #0000bb;">header</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"Content-type: image/png"</span><span style="color: #007700;">);

</span><span style="color: #0000bb;">imagepng</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$captcha</span><span style="color: #007700;">);
</span><span style="color: #0000bb;">?&gt;</span></pre>
<p>This script is really neat, considering the .PHP file is treated as a .PNG image if successfully executed. This way, we can simply call to the image from the form and have a dynamic image to test our users with!</p>
<p>Now let&#8217;s go back to our form and make some necessary changes, as seen in red words below. This is the final version of the script- enjoy!</p>
<pre>&lt;?php
    <span style="color: #800000;"><strong>session_start();</strong></span>
    if (<span style="color: #000000;">$_POST['form_submitted'] != '1'</span><span style="color: #007700;"><span style="color: #000000;">) {</span>
</span><span style="color: #0000bb;">?&gt;

</span>
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;html&gt;
&lt;head&gt;

&lt;title&gt;Contact Form - YourWebsite.com&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div style="width:650px;border-left:1px solid black;border-right:1px solid gray;margin:0px auto;overflow:hidden;"&gt;

  &lt;h1 style="background-color:#1c5665;color:white;padding:5px;text-align:center;margin-top:0px;"&gt;Contact Form - YourWebsite.com&lt;/h1&gt;

  &lt;div style="float:left;width:214px;border-right:1px solid gray;padding:5px;background-color:#f6f8f9;height:100%;"&gt;
    &lt;form method="post"&gt;

    &lt;p align="right" style="padding:5px;"&gt;Name:&lt;/p&gt;
    &lt;p align="right" style="padding:5px;"&gt;Email Address:&lt;/p&gt;
    &lt;p align="right" style="padding:5px;"&gt;Comment/Suggestion:&lt;/p&gt;

  &lt;/div&gt;

  &lt;div style="float:left;width:415px;padding:5px;"&gt;
    &lt;p&gt;&lt;input type="text" name="name" style="border:1px solid #1c5665;padding:3px;margin-top:5px;"&gt;&lt;/p&gt;
    &lt;p&gt;&lt;input type="text" name="email" style="border:1px solid #1c5665;padding:3px;margin-top:5px;"&gt;&lt;/p&gt;

    &lt;p&gt;&lt;textarea cols="40" name="comment" rows="4" style="border:1px solid #1c5665;padding:3px;margin-top:5px;"&gt;&lt;/textarea&gt;&lt;/p&gt;
  &lt;/div&gt;

  &lt;div style="clear:both;"&gt;&amp;nbsp;&lt;/div&gt;

  &lt;hr style="color:gray" /&gt;

 <span style="color: #800000;"> <strong>&lt;div style="width:325px; border:1px solid black;margin:0px auto;text-align:center;"&gt;
    &lt;p&gt;&lt;img src="captcha.php" /&gt;&lt;/p&gt;

      &lt;div style="margin-top:-15px;"&gt;
        Please enter the image text:
      &lt;/div&gt;
      &lt;div style="margin-top:-3px;margin-bottom: 4px;"&gt;
        &lt;input type="text" name="code" style="border:1px solid #1c5665;padding:3px;margin-top:5px;"&gt;

      &lt;/div&gt;
      &lt;input type="submit" value="Submit Form" /&gt;
      &lt;input type="hidden" name="form_submitted" value="1"/&gt;
      &lt;/form&gt;
  &lt;/div&gt;</strong>
</span>
  &lt;div style="width:650px;height:10px;background-color:#1c5665;"&gt;&amp;nbsp&lt;/div&gt;

<span style="color: #000000;">&lt;?php } else if ($_POST[form_submitted] == 1) { ?&gt;

&lt;?php
<span style="color: #800000;"><strong>//Encrypt the posted code field and then compare with the stored key

if(md5($_POST['code']) != $_SESSION['key'])

{
  echo "It seems you entered an invalid Captcha key. Please go back and try again.";

}else{
session_unset();
session_destroy();</strong></span>
// Send

$to      = 'yourname@yourwebsite.com';
$subject = "Message from " . $_POST['name'];

$message = $_POST['comment'];
$headers = "From: " . $_POST['email'] . "\r\n" .

    "Reply-To: ".$_POST['name'] . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

echo "&lt;html&gt;&lt;body style='background-color:#ececec;'&gt;&lt;div style='width:300px;border:1px dashed black;text-align:center;margin:0px auto;margin-top:200px;padding:20px;font-size:20px;background-color:white;'&gt;Your comment has been sent! Thanks!&lt;/div&gt;";

}
?&gt;
&lt;?php }  </span>

<span style="color: #0000bb;"><span style="color: #000000;">?&gt;</span>
</span>
&lt;/div&gt;
&lt;/body&gt;

&lt;/html&gt;</pre>
<p>Note in particular that we are using the <strong>Session Unset</strong> and <strong>Session Destroy</strong> functions. Without them, you could refresh the page continually after you passed the Captcha test and send as much spam as you would like- &#8211; despite our efforts!</p>
<h4>Closing Comments</h4>
<p style="text-align: center;">Our form is still missing several things. You will likely need to add form validation, such as checking to see if all fields were filled out. You may also tweak the Captcha system to your liking- which is a great way to get experience with the GD Library. You may also want to consider that not everyone can see or hear- and that the Captcha in its current state will prevent such users from sending you mail.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learnphponline.com/scripts/a-simple-yet-elegant-contact-form-in-php/feed</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Email Activation For Registration Forms</title>
		<link>http://www.learnphponline.com/scripts/email-activation-for-php-forms</link>
		<comments>http://www.learnphponline.com/scripts/email-activation-for-php-forms#comments</comments>
		<pubDate>Fri, 03 Apr 2009 23:31:16 +0000</pubDate>
		<dc:creator>Zachary Schuessler</dc:creator>
				<category><![CDATA[PHP Scripts]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php email]]></category>
		<category><![CDATA[php email activation]]></category>
		<category><![CDATA[php scripts]]></category>
		<category><![CDATA[php tutorials]]></category>

		<guid isPermaLink="false">http://www.learnphponline.com/?p=53</guid>
		<description><![CDATA[How to make use of email activation functionality in PHP registration forms.]]></description>
			<content:encoded><![CDATA[<h3>Email Activation For Registration Forms</h3>
<p style="border-top: 1px solid blue; border-bottom: 1px solid blue; margin: 0px auto; padding: 10px; width: 95%; background-color: #e0eaef; text-indent: 0px;"><strong>Foreword</strong>: There are two good reasons why email activation is a necessary for webmasters. First, it helps root out  spam by requiring user interaction. It also creates a sense of trust, since we can build a certain amount of trust with a user who can confirm they are who they say they are. In this example you will need access to a database (MySQL is what we&#8217;ll use) with proper permissions.</p>
<p>We&#8217;ll start out creating the interface of the form. You will need to create two files: one file to hold the form, the next file to handle the verification process and interface with the database. It doesn&#8217;t necessarily matter what you name your files, but to stay uniform with our examples name the registration and verification files register.php and verify.php, respectively.</p>
<p>Below you will see register.php in action- in all its simplistic glory.</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px;; text-indent: 15px;">Our Registration Form &#8211; register.php</p>
<pre style="border: 1px solid black; padding: 10px;">&lt;html&gt;
 &lt;body&gt;
  &lt;form action="verify.php" method="post" name="register"&gt;
    Username: &lt;input type="text" name="username" /&gt;
    Password: &lt;input type="text" name="password" /&gt;
    Email: &lt;input type="text" name="email" /&gt;
  &lt;input type="submit" /&gt;
  &lt;/form&gt;
 &lt;/body&gt;

&lt;/html&gt;</pre>
<p>At this point the only things worth mentioning is that we are putting &#8220;verify.php&#8221; as the form action, and naming the form &#8220;register&#8221; with the name command. Go ahead and save this file and upload it to your hosting account- we&#8217;re done with this file for now.</p>
<h3>Groundwork For The Verification Process</h3>
<p>Now let&#8217;s create a file named verify.php. We are using this file for two things. First, we use it to insert data into the database if everything seems to be hunky-dory. But we also use it to confirm the verification code we email the user, so we&#8217;ll need to make use of the &#8220;IF&#8221; selection structure to differentiate between the two processes.</p>
<p>So how do we know if the verify.php file should submit data to our database or verify the activation code a user provides? We&#8217;ll admit that when we said we were done with register.php, we lied. To properly determine if the user is submitting data or verifying a code, we need to add a hidden value on the registration form, as seen below:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px;; text-indent: 15px;">Hidden Values For Our Registration Form &#8211; register.php</p>
<pre style="border: 1px solid black; padding: 10px;color:gray;">&lt;html&gt;
 &lt;body&gt;
  &lt;form action="verify.php" method="post" name="register"&gt;
    Username: &lt;input type="text" name="username" /&gt;
    Password: &lt;input type="text" name="password" /&gt;
    Email: &lt;input type="text" name="email" /&gt;
	<strong style="color:black">&lt;input type="hidden" name="form_submitted" value="1"/&gt; </strong>
  &lt;input type="submit" /&gt;
  &lt;/form&gt;
 &lt;/body&gt;
&lt;/html&gt;</pre>
<p>Now we can check to see if this value is set on our verify.php file with the following code:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px;; text-indent: 15px;">Selection Structure &#8211; verify.php</p>
<pre style="border: 1px solid black; padding: 10px;color:gray;"><span style="color: #007700;">if (</span><span style="color: #0000bb;">$_POST</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'form_submitted'</span><span style="color: #007700;">] == </span><span style="color: #dd0000;">'1'</span><span style="color: #007700;">) {
</span><span style="color: #007700;">
</span><span style="color: #ff8000;">## Form was submitted,the user is registering!

 </span><span style="color: #007700;">} else{

</span><span style="color: #ff8000;">## No value found, user must be activating their account!

</span><span style="color: #007700;">}</span></pre>
<p>With our form and selection structure in place, we need to go to our &#8220;backend&#8221; and create a database.</p>
<h3>Database Design For Email Activation</h3>
<p>In our example we are creating a table named &#8220;users&#8221; with the fields &#8220;id, status, username, password, email, and activationkey&#8221; &#8211; we encourage you to use the same values for the sake of simplicity. In fact, you can just run the SQL query below and do just that:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px;; text-indent: 15px;">SQL Query Code &#8211; Run Code to Create Table And Fields</p>
<pre style="border: 1px solid black; padding: 10px;">CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL auto_increment,
  `status` varchar(20) NOT NULL,
  `username` varchar(20) NOT NULL,
  `password` varchar(20) NOT NULL,
  `email` varchar(20) NOT NULL,
  `activationkey` varchar(100) NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `username` (`username`),
  UNIQUE KEY `email` (`email`),
  UNIQUE KEY `activationkey` (`activationkey`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;</pre>
<p>If all has gone well, your database should look something like the following (note if you aren&#8217;t using PHPMyAdmin and MySQL, you may see some differences):</p>
<p><img style="margin: 0px auto; width: 500px; display: block;" title="PHP MySQL Screenshot" src="../mysqltable.jpg" alt="" /></p>
<h3>Inserting Registration Data Into Database With PHP</h3>
<p>Now that we have a good grasp on where we are going, we can go ahead and connect to our database. First we&#8217;ll need to arrange the correct connection statement. We will be using the <span style="color: #0000bb;">mysql_connect</span> and <span style="color: #0000bb;">mysql_select_db functions</span> to make the connection to our database.</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px;; text-indent: 15px;">Connecting To The Database &#8211; verify.php</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #0000bb;">mysql_connect</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"localhost"</span><span style="color: #007700;">, </span><span style="color: #0000bb;">DATABASE</span><span style="color: #007700;">, </span><span style="color: #0000bb;">PASSWORD </span><span style="color: #007700;">or ;die(</span><span style="color: #0000bb;">mysql_error</span><span style="color: #007700;">());

</span><span style="color: #0000bb;">mysql_select_db</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"USER_TABLENAME "</span><span style="color: #007700;">) or die(</span><span style="color: #0000bb;">mysql_error</span><span style="color: #007700;">());

</span></pre>
<p>Above we can see that the only thing we need to change is the database, password, and table name. Ideally the table name should be &#8220;users&#8221; as per our example. Your password and database name can be created via MySQL if you have the proper permissions. If you don&#8217;t, contact your web host to get a database configured.</p>
<p>So far your verify.php should look like this:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px;; text-indent: 15px;">Project Thus Far &#8211; verify.php</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #0000bb;">&lt;?php

mysql_connect</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"localhost"</span><span style="color: #007700;">, </span><span style="color: #0000bb;">"DATABASE"</span><span style="color: #007700;">, </span><span style="color: #dd0000;">"PASSWORD"</span><span style="color: #007700;">) or die(</span><span style="color: #0000bb;">mysql_error</span><span style="color: #007700;">());

</span><span style="color: #0000bb;">mysql_select_db</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"USER_TABLENAME"</span><span style="color: #007700;">) or die(</span><span style="color: #0000bb;">mysql_error</span><span style="color: #007700;">());</span>

<span style="color: #888888;">if ($_POST['form_submitted'] == '1') {</span><span style="color:gray;">
} else {

}
?&gt;</span></pre>
<p>Test the connection by uploading the file to your server and navigating to the file. If an error doesn&#8217;t present itself, it means you successfully connected to your database! (Even if you see a blank page) Now we can create a random key and answer all of the data into our database.</p>
<h3>Creating A Random Key And Inserting Database Values</h3>
<p>We will be using the mt_rand() function to create our random key. Below you&#8217;ll see that we concatenate the function five times in order to get a lengthy string.</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px;; text-indent: 15px;">Random Number Generator &#8211; verify.php</p>
<pre style="border: 1px solid black; padding: 10px;">$activationKey =  mt_rand() . mt_rand() . mt_rand() . mt_rand() . mt_rand();</pre>
<p>Don&#8217;t get too excited to try it out yet, first let&#8217;s write the code to insert the value into our database.</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px;; text-indent: 15px;">Inserting Data Into A Database &#8211; verify.php</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #0000bb;">$sql</span><span style="color: #007700;">=</span><span style="color: #dd0000;">"INSERT INTO users (username, password, email, activationkey, status) VALUES ('$_POST[username]', '$_POST[password]', '$_POST[email]', '$activationKey', 'verify')"</span><span style="color: #007700;">;

if (!</span><span style="color: #0000bb;">mysql_query</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$sql</span><span style="color: #007700;">))

  {

  die(</span><span style="color: #dd0000;">'Error: ' </span><span style="color: #007700;">. </span><span style="color: #0000bb;">mysql_error</span><span style="color: #007700;">());

  }</span></pre>
<p>Above you can see we are updating all of the rows with information from our registration field via the $_POST command. We are also including the $activationKey variable and inputting the word &#8216;verify&#8217; into the status field. This is to keep track of who is verified and who isn&#8217;t. If someone isn&#8217;t verified yet, but has registered, we could easily have them request to resend the email instead of having to register again. Oh, technology!</p>
<p>So far the code should be as below:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px;; text-indent: 15px;">Script Thus Far &#8211; verify.php</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #0000bb;">&lt;?php

mysql_connect</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"localhost"</span><span style="color: #007700;">, </span><span style="color: #0000bb;">DATABASE</span><span style="color: #007700;">, </span><span style="color: #dd0000;">"PASSWORD"</span><span style="color: #007700;">) or die(</span><span style="color: #0000bb;">mysql_error</span><span style="color: #007700;">());

</span><span style="color: #0000bb;">mysql_select_db</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"USER_TABLENAME "</span><span style="color: #007700;">) or die(</span><span style="color: #0000bb;">mysql_error</span><span style="color: #007700;">());

</span><span style="color: #007700;">if (</span><span style="color: #0000bb;">$_POST</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'form_submitted'</span><span style="color: #007700;">] == </span><span style="color: #dd0000;">'1'</span><span style="color: #007700;">) {</span><span style="color: #007700;">
</span><span style="color: #ff8000;">##User is registering, insert data until ;we can activate it

</span><span style="color: #0000bb;">$activationKey </span><span style="color: #007700;">=  </span><span style="color: #0000bb;">mt_rand</span><span style="color: #007700;">() . </span><span style="color: #0000bb;">mt_rand</span><span style="color: #007700;">() . </span><span style="color: #0000bb;">mt_rand</span><span style="color: #007700;">() . </span><span style="color: #0000bb;">mt_rand</span><span style="color: #007700;">() . </span><span style="color: #0000bb;">mt_rand</span><span style="color: #007700;">();

</span><span style="color: #0000bb;">$sql</span><span style="color: #007700;">=</span><span style="color: #dd0000;">"INSERT INTO users (username, password, email, activationkey, status)

VALUES

('$_POST[username]', '$_POST[password]', '$_POST[email]','$activationKey', 'verify')"</span><span style="color: #007700;">;

if (!</span><span style="color: #0000bb;">mysql_query</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$sql</span><span style="color: #007700;">))

  {

  die(</span><span style="color: #dd0000;">'Error: ' </span><span style="color: #007700;">. </span><span style="color: #0000bb;">mysql_error</span><span style="color: #007700;">());

  }

} else {

}

</span><span style="color: #0000bb;">?&gt;</span></pre>
<h3>Sending The Activation Key</h3>
<p>Sending an email with PHP is painlessly easy- we just have to supply a few values to an already-made function in PHP: the aptly named mail() command. In our example we are using four parameters to send the email: the recipient address, the subject of the email, the message, and our own return address.</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px;; text-indent: 15px;">Sending Mail With PHP &#8211; verify.php</p>
<pre style="border: 1px solid black; padding: 10px;">	<span style="color: #007700;">echo </span><span style="color: #dd0000;">"An email has been sent to $_POST[email] with an activation key. Please check your mail to complete registration."</span><span style="color: #007700;">;

</span><span style="color: #ff8000;">##Send activation Email

</span><span style="color: #0000bb;">$to      </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$_POST</span><span style="color: #007700;">[</span><span style="color: #0000bb;">email</span><span style="color: #007700;">];

</span><span style="color: #0000bb;">$subject </span><span style="color: #007700;">= </span><span style="color: #dd0000;">" YOURWEBSITE.com Registration"</span><span style="color: #007700;">;

</span><span style="color: #0000bb;">$message </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"Welcome to our website!\r\rYou, or someone using your email address, has completed registration at YOURWEBSITE.com. You can complete registration by clicking the following link:\rhttp://www.YOURWEBSITE.com/verify.php?$activationKey\r\rIf this is an error, ignore this email and you will be removed from our mailing list.\r\rRegards,\ YOURWEBSITE.com Team"</span><span style="color: #007700;">;

</span><span style="color: #0000bb;">$headers </span><span style="color: #007700;">= </span><span style="color: #dd0000;">'From: noreply@ YOURWEBSITE.com' </span><span style="color: #007700;">. </span><span style="color: #dd0000;">"\r\n" </span><span style="color: #007700;">.

    </span><span style="color: #dd0000;">'Reply-To: noreply@ YOURWEBSITE.com' </span><span style="color: #007700;">. </span><span style="color: #dd0000;">"\r\n" </span><span style="color: #007700;">.

    </span><span style="color: #dd0000;">'X-Mailer: PHP/' </span><span style="color: #007700;">. </span><span style="color: #0000bb;">phpversion</span><span style="color: #007700;">();

</span><span style="color: #0000bb;">mail</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$to</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$subject</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$message</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$headers</span><span style="color: #007700;">);

</span></pre>
<p>This should be fairly self-explanatory. Notice that we are using the \r command to force a return- this is to format the email so all the text isn&#8217;t on one line. If you wanted, you could even include HTML and images into the email. We would recommend you didn&#8217;t, however, as many mail platforms today either don&#8217;t support such features or mark most emails that contain them as spam.</p>
<h3>Coding The Verification Checking Process</h3>
<p>We have arrived at the final part of this lesson: checking the verification code and allowing the user to either be registered or tell them they have entered the wrong code and to try again. In this section we will actually grab the current URL, take the query string, and then check our database to see if it matches a record. If it does, we will call the registrant a member and remove the key from our database. Otherwise, tough luck!</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px;; text-indent: 15px;">Script Thus Far &#8211; verify.php</p>
<pre style="border: 1px solid black; padding: 10px;">##User isn't registering, check verify code and change activation code to null, status to activated on success

<span style="color: #0000bb;">$queryString </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$_SERVER</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'QUERY_STRING'</span><span style="color: #007700;">];

</span><span style="color: #0000bb;">$query </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"SELECT * FROM users"</span><span style="color: #007700;">;

</span><span style="color: #0000bb;">$result </span><span style="color: #007700;">= </span><span style="color: #0000bb;">mysql_query</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$query</span><span style="color: #007700;">) or die(</span><span style="color: #0000bb;">mysql_error</span><span style="color: #007700;">());

  while(</span><span style="color: #0000bb;">$row </span><span style="color: #007700;">= </span><span style="color: #0000bb;">mysql_fetch_array</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$result</span><span style="color: #007700;">)){

    if (</span><span style="color: #0000bb;">$queryString </span><span style="color: #007700;">== </span><span style="color: #0000bb;">$row</span><span style="color: #007700;">[</span><span style="color: #dd0000;">"activationkey"</span><span style="color: #007700;">]){

       echo </span><span style="color: #dd0000;">"Congratulations!" </span><span style="color: #007700;">. </span><span style="color: #0000bb;">$row</span><span style="color: #007700;">[</span><span style="color: #dd0000;">"username"</span><span style="color: #007700;">] . </span><span style="color: #dd0000;">" is now the proud new owner of a YOURWEBSITE.com account."</span><span style="color: #007700;">;

       </span><span style="color: #0000bb;">$sql</span><span style="color: #007700;">=</span><span style="color: #dd0000;">"UPDATE users SET activationkey = '', status='activated' WHERE (id = $row[id])"</span><span style="color: #007700;">;

       if (!</span><span style="color: #0000bb;">mysql_query</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$sql</span><span style="color: #007700;">))

  {

        die(</span><span style="color: #dd0000;">'Error: ' </span><span style="color: #007700;">. </span><span style="color: #0000bb;">mysql_error</span><span style="color: #007700;">());

  }

    }

  }

</span></pre>
<p>Above we are doing just as we stated. Pay special attention to the fact we are using the UPDATE command in SQL- not INSERT. Also note that we need the while loop to find the exact ID of the member to update- we don&#8217;t want to update everyone in our database! We do this by comparing the current record ID with one from the database- and voila! If a match is found, we can update it.</p>
<p>Finally, we need to add some security to our script. Read our <a title="sql injection tutorial" href="http://www.learnphponline.com/security/sql-injection-prevention-mysql-php">SQL Injection Tutorial</a> and add the updates below:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px;; text-indent: 15px;">Final Result &#8211; verify.php</p>
<pre style="border: 1px solid black; padding: 10px;color:gray;">mysql_connect("localhost", DATABASE, "PASSWORD") or die(mysql_error());

mysql_select_db("USER_TABLENAME") or die(mysql_error());

<span style="color: #888888;">if ($_POST['form_submitted'] == '1') {</span>
##User is registering, insert data until we can activate it

$activationKey =  mt_rand() . mt_rand() . mt_rand() . mt_rand() . mt_rand();
<span style="color:blue;">$username</span> = <span style="color:blue;">mysql_real_escape_string($_POST[username])</span>;
<span style="color:blue;">$password</span> = <span style="color:blue;">mysql_real_escape_string($_POST[password])</span>;

<span style="color:blue;">$email</span> = <span style="color:blue;">mysql_real_escape_string($_POST[email])</span>;

$sql="INSERT INTO users (username, password, email, activationkey, status) VALUES ('<span style="color:blue;">$username</span>', '<span style="color:blue;">$password</span>', '<span style="color:blue;">$email</span>', '$activationKey', 'verify')";

if (!mysql_query($sql))

  {

  die('Error: ' . mysql_error());

  }

echo "An email has been sent to $_POST[email] with an activation key. Please check your mail to complete registration.";

##Send activation Email

$to      = $_POST[email];

$subject = " YOURWEBSITE.com Registration";

$message = "Welcome to our website!\r\rYou, or someone using your email address, has completed registration at YOURWEBSITE.com. You can complete registration by clicking the following link:\rhttp://www.YOURWEBSITE.com/verify.php?$activationKey\r\rIf this is an error, ignore this email and you will be removed from our mailing list.\r\rRegards,\ YOURWEBSITE.com Team";

$headers = 'From: noreply@ YOURWEBSITE.com' . "\r\n" .

    'Reply-To: noreply@ YOURWEBSITE.com' . "\r\n" .

    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

} else {

##User isn't registering, check verify code and change activation code to null, status to activated on success

$queryString = $_SERVER['QUERY_STRING'];

$query = "SELECT * FROM users"; 

$result = mysql_query($query) or die(mysql_error());

  while($row = mysql_fetch_array($result)){

    if ($queryString == $row["activationkey"]){

       echo "Congratulations!" . $row["username"] . " is now the proud new owner of an YOURWEBSITE.com account.";

       $sql="UPDATE users SET activationkey = '', status='activated' WHERE (id = $row[id])";

       if (!mysql_query($sql))

  {

        die('Error: ' . mysql_error());

  }

    }

  }

}</pre>
<h4>Verification Key Conclusion</h4>
<p>So where should you take it from here? Obviously we haven&#8217;t included any error checking for input data. What if the user misspelled his or her password? We should probably put another password field in, and check to see if the passwords match. Additionally, we should mask the content in the password field to ensure security.</p>
<p>We might also add a CAPTCHA to prevent the mail server from getting abused by spam bots- something your host would probably appreciate. We could also simplify matters by using functions and cleaning up code.</p>
<p>There are many ways to improve- but be sure to check out our scripts and tutorials section for more information, because we&#8217;ve covered such topics like this in the past.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learnphponline.com/scripts/email-activation-for-php-forms/feed</wfw:commentRss>
		<slash:comments>119</slash:comments>
		</item>
	</channel>
</rss>

