tags:

views:

88

answers:

3
A: 

This is very complicated (script you wrote). Essentially you need two scripts, one for ajax and second one normal php contact form. Lets call the first one ajax.php (both script contain sam validation rules), when user enters for example his mail (you bind jquery blur event to email) a request is sent to...

ajax.php?action=checkmail&[email protected]

You can then print return by email input box, eg "Your email is valid." or contrary.

You can also use only ajax.php to send mail by passing all parameters to it, and then showing user "email is sent" or "email is not sent because..." without refreshing the page.

Webarto
I think this is above my head is there anyway you can show me? Also should I not be using Eregi?
Jacinto
A: 

Well this is quite nice solution, a working one. I've seen much worst around.
Only a few notes.

  • it has some unnecessary validations, say contains_bad_str(body); is useless. And whole "bad words" approach at all. Newline test is enough
  • just a rule: DO NOT TALK TO STRANGERS. Never talk to possible attacker, providing them with any feedback. All these "Suspected injection attempt" are childish and more of it - it supply an attacker with information they can use. Always send just 500 HTTP error in all these cases and nothing else.
  • you're using pretty unusual way of error handling. I can't understand the difference between these code snippets

why the first one just sets a variable

if(trim($_POST['emailTo']) == '') {
    $hasError = true;

and second one prints an error and exits?

if (!is_valid_email($email)) {
  echo 'Invalid email submitted - mail not being sent.';
  exit;
}

Will you provide a fair user with any feedback on error occurred? Something like "Please fill all required fields"?

  • I wouldn't make user supplied e-mail subject but rather some hardcoded sentence, like Feedback from ".$_SERVER['HTTP_HOST']. I'd like to distinguish feedback messages in my mailbox from the regular ones. (I wouldn't make that fancy reply-to feature either, but it's my own preference)

  • stripslashes thing. Why only message? Aren't other fields behave the same? And at least you have check get_magic_quotes_gpc() before applying. I'd make it automated, at hte top of the script, both stripslashes and trim, just in the loop over $_POST array.

Col. Shrapnel
How do I redirect to 500 http?Thats just based off the tutorial I read should it not be set as a variable and then ext is that bad practice? Can you show me a better way?Also I dunno about the magic quotes thing can you explain how or show me an example?Sorry I am still very noob at thisThanks
Jacinto
@Jacinto not redirect. just send a 500 error using `header()` function and exit. Aside from tutorials - how do you think your form should look and behave? Just from the users point of view? Did you happen to use such a forms on the other sites? What is your own opinion? magic quotes are well explained in the manual, just google for `php magic quotes`
Col. Shrapnel
A: 

I'd suggest that three year old error-ridden articles from phpbuilder.com are probably not the best source to be using to concoct your own mailer. Instead, I'd be using something more up-to-date like the validate and sanitize filters in PHP 5 SPL.

I'd also take a look at PHPMailer or some other library that will perform security checks for you.

Nev Stokes