Secure 1 field php form - help

Discussions of secure PHP coding. Security in software is important, so don't be afraid to ask. And when answering: be anal. Nitpick. No security vulnerability is too small.

Moderator: General Moderators

Post Reply
Davidjohny
Forum Newbie
Posts: 1
Joined: Thu Apr 05, 2012 12:24 pm

Secure 1 field php form - help

Post by Davidjohny »

Hello
I have a website with an online email newsletter subscription form in PHP.

Current status:

Step1/- A visitor fills in his email address.
Step 2/- Next, I get visitor email address inside my INBOX.

Current PHP form consist of two pages

================================================
[[[ Page with PHP Form Code | Page NO.1 Formpage.php ]]]

<html><body>
<form name="myform" method="post" action="SEND.php"><br> <br />
<input name="email" type="text" id="email" />
<br />
<input type="submit" name="Submit" value="Submit" />
</form>
</body></html>
================================================
[[[ Confirmation Page Code | Page NO.2 SEND.php ]]]

<?php
$email = $_REQUEST['email'] ;
mail( "myemail@mydomain.com", "Newsletter Request", "From: $email" );
?>
================================================
I have heard about php code injection, malicious script and spam.
How can I secure this simple php two pages form?

Best wishes,
Dave
============
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Secure 1 field php form - help

Post by social_experiment »

Davidjohny wrote:I have heard about php code injection, malicious script and spam.
How can I secure this simple php two pages form?
It's good practise to check the input you receive; check that the data you receive is an email address by using regular expressions. Regarding spam you could implement captcha or a human-test system where a user has to answer a question, type in some word, etc. Check that the form was submitted from your site: set a session variable (or value in a hidden field) and do a check on this value before processing the form

Code: Select all

<?php $email = $_REQUEST['email'] ; ?>
If you have the method of your form as 'post' use $_POST instead of $_REQUEST.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Secure 1 field php form - help

Post by Celauran »

social_experiment wrote:Regarding spam you could implement captcha or a human-test system where a user has to answer a question, type in some word, etc.
And/or use a honeypot.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Secure 1 field php form - help

Post by social_experiment »

Celauran wrote:And/or use a honeypot.
Interesting; how would this be setup, if i only have access to one web server? Or is this something that will only work on a network?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Secure 1 field php form - help

Post by Celauran »

A simple honeypot can consist of a form field hidden by CSS. Humans won't see it, so they won't fill it in. Bots will see it, however, and tend to fill in everything. If that field contains data, discard the post.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Secure 1 field php form - help

Post by social_experiment »

Ah ok; thanks for the idea :)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Gopesh
Forum Contributor
Posts: 143
Joined: Fri Dec 24, 2010 12:48 am
Location: India

Re: Secure 1 field php form - help

Post by Gopesh »

Thanks Celauran for the idea of honeypot..:D
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: Secure 1 field php form - help

Post by x_mutatis_mutandis_x »

Celauran wrote:A simple honeypot can consist of a form field hidden by CSS. Humans won't see it, so they won't fill it in. Bots will see it, however, and tend to fill in everything. If that field contains data, discard the post.
Thats very clever and ingenious
Post Reply