<?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>Cyclonic Studios &#187; PHP</title>
	<atom:link href="http://www.cyclonicstudios.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cyclonicstudios.com</link>
	<description>Windswept Design and Development by Graham Kaemmer</description>
	<lastBuildDate>Fri, 06 Jan 2012 22:17:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Validating a Contact Form with PHP and the GD Library</title>
		<link>http://www.cyclonicstudios.com/validating-a-contact-form-with-php-and-the-gd-library/</link>
		<comments>http://www.cyclonicstudios.com/validating-a-contact-form-with-php-and-the-gd-library/#comments</comments>
		<pubDate>Tue, 07 Jul 2009 03:03:12 +0000</pubDate>
		<dc:creator>Cyclonic</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[web design]]></category>

		<guid isPermaLink="false">http://www.cyclonicstudios.com/?p=119</guid>
		<description><![CDATA[Sorry about my last post, I was misinformed about the security of the $_SESSION superglobal. This time we&#8217;re going to make a user validation box to make sure that it is a human sending the contact request (not a spambot). This is the most reliable way to prevent spam. To follow this tutorial, you&#8217;ll need [...]]]></description>
			<content:encoded><![CDATA[<p>Sorry about my <a href="http://www.cyclonicstudios.com/php/making-a-secure-contact-form-with-php-and-jquery/">last post</a>, I was misinformed about the security of the $_SESSION superglobal. This time we&#8217;re going to make a user validation box to make sure that it is a human sending the contact request (not a spambot). This is the most reliable way to prevent spam.<br />
<br />
To follow this tutorial, you&#8217;ll need a free font (make sure its FREE, not free for personal use) to use in our validation image. There probably aren&#8217;t going to be many spambots with image-to-text capabilities, but try to find a font that isn&#8217;t too easily interpreted by a computer. I used <a href="http://www.dafont.com/acidic.font">Acidic</a>, which is fairly readable for a human but very difficult for a computer to interpret.<br />
<br />
We&#8217;re going to start out by making our form, just with simple HTML. In the form we&#8217;re going to have a text box for the contacter&#8217;s email, a textarea for the message, a validation image and text box, and submit button.<br />
</p>
<pre><code class="html">
<form id="contact" name="contact" method="post" action="contact.php">
<h2>Contact Me</h2>

	Email:
<input type="text" name="contact_email" id="contact_email" />
	<textarea name="contact_text" id="contact_text">

	</textarea>
	<img src="valid_image.php" width="100" height="30" alt="Type this text in the box below."/>
<input type="text" name="validate" />
<input type="submit" id="contact_submit" name="contact_submit" value="Send" />
</form>

</code></pre>
<p>
As you can see, the image is actually a PHP file, valid_image.php. In this file, we are going to come up with a random validation string, store it in a session variable, and draw an image with the text in the font we chose. For this to work, you need to give the <code>imagettftext()</code> a relative path to the font from where valid_image.php is saved. If it is the same directory, than just &#8220;yourfont.ttf&#8221; will do.<br />
</p>
<pre><code class="php">
<?php
session_start();
header("Content-type: image/png");
// make the random 5-character string
$rand = "";
for ($i=0;$i<5;$i++) {
	$rand .= chr(rand(97,122));
}
$_SESSION['validate'] = $rand;
$width = 100;
$height = 30;
$image = @imagecreate($width,$height);
$bgcolor = imagecolorallocate($image,255,255,255); // put your background color here
$textcolor = imagecolorallocate($image,0,0,0); // put your text color here
imagefilledrectangle($image,0,0,$width,$height,$bgcolor);
imagettftext($image,25,0,0,$height,$textcolor,"acidic.ttf",$rand);
imagepng($image);
imagedestroy($image);
?>
</code></pre>
<p>
So now we have an image with the string, and a session variable holding the string. Now, when the request is sent to contact.php, we will compare the session variable to the value given in the form. If they are the same, then the contacter is a human that could read the image.<br />
<br />
Your contact.php should be saved in the same directory as your form (or you could edit the action attribute of the form), and it should look like this:<br />
</p>
<pre><code class="php">
<?php
session_start();
if (!empty($_POST['validate'])) {
if ($_POST['validate'] == $_SESSION['validate']) {
	unset($_SESSION['validate']);
	if (!empty($_POST['contact_text']) &#038;&#038; !empty($_POST['contact_email'])) {
		$to      = 'webmaster@example.com';
		$subject = 'Contact Request from '.$_POST['contact_email'];
		$message = $_POST['contact_text'];
		$headers = 'From: '.$_POST['contact_email']." \r\n" .
			'Reply-To: '.$_POST['contact_email']."\r\n" .
			'X-Mailer: PHP/' . phpversion();
		mail($to, $subject, $message, $headers);
		echo "Thanks for contacting me!";
	} else {
		echo "Please enter both your email and a message.";
	}
} else {
	echo "Your validation code was incorrect."
}
}
?>
</code></pre>
<p>
<a href="http://stevelove.org">Steve Love</a> gave me a good tip to unset the session variable after use so that it could not be reused. Thanks!</p>
<p>This tutorial was mainly about the validation technique, but if you want to make this form asynchronous, you can follow the last few steps of my <a href="http://www.cyclonicstudios.com/php/making-a-secure-contact-form-with-php-and-jquery/">last tutorial</a>, to implement the jQuery and AJAX.<br />
<br />
Thanks for reading!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cyclonicstudios.com/validating-a-contact-form-with-php-and-the-gd-library/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making a Secure Contact Form with PHP and jQuery</title>
		<link>http://www.cyclonicstudios.com/making-a-secure-contact-form-with-php-and-jquery/</link>
		<comments>http://www.cyclonicstudios.com/making-a-secure-contact-form-with-php-and-jquery/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 17:11:51 +0000</pubDate>
		<dc:creator>Cyclonic</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[web design]]></category>

		<guid isPermaLink="false">http://cyclonicstudios.com/?p=104</guid>
		<description><![CDATA[Its always nice when a site gives you an opportunity to contact the webmaster. You should always make sure, when you are building a website, to give your users opportunity to do this. One way to go about doing this is just giving out your email address. Unfortunately, putting your email address up on your [...]]]></description>
			<content:encoded><![CDATA[<p>Its always nice when a site gives you an opportunity to contact the webmaster. You should always make sure, when you are building a website, to give your users opportunity to do this. One way to go about doing this is just giving out your email address. Unfortunately, putting your email address up on your site makes it easy form spam bots to find it and start sending you all sorts of emails. The best way is to make a contact form.</p>
<p><br class="spacer_" /></p>
<p>To follow this tutorial, you&#8217;ll need the <a href="http://jquery.com/">jQuery Library</a>.</p>
<p><br class="spacer_" /></p>
<p>Let&#8217;s start out web 1.0, with just a simple form with a submit button. You&#8217;ll want a simple HTML form that looks like this:</p>
<p><code lang="html4strict"><br />
<form id="contact" action="contact.php" method="post">
<h2>Contact Me</h2>
<p>	Email:</p>
<input id="contact_email" name="contact_email" type="text" />
	<textarea id="contact_text" name="contact_text"><br />
	</textarea></p>
<input id="contact_submit" name="contact_submit" type="submit" value="Send" />
</form>
<p></code></p>
<p><br class="spacer_" /></p>
<p>So now that we have a form, we&#8217;ll need a PHP page to process that form. What we&#8217;ll do is take the message, put it into an email and email it to you (you&#8217;ll need to put your email, or whatever email you want to receive contact requests). Put this code into a &#8216;contact.php&#8217; file:</p>
<p><br class="spacer_" /></p>
<p><code lang="php"><br />
<?php<br />
	if (!empty($_POST['contact_text']) &#038;&#038; !empty($_POST['contact_email'])) {<br />
		$to = 'webmaster@example.com';<br />
		$subject = 'Contact Request from '.$_POST['contact_email'];<br />
		$message = $_POST['contact_text'];<br />
		$headers = 'From: '.$_POST['contact_email']." \r\n" .<br />
			'Reply-To: '.$_POST['contact_email']."\r\n" .<br />
			'X-Mailer: PHP/' . phpversion();<br />
		mail($to, $subject, $message, $headers);<br />
		echo "Thanks for contacting me!";<br />
	} else {<br />
		echo "Please enter your email and a message.";<br />
	}<br />
?><br />
</code></p>
<p><br class="spacer_" /></p>
<p>Basically, that code checks to make sure that both <code>$_POST['contact_email']</code> and <code>$_POST['contact_text']</code> are defined, and then it constructs an email to you (webmaster@example.com). In the message is the text that they entered. Then it prints a friendly message.</p>
<p><br class="spacer_" /></p>
<p>The only problem with what we have so far is that the contact form is very vulnerable to automatic spam. Not quite as bad as putting your email out there, but it is still pretty easy for spammers to just send a request to your &#8216;contact.php&#8217; page and bombard you with emails. There is a way to prevent this from happening. It involves using PHP&#8217;s $_SESSION superglobal to make sure that the contact-er is using your contact form, and not just a single request to contact.php. You need to add a little PHP code to your contact form to make this work. Because we are going to use the session superglobal, we need to set up the session at the top of your pages. In both the page holding the form AND contact.php, put this code at the top:</p>
<p><code lang="php"><br />
<?php session_start(); ?><br />
</code></p>
<p><br class="spacer_" /></p>
<p>Now, what we&#8217;re going to do is make a random string (an md5 hash of a random number between 1 and 10000), and put it into the form as a &#8216;hidden&#8217; input field. We also store the hash in a session variable, so we can compare the two in contact.php. Here&#8217;s what your form should look like now.</p>
<p><code lang="php"></p>
<form id="contact" name="contact" method="post" action="contact.php">
<h2>Contact Me</h2>
<p>	Email:<br />
<input type="text" name="contact_email" id="contact_email" />
	<textarea name="contact_text" id="contact_text"></p>
<p>	</textarea><br />
	<?php<br />
	$randhash = md5(rand(0,10000));<br />
	$_SESSION['contact_key'] = $randhash;<br />
	?></p>
<input type="hidden" name="contact_key" value="<?php echo $randhash; ?>" /></p>
<input type="submit" id="contact_submit" name="contact_submit" value="Send" />
</form>
<p></code></p>
<p><br class="spacer_" /></p>
<p>(Notice the PHP lines and the new &#8216;input&#8217; tag).</p>
<p>And now in contact.php, we need to make sure that the two keys are the same. This is what contact.php should look like:</p>
<p><code lang="php"><br />
<?php<br />
session_start();<br />
if (isset($_POST['contact_key'])) {<br />
	if ($_POST['contact_key'] == $_SESSION['contact_key']) {<br />
		if (!empty($_POST['contact_text']) &#038;&#038; !empty($_POST['contact_email'])) {<br />
			$to      = 'webmaster@example.com';<br />
			$subject = 'Contact Request from '.$_POST['contact_email'];<br />
			$message = $_POST['contact_text'];<br />
			$headers = 'From: '.$_POST['contact_email']." \r\n" .<br />
				'Reply-To: '.$_POST['contact_email']."\r\n" .<br />
				'X-Mailer: PHP/' . phpversion();<br />
			mail($to, $subject, $message, $headers);<br />
			echo "Thanks for contacting me!";<br />
		} else {<br />
			echo "Please enter your email and a message.";<br />
		}<br />
	}<br />
}<br />
?><br />
</code></p>
<p><br class="spacer_" /></p>
<p>Great! Now we have a functioning contact form. The only problem is, when people use the form, they are sent to a boring page with only text in it. So let&#8217;s make the form asynchronous with javascript. jQuery javascript, actually. The script will wait for the form submit button to be pushed, and then will use AJAX to send a request to contact.php, thus sending the contact request. When that finishes, it will append the response text to the form to give the contact-er a result (good if all fields were filled in, bad if they forgot their email or a message). All you need to do is put a little bit of code into your page with the form.</p>
<p><code lang="javascript">contactform = $('form#contact');<br />
contactform.submit(function() {<br />
	$.post('/contact.php', $(this).serialize(), function(data) {<br />
		$('form#contact').append("<br />"+data);<br />
	});<br />
	return false;<br />
});<br />
</code></p>
<p>So there we go. Somebody can, with javascript enabled, just press the send button and send the contact request to your email asynchronously, without your putting your email out to be picked up by spam bots. The best thing about this is that when somebody isn&#8217;t running javascript in their browser, they will still be able to use the form effectively.</p>
<p>Obviously you&#8217;d want to style up your form and maybe change around the &#8216;submit&#8217; event to make it a little more stylish, but we have here a solid, secure AJAX-PHP contact form which will help make your site more user-friendly and accessible.</p>
<p>UPDATE: If you want to make sure that your form is validated, check out <a href="http://www.cyclonicstudios.com/php/validating-a-contact-form-with-php-and-the-gd-library/">this</a> tutorial. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.cyclonicstudios.com/making-a-secure-contact-form-with-php-and-jquery/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

