Page 1 of 1

php validation with captcha

Posted: Fri May 26, 2017 2:10 pm
by cjkeane
Hi everyone,

I've gone over and over my code and I just can't seem to identify where the issue is. There don't seem to be any validation errors, yet my coding says there is.

Any help would be appreciated to point me in the right direction. The captcha code is correct.

Code: Select all

<?php 
        	$error = false;
        	if(isset($_POST['submit']) && !empty($_POST['submit'])) {
        		$full_name = isset($_POST['full_name']) ? trim($_POST['full_name']) : '';
 	           	$email_address = isset($_POST['email_address']) ? trim($_POST['email_address']) : '';
    	                $company_name = isset($_POST['company_name']) ? trim($_POST['company_name']) : '';
      	      	        $phone_number = isset($_POST['phone_number']) ? trim($_POST['phone_number']) : '';
      	      	        $location = isset($_POST['location']) ? trim($_POST['location']) : '';
			$services_required = isset($_POST['services_required']) ? ($_POST['services_required']) : '';
        	        $comment = isset($_POST['comment']) ? trim($_POST['comment']) : '';
            	        $response = $_POST["g-recaptcha-response"];			 
                        if (!preg_match("/^[a-zA-Z ]+$/",$full_name))  { 
				$error = true; $full_name_error = "Please Enter Valid Full Name"; 
			} else {
				if(strlen($full_name) < 5) { $error = true; $full_name_error = "The Full Name field must be more than 5 characters"; }
			}
            	        if(!filter_var($email_address,FILTER_VALIDATE_EMAIL)) {  $error = true; $email_address_error = "Please Enter Valid Email Address"; }
            	        if(empty($company_name)) { $error = true; $company_name_error = "Please Enter Your Company Name"; }
            	        // validate phone number field to have just numbers
			// (?=.*[0-9]) postive look ahead. Ensures that there is atleast one digit
			// [- +()0-9]+ matches numbers, spaces, plus sign, hyphen and brackets
			if (!preg_match('/^(?=.*[0-9])[- +()0-9]+$/',$phone_number))  { 
				$error = true; $phone_number_error = "Please Enter a Valid Phone Number (including dashes and brackets)"; 
			} else {
				if(strlen($phone_number) < 8) { $error = true; $phone_number_error = "The Phone Number field must be more than 8 numbers including dashes and brackets"; }
			}
			if(empty($services_required)) { $error = true; $services_required_error = "Please Select at least one Service"; }
			if(empty($location)) { $error = true; $location_error = "Please Select a Location"; }
            	        if(empty($comment)) { 
                            $error = true; $comment_error = "Please Enter Your Comments"; 
			} else {
			    if(strlen($comment) < 20) { $error = true; $comment_error = "The comment field must be more than 20 characters"; }
			}
            		if(isset($_POST['g-recaptcha-response']))	{$captcha=$_POST['g-recaptcha-response']; }
            	       	$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=...&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);	
            	        $googleobj = json_decode($response);
            	        $verified = $googleobj->success;
            	        if ($verified === true){
                	    if (!$error) {
				if (isset($_POST['services_required'])) {$services_list = "<ul><li>" . implode("</li><li>", $_POST['services_required']) . "</li></ul>";}
                       	            $body = "
				        <html>
						<body>
							<table rules='all' style='border-color: #666;' cellpadding='10' width='697'>
								<tr><td colspan='2'><h1><strong>Request A Quote</strong></h1></td></tr>
								<tr><td colspan='2'><h2 style='background: #eaf1dd; color:#660066'><strong>Contact Information</strong> for:</h2></td></tr>
                                   	                        <tr><td width='323'> Name</td><td width='374'>$full_name</td></tr><tr><td>Email Address</td><td>$email_address</td></tr> 
					              	        <tr><td>Company Name</td><td>$company_name</td></tr>
                                   	                        <tr><td> Phone Number</td><td>$phone_number</td></tr>
                                   	                        <tr><td>Location</td><td>$location</td></tr>
								<tr><td align='left' valign='top'>Services Required</td><td>$services_list</td></tr>
                                   	                       <tr><td align='left' valign='top'>Comments:</td><td>$comment</td></tr>
                               	                         </table>
                           	                     </body>
						</html>";  
                                                require_once 'class.phpmailer.php';
                                                $mail = new PHPMailer;
                                                $mail->CharSet = 'UTF-8';
                                                $mail->IsHTML(true);
                                                $mail->From = $email_address;
                                                $mail->FromName = $email_address;
                                                $mail->addAddress('info@gmail.com');
						$mail->Subject = "RE: Request A Quote";
                                                $mail->Body = $body;
                                                if(!$mail->send()) {
                           	                    echo '<div class="alert alert-danger" role="alert" >Message Failed <i class="glyphicon glyphicon-thumbs-down"></i> Sorry there was an error sending your message. </div>';
                                                } else {
                           	                    echo '<div class="alert alert-success" role="alert">Success <i class="glyphicon glyphicon-thumbs-up"></i> Thanks for contacting us, we will get back to you shortly.</div>';
                       	                        } 
                	                 } else {
                   	 	             echo '<div class="alert alert-danger" role="alert" >Captcha Failed <i class="glyphicon glyphicon-thumbs-down"></i> Sorry there was an error validating the captcha. Please try again! </div>';
					}
				} else {
					echo '<div class="alert alert-danger" role="alert" >Message Failed <i class="glyphicon glyphicon-thumbs-down"></i> Sorry there were validation errors. Please check the form and try again. </div>';
            	                 }
			 }
        ?>


Re: php validation with captcha

Posted: Fri May 26, 2017 4:47 pm
by requinix
That error message comes from the $verified==true condition. Which means it wasn't verified. Dump out the contents of $googleobj or $response to see what's going on.