Forum Moderators: coopster

Message Too Old, No Replies

PHP Form to Email

Help me make it bomb proof

         

gecko001

9:56 am on Aug 3, 2006 (gmt 0)

10+ Year Member



Hi guys i am wondering if someone can point me in the right direction.
I have searched here with no luck, so far i have managed to get a couple of bits of code off the net and one from here and hang them together.

Basically i am trying to stop robots from spamming the form with links to porn sites and other such junk.

My form posts to this code "thankyou.php" and it sends it on to the site owner.

<?php
function check_referer($referers)
{
//If there are any referrers in the list ...
if (count($referers))
{
$found = false;

// Use the browsers referrer header.
$temp = explode("/",getenv("HTTP_REFERER"));
$referer = $temp[2];

if ($referer=="")
{
$referer = $_SERVER['HTTP_REFERER'];
list($remove,$stuff)=split('//',$referer,2);
list($home,$stuff)=split('/',$stuff,2);
$referer = $home;
}

// Check agains list.
for ($x=0; $x < count($referers); $x++)
{
if (eregi ($referers[$x], $referer))
{
$found = true;
}
}

// Refererer is blank.
if ($referer =="")
$found = false;

if (!$found)
{
// You might alter this to print some sort of error of your own.
echo ("You are coming from an <b>unauthorized domain.</b>");
error_log("[formMail.php] Illegal Referer. (".getenv("HTTP_REFERER").")", 0);
}

return $found;

}
else
{
return true;
}
}

?>
<?php
//check referer
$referers = array ('anydomain.com','www.anydomain.com','205.48.267.71');
if (!check_referer($referers))
{
echo "<h1>Sorry It didn't work.</h1>\n";
echo "<p>Please go back to the form you came from and try again</p>";
}
else {
$sent =0;
$email = $HTTP_POST_VARS[Email];
$mailto = "me@anydomain.com";
$mailsubj = "Booking Enquiry from website(new form)";
$mailhead = "From: $email\n";
reset ($HTTP_POST_VARS);
$mailbody = "Details submitted from new web site form:\n";
while (list ($key, $val) = each ($HTTP_POST_VARS)) { $mailbody .= "$key : $val\n"; }
if (!eregi("\n",$HTTP_POST_VARS[email])) { mail($mailto, $mailsubj, $mailbody, $mailhead); }
echo "<h1>Thank you for your enquiry</h1>\n";
echo "<p>Your booking enquiry has been sent to us.</p>\n";
echo "<p>We will endeavour to contact you as soon as possible.</p>\n";
}
?>

Any help would be appreciated.

gecko001

12:45 am on Aug 4, 2006 (gmt 0)

10+ Year Member



I am also trying to ensure parts of the form is filled in via java script first before being posted to PHP script

as follows code from chadseo
<script>
function validateform(frm) {
if(frm.name.value.length ==0 ) {
alert("Please enter a Name!");
return false;
}
else {return true; }
}
</script>

<form ... onSubmit="return validateform(this);">

Of course, javascript can be disabled, so always validate the information on next page as well using PHP, but this will notify most people if they forget a required field, etc. You can do multiple checks to: make sure an email address is entered, make sure it's in the correct format, make sure a "new_name" has a value if they choose "New" from a drop-down, etc.

jatar_k

11:22 am on Aug 4, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I use php for all my form validation then I don't have to worry about whether js is on or off and it is just better imo

as far as your top code

is it not working or are there errors?

GrapefruiTgirl

12:54 am on Aug 13, 2006 (gmt 0)

10+ Year Member



Looks like I am using virtually the exact same code, chopped from the same script I'd say. I am trying to run this on my localhost testing server, which may be part of the problem, but here are code and the errors I get; any advice would be greatly appreciated :) (line numbers relevant to the errors are in BOLD. THANK YOU :)

<?
$referers = array ('127.0.0.1','192.168.0.0','192.168.0.1','http://localhost');//problem here because of testing server setup?
function check_referer($referers)
{
5 if (count($referers))
{
$found = false;
$temp = explode("/",getenv("HTTP_REFERER"));
$referer = $temp[2];
if ($referer=="")
11 {
$referer = $_SERVER['HTTP_REFERER'];
list($remove,$stuff)=split('//',$referer,2);
list($home,$stuff)=split('/',$stuff,2);
15 $referer = $home;
16 }
17 for ($x=0; $x < count($referers); $x++)
{
if (eregi ($referers[$x], $referer))
{
$found = true;
}
}
if ($referer =="")
$found = false;
if (!$found)
{
echo "You are coming from an <b>unauthorized domain.</b>";
return $found;
}
else
return false;
}
}
?>

