Fixing Jack's formmail.php for register_globals = off
For many years now, I've used various versions of Jack's FormMail.php to handle simple form submissions on various websites, mostly 'contact us' forms. The script has gotten a little more sophisticated with time, but it's still one of the simpler ways of making a website 'interactive.'
The website at work uses it to send the marketing department emails when someone fills in the contact us form. The form is somewhat sophisticated, in that it presents different information depending on a few parameters in the URL, and passes formmail.php a different subject for the email depending on what configuration it's in. It also passes a redirect URL in a hidden field, so the user is redirected to a 'thanks for contacting us' page after submitting the form. Google Analytics is set up so that any hits on the 'thanks' page after viewing the contact page counts as a Goal. And this is where I spotted the problem - as I checked through the Analytics stats I noticed we hadn't achieved any goals since the middle of last week, although a day or two here and there without any is fairly common five on the trot is pretty rare.
My first step was to go to the contact form and try submitting it, the email duly arrived in the correct mailbox but the script didn't redirect to the thanks page. So first mystery solved, no hits on the thanks page means no goals on Analytics, but the marketing department hadn't noticed because they were still getting emails. I messed around with some of the different configurations of the contact page but none of them redirected as intended. Then I realised that all the emails had the same subject, and that was the default one given by formmail.php when it doesn't get passed a subject, though the values in the form fields were appearing in the email correctly, so it wasn't like the information wasn't getting passed though.
I checked the web host's page and discovered that they had recently updated the PHP config on the server, this in order to support PHP v5. There was a new option in CPanel to select the default version of PHP, so I went and set it to v4 in case formmail was incompatible with the newer release. It didn't make any difference though, so I started looking at formmail.php itself in detail.
The first thing I noticed was that the redirect functionality required register_globals
to be enabled. Although my PHP configuration page claimed that register_globals
was enabled, the behaviour of the script indicated that it wasn't. Unsurprisingly, the subject field also expected register_globals
to be enabled, as did several other fields which I wasn't using, but the code which builds the email uses $HTTP_POST_VARS
so works fine without.
If you've managed to read this far without falling asleep and you know what register_globals
is, then you now have enough information to fix the problem yourself and there's no need to torture yourself any further. If you currently have no idea what I'm on about, I'm now going to try and explain and then show you how to edit your script to fix the problem. register_globals
is a configuration setting for the PHP interpreter, if it's set to on
then PHP will helpfully make any parameters passed into the script global variables. Parameters are the bits in the URL after the question mark, or fields in a form. This can be quite handy when you're slapping together a quick script, but is also very dangerous because, if you're not careful, you can create easy opportunities for hackers to do nasty things. For this reason, register_globals = on
has long been discouraged in the PHP community, unfortunately many useful scripts were written in the days before anyone realised it was an issue, so web hosts usually provide a way turn it back on even if it's not enabled by default.
However, today we're going to make formmail.php work properly with register_globals = off
. You're going to need to open the formmail.php script in a text editor - Notepad will do fine. First we shall fix the redirect issue, find the following lines near the end of the file:
if ($redirect) { header("Location: $redirect"); exit;
We're going to replace $redirect
, which is trying to access the global variable, with $_POST['redirect']
, like so:
if (isset($_POST['redirect'])) { header("Location: ". $_POST['redirect']); exit;
The same trick works with the subject and email address, scroll up a few lines and find this:
mail_it(stripslashes($content), ($subject)?stripslashes($subject):"Form Submission", $email, $recipient);
Replace it with:
mail_it(stripslashes($content), isset($_POST['subject'])?stripslashes($_POST['subject']):"Form Submission", $_POST['email'], $recipient);
Now upload your new version to the server and you should find things start working as before. As you can see the general rule is to replace $variable
with $_POST['variable']
, notice that I didn't change $recipient
because I always hard code that value at the top of the script as a security precaution.
Print article | This entry was posted by robertc on 03/03/08 at 11:37:49 pm . Follow any responses to this post through RSS 2.0. |