Jump to content

Problem with PHP Form Script


Ricky55

Recommended Posts

Hi

 

I have a form setup which works fine but it requires that all fields are complete before you submit.

 

I want all fields to be required apart from the email address, could anyone have a look at the script and tell me how it needs to be modified to take out the email field from being required.

 

Thanks By the way I am a complete newbie with PHP.

 

the script

 

<?php
$after = "thanks.html"; 
$oops = "oops.html";

if (!isset($_POST['submit']) || $_SERVER['REQUEST_METHOD'] != "POST") {
    exit("<p>You did not press the submit button; this page should not be accessed directly.</p>");
} else {
    $exploits = "/(content-type|bcc:|cc:|document.cookie|onclick|onload|javascript|alert)/i";
    $profanity = "/(beastial|bestial|blowjob|clit|cock|cum|cunilingus|cunillingus|cunnilingus|cunt|ejaculate|fag|felatio|fellatio|****|fuk|fuks|gangbang|gangbanged|gangbangs|hotsex|jism|jiz|kock|kondum|kum|kunilingus|orgasim|orgasims|orgasm|orgasms|phonesex|phuk|phuq|porn|pussies|pussy|spunk|xxx)/i";
    $spamwords = "/(viagra|phentermine|tramadol|adipex|advai|alprazolam|ambien|ambian|amoxicillin|antivert|blackjack|backgammon|texas|holdem|poker|carisoprodol|ciara|ciprofloxacin|debt|dating|porn)/i";
    $bots = "/(Indy|Blaiz|Java|libwww-perl|Python|OutfoxBot|User-Agent|PycURL|AlphaServer)/i";

    if (preg_match($bots, $_SERVER['HTTP_USER_AGENT'])) {
        exit("<p>Known spam bots are not allowed.</p>");
    }
    foreach ($_POST as $key => $value) {
        $value = trim($value);

        if (empty($value)) {
            exit("<p>Empty fields are not allowed. Please go back and fill in the form properly.</p>");
        } elseif (preg_match($exploits, $value)) {
            exit("<p>Exploits/malicious scripting attributes aren't allowed.</p>");
        } elseif (preg_match($profanity, $value) || preg_match($spamwords, $value)) {
            exit("<p>That kind of language is not allowed through our form.</p>");
        }

        $_POST[$key] = stripslashes(strip_tags($value));
    }

    if (!ereg("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,6})$",strtolower($_POST['email']))) {
        exit("<p>That e-mail address is not valid, please use another.</p>");
    }

    $recipient = "07932234312@text.aql.com, jasonhutchinson32@btinternet.com";
    $subject = "Web Site Enquiry";
    
    $message .= "Name: {$_POST['name']} \n";
    $message .= "Email: {$_POST['email']} \n";
    $message .= "Contact: {$_POST['contact']} \n";
    $message .= "Address: {$_POST['address']} \n";
    $message .= "Postcode: {$_POST['postcode']} \n";
    $message .= "Vehicle: {$_POST['vehicle']} \n";
    $message .= "Damage: {$_POST['damage']} \n";




    $headers .= "Reply-To: <{$_POST['email']}>";

    if (mail($recipient,$subject,$message,$headers)) {
        echo "<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=$after\">"; 
    } else {
        echo "<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=$oops\">";
    }
}
?>

 

 

 

 

Link to comment
Share on other sites

<?php
$after = "thanks.html"; 
$oops = "oops.html";

if (!isset($_POST['submit']) || $_SERVER['REQUEST_METHOD'] != "POST") {
    exit("<p>You did not press the submit button; this page should not be accessed directly.</p>");
} else {
    $exploits = "/(content-type|bcc:|cc:|document.cookie|onclick|onload|javascript|alert)/i";
    $profanity = "/(beastial|bestial|blowjob|clit|cock|cum|cunilingus|cunillingus|cunnilingus|cunt|ejaculate|fag|felatio|fellatio|****|fuk|fuks|gangbang|gangbanged|gangbangs|hotsex|jism|jiz|kock|kondum|kum|kunilingus|orgasim|orgasims|orgasm|orgasms|phonesex|phuk|phuq|porn|pussies|pussy|spunk|xxx)/i";
    $spamwords = "/(viagra|phentermine|tramadol|adipex|advai|alprazolam|ambien|ambian|amoxicillin|antivert|blackjack|backgammon|texas|holdem|poker|carisoprodol|ciara|ciprofloxacin|debt|dating|porn)/i";
    $bots = "/(Indy|Blaiz|Java|libwww-perl|Python|OutfoxBot|User-Agent|PycURL|AlphaServer)/i";

    if (preg_match($bots, $_SERVER['HTTP_USER_AGENT'])) {
        exit("<p>Known spam bots are not allowed.</p>");
    }
    foreach ($_POST as $key => $value) {
        $value = trim($value);

        if (empty($value)) {
        	if ($key != "email")  
        		{
            exit("<p>Empty fields are not allowed. Please go back and fill in the form properly.</p>");      
            	}
        } elseif (preg_match($exploits, $value)) {
            exit("<p>Exploits/malicious scripting attributes aren't allowed.</p>");
        } elseif (preg_match($profanity, $value) || preg_match($spamwords, $value)) {
            exit("<p>That kind of language is not allowed through our form.</p>");
        }

        $_POST[$key] = stripslashes(strip_tags($value));
    }

    if (!ereg("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,6})$",strtolower($_POST['email']))) {
        exit("<p>That e-mail address is not valid, please use another.</p>");
    }

    $recipient = "07932234312@text.aql.com, jasonhutchinson32@btinternet.com";
    $subject = "Web Site Enquiry";
    
    $message .= "Name: {$_POST['name']} \n";
    $message .= "Email: {$_POST['email']} \n";
    $message .= "Contact: {$_POST['contact']} \n";
    $message .= "Address: {$_POST['address']} \n";
    $message .= "Postcode: {$_POST['postcode']} \n";
    $message .= "Vehicle: {$_POST['vehicle']} \n";
    $message .= "Damage: {$_POST['damage']} \n";




    $headers .= "Reply-To: <{$_POST['email']}>";

    if (mail($recipient,$subject,$message,$headers)) {
        echo "<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=$after\">"; 
    } else {
        echo "<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=$oops\">";
    }
}
?>

Try that.

Link to comment
Share on other sites

Hi

 

That worked when I also removed this section of code

 

    if (!ereg("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,6})$",strtolower($_POST['email']))) {
        exit("<p>That e-mail address is not valid, please use another.</p>");
    }

 

With that present even when the email is left blank it through up the error about the email address not being valid is there an way we can modify this still checks that the email address is valid when one is used but also allows no email to be used.

 

Thanks very much

 

Ricky55

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.