No matter what I do, I get the following:

Notice: Undefined offset: 2 in C:\Program Files\Apache Software Foundation\Apache2\htdocs\SGFG-root\19910606\chkref.php on line 11

Notice: Undefined index: HTTP_REFERER in C:\Program Files\Apache Software Foundation\Apache2\htdocs\SGFG-root\19910606\chkref.php on line 15

Notice: Undefined offset: 1 in C:\Program Files\Apache Software Foundation\Apache2\htdocs\SGFG-root\19910606\chkref.php on line 16

Notice: Undefined offset: 1 in C:\Program Files\Apache Software Foundation\Apache2\htdocs\SGFG-root\19910606\chkref.php on line 17

You are coming from an unauthorized domain.

Warning: Missing argument 1 for check_referer() in C:\Program Files\Apache Software Foundation\Apache2\htdocs\SGFG-root\19910606\chkref.php on line 5

jatar_k

3:54 pm on Aug 13, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well, first I am betting there are 2 lines of blank space in the beginning of your file

Notice: Undefined offset: 2 on line 11

I am betting that is here

$referer = $temp[2];

the referer probably didn't have three parts, which means your offset is no good and therefore it throws off your whole script

always fix the top error first and then rerun the script to see what changes. Try using something static that is of the desired format and then go from there.

I don't really get parts of that script to be honest. Not that I don't understand the code, it is more the logic I find a little vague.

Are you just trying to take the referer and test it against an array of banned referers? Then show them a blocked message if they come from one of them?

John_Keates

12:51 am on Aug 14, 2006 (gmt 0)

10+ Year Member



I've got a small idea to make it bomb proof... If the bomber has a static ip for at least your specified time.

Make the mailer Ip/time secured so an external IP can only send an email with your system every 5 minits or so. You should keep a log too.

GrapefruiTgirl

1:54 am on Aug 14, 2006 (gmt 0)

10+ Year Member



Hmm, ok, last thing first: re: what John Keats said;

The mailer should only be allowed to work when the referring page is within the domain of the mailer itself; i.e.- if you havent landed on the mailer page via a navigation button elsewhere on the same site, the mailer shouldn't work, infact I'd prefer if the form didn't even materialize on the page, just a redirect. But for now, as long as it doesnt send mail, that's fine. The script from which I hacked this code did keep a log, but I don't much feel I need to do that; I could reimplement it, but dont think I need it.
The mail only goes to one address too (the form-filler-person doesnt get to enter an email to send to; the mail technically goes from me and to me, regardless).

Now, Jatar: The referer indeed doesn't have 3 parts, because I am running this on my testing server (my to-be host isn't ready yet, they are transferring to a new server and having some difficulties, and meanwhile, my current host doesn't support php so I can't test it live); I think that might be part of the problem, but havent come up with a way of simulating a real referer or altering the allowed-referer-array to work with the localhost address, which is something like:
Http://localhost/directory/directory/directory/filename.php.
This code is cut from a free open source form mail script, and I haven't altered it other than to remove irrelevant stuff (logging, banned-IP check, etc); so if it is illogical, LOL blame the original designer, as I am not proficient near enough in this stuff yet to detect illogicalness :)
And no, it isnt to check for banned referers, it is only to see if the referer is on my site (so the mail script cannot be used by someone outside the site/domain) in other words, every referer is a banned one, except those within the same site.
Thank you guys for the help;

GrapefruiTgirl

2:03 am on Aug 14, 2006 (gmt 0)

10+ Year Member



I was just thinking, and came up with a better idea..
There must be an easier way, so how about this:

Since I will know what the absolute path of the referer calling this script should be, can't I simply compare the actual referer and what the referer should be and if they don't match, redirect the user to another page or an error?
This mail script should ONLY be accessed by the user having pressed the CONTACT link on the navigation bar of my site; anywhere else calling the script should be NOT allowed.

Sasha

FiRe

9:22 am on Aug 14, 2006 (gmt 0)

10+ Year Member



dont use complicated refer processes, people can have their refer disabled and it maybe a geniune user. stick a captcha protection on the form instead.

GrapefruiTgirl

4:00 pm on Aug 14, 2006 (gmt 0)

10+ Year Member



UPDATE:
On above advice, I have now tried 3 CAPTCHA routines in PHP.. The first 2 were incomplete, requiring modification beyond my current scope, and the third, which I basically have working, requires a higher version of the GD Library (2.0.2) which I don't seem to have, though I have 2.0.something.. Grrr..

I would still like to get the referer header thing working, if only for my own knowledge/learning. The captcha is neato too; I'd really like them both working, and then decide what to use... :)