Add Math Protection to contact_us.php

By | May 11, 2012

This morning I was contacted to see about adding a question to osCommerce just like I have on this blog – where I ask a math question…I was going to suggest using Google recaptcha service, but the client did not want that.

And so…

Adding a question such as “what is 3+5” cuts down on automated bots spamming the heck out of you. A good place to implement this in contact_us.php, and the code changes are very simple.

The basis of the change is to create two random numbers, and ask the person contacting you to insert the total into an input field. The field is then checked to see if the answer is correct. If it is correct, then the contact email is sent to the shop owner. If the answer is incorrect, a “fail” message appears.

Step 1. Add the language defines to the language file.

We need two new defines, one for the “question” and one for the response if the question is incorrect.

/includes/languages/english.php
(and any other languages you operate in your shop)

https://pastebin.com/EGtFrYrc

Step 2. Add the logic to the contact file.

Here we need to create two numbers, add them up and ask the question to the user. http://pastebin.com/L7zaX7Tr

And that is all there is to it. Try it out in your store if you wish.

Notes

In line 21 of the pastebin I used base64_encode to create a hidden “answer” variable which creates something to check the users answer against. In line 6 I decode it, and then in line 8 I check one against the other. I use base64 to “hide” the value of $a + $b from prying eyes.

My use of base64_encode and base64_decode is no more than that…if you are unsure of what I am doing in the code, I suggest to ask question to help yourself to understand more fully, or not use the code. The base64 code might (assuming you use “site monitor”) ring alarm bells. I guarantee that if you use my “math protection” code as is, there is nothing harmful in it.

Summary

A useful way to cut down on the spam that is sent via contact forms. You could also add this to any other form in a similar way (eg, create_account.php, tell_a_friend.php and so on).

7 thoughts on “Add Math Protection to contact_us.php

  1. Eddy

    Hi Gary – can we use convert_uuencode() and convert_uudecode() instead?

  2. Gary Post author

    Hi Eddy, that should work fine.

  3. Eddy

    Hi Gary – I replaced “base64_decode” & “base64_encode” with “convert_uudecode” & “convert_uuencode” and received this error:

    “Warning: convert_uudecode() [function.convert-uudecode]: The given parameter is not a valid uuencoded string.”

    Any ideas?

    Cheers, Eddy

  4. Gary Post author

    Not sure Eddy. I’d say stick with base64 or just remove the base64 entirely and replace with nothing. It does not have to be in there, the only reason it is, is to stop the person commenting from looking at the source code to get the answer to the question…

  5. Matthew Lyons

    I tried adding this on my rc2.2 shop and it works but the equation shows like this “What is the sum of %s and %s ?32”

  6. Gary Post author

    Means you have not installed correctly. Try again.

Leave a Reply

Your email address will not be published.