I am trying to check on validate. in PrestaShop i do it this way:
if (empty($email)) {
$this->errors[] = Tools::displayError('Email is empty.');
$this->doLog('ERROR: Email/username is empty');
} elseif (!Validate::isEmail($email)) {
$this->errors[] = Tools::displayError('Invalid email address.');
$this->doLog('ERROR: Invalid Email address');
}
Does anyone have any idea how to do this in OpenCart?
Thanks
open this file for example:
catalog/controller/information/contact.php
you will see validate function:
protected function validate() {
if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 32)) {
$this->error['name'] = $this->language->get('error_name');
}
if (!filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL)) {
$this->error['email'] = $this->language->get('error_email');
}
if ((utf8_strlen($this->request->post['enquiry']) < 10) || (utf8_strlen($this->request->post['enquiry']) > 3000)) {
$this->error['enquiry'] = $this->language->get('error_enquiry');
}
// Captcha
if ($this->config->get($this->config->get('config_captcha') . '_status') && in_array('contact', (array)$this->config->get('config_captcha_page'))) {
$captcha = $this->load->controller('extension/captcha/' . $this->config->get('config_captcha') . '/validate');
if ($captcha) {
$this->error['captcha'] = $captcha;
}
}
return !$this->error;
}
In that file, you can also see how this function is used:
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
Related
Hope everything is fine.
I am an ASP.Net Developer, However I am using now php yii framework.
I have a form in which I can attach files to it.
In the form submission, I am doing some validation, for email address and name.
I amusing this method $this->validateInput($_POST['FullName']);
When I attach 5mb file in the form, validation does not work.
$fullName = $this->validateInput($_POST['FullName']);
if (!$fullName || $fullName == "") {
return "ssv_fullname";
}
It return for me ssv_fullname.
However when I attach less than 5 mb, things go fine and the form get submitted.
here is my validation method :
private function validateLoanApplication() {
// $valid = true;
if (isset($_POST["user_answer"]) && substr(md5($_POST["user_answer"]), 5, 10) === $_SESSION['edbcaptchaans'])
{
$valid = true;
}
else
{
$valid = false;
return "Incorrect Verification Code";
}
if($valid)
{
$fullName = $this->validateInput($_POST['FullName']);
if (!$fullName || $fullName == "") {
return "ssv_fullname";
}
$dob = $this->validateInput($_POST['dob']);
if (isset($_POST['dob'])) {
$dob = $this->validateInput($_POST['dob']);
if (!$dob || $dob == "") {
return "ssv_dob";
}
}
if (isset($_POST['Salary'])) {
$Salary = $this->validateInput($_POST['Salary']);
if (!$Salary || $Salary == "") {
return "ssv_salary";
}
}
if (isset($_POST['employer'])) {
$employer = $this->validateInput($_POST['employer']);
if (!$employer || $employer == "") {
return "ssv_employer";
}
}
if (isset($_POST['mobile'])) {
$mobile = $this->validateInput($_POST['mobile']);
if (!$mobile || $mobile == "") {
return "ssv_mobile";
}
}
// if (isset($_post['email']) && $_post['email'] != "") {
// $email = $this->validateinput($_post['email']);
// } else {
// return "ssv_email";
// }
if (isset($_POST['workadrress']) && $_POST['workadrress'] != "") {
$workadrress = $this->validateInput($_POST['workadrress']);
} else {
return "ssv_work_address";
}
if(isset($_POST['loanamount'])){
$loanamount = $this->validateInput($_POST['loanamount']);
if (!$loanamount || $loanamount == "") {
return "ssv_loan_amount";
}
} else {
return "ssv_loan_amount";
}
$loantype = $_POST['loantype'];
// if (isset($_POST['loantype'])) {
// $loantype = $this->validateInput($_POST['loantype']);
// if (!$loantype || $loantype == "") {
// return "ssv_type_of_loan";
// }else {
// return "ssv_loan_amount Test Instead of loan type";
// }
// }
if (isset($_POST['id_name'])) {
$id_name = $this->validateInput($_POST['id_name']);
if (!$id_name || $id_name == "") {
return "ssv_identifier_1";
}
}
if (isset($_POST['id_mobile'])) {
$id_mobile = $this->validateInput($_POST['id_mobile']);
if (!$id_mobile || $id_mobile == "") {
return "ssv_identifier_mobile_1";
}
}
if (isset($_POST['id_relation'])) {
$id_relation = $this->validateInput($_POST['id_relation']);
if (!$id_relation || $id_relation == "") {
return "ssv_identifier_relation_1";
}
}
if (isset($_POST['id2_name'])) {
$id2_name = $this->validateInput($_POST['id2_name']);
if (!$id2_name || $id2_name == "") {
return "ssv_identifier_2";
}
}
if (isset($_POST['id2_mobile'])) {
$id2_mobile = $this->validateInput($_POST['id2_mobile']);
if (!$id2_mobile || $id2_mobile == "") {
return "ssv_identifier_mobile_2";
}
}
if (isset($_POST['id2_relation'])) {
$id2_relation = $this->validateInput($_POST['id2_relation']);
if (!$id2_relation || $id2_relation == "") {
return "ssv_identifier_relation_2";
}
}
$id3_name = $this->validateInput($_POST['id3_name']);
$id3_mobile= $this->validateInput($_POST['id3_mobile']);
$id3_relation = $this->validateInput($_POST['id3_relation']);
if (isset($_POST['houseno'])) {
$house_no = $this->validateInput($_POST['houseno']);
if (!$house_no || $house_no == "") {
return "ssv_house_number";
}
}
if (isset($_POST['street'])) {
$street = $this->validateInput($_POST['street']);
if (!$street || $street == "") {
return "ssv_street";
}
}
if (isset($_POST['area'])) {
$area = $this->validateInput($_POST['area']);
if (!$area || $area == "") {
return "ssv_area";
}
}
if (isset($_POST['Emirate'])) {
$emirate = $this->validateInput($_POST['Emirate']);
if (!$emirate || $emirate == "") {
return "ssv_emirate";
}
}
$district = $this->validateInput($_POST['district']);
$city = $this->validateInput($_POST['city']);
// Changed on 23-05-2018 by mohammad jouhari to increase the size of attachment file to 5 mb
if ($_FILES['doc1']!='') {
if($_FILES['doc1']['size']<=7242880) {
$fileParts= explode('.', $_FILES['doc1']['name']);
$imgext1=strtolower( end($fileParts) );
if($imgext1!='tiff' && $imgext1!='pdf' && $imgext1!='jpg' && $imgext1!='jpeg' && $imgext1!='tif') {
return "ssv_valid_extensions";
}
} else {
return "ssv_valid_size_2";
}
} else {
return "ssv_valid_required";
}
if ($_FILES['doc2']!='') {
if($_FILES['doc2']['size']<=7242880) {
$fileParts= explode('.', $_FILES['doc2']['name']);
$imgext1=strtolower( end($fileParts) );
if($imgext1!='tiff' && $imgext1!='pdf' && $imgext1!='jpg' && $imgext1!='jpeg' && $imgext1!='tif') {
return "ssv_valid_extensions";
}
} else {
return "ssv_valid_size_2";
}
} else {
return "ssv_valid_required";
}
if ($_FILES['doc3']!='') {
if($_FILES['doc3']['size']<=7242880) {
$fileParts= explode('.', $_FILES['doc3']['name']);
$imgext1=strtolower( end($fileParts) );
if($imgext1!='tiff' && $imgext1!='pdf' && $imgext1!='jpg' && $imgext1!='jpeg' && $imgext1!='tif') {
return "ssv_valid_extensions";
}
} else {
return "ssv_valid_size_2";
}
} else {
return "ssv_valid_required";
}
if ($_FILES['doc4']!='') {
if($_FILES['doc4']['size']<=7242880) {
$fileParts= explode('.', $_FILES['doc4']['name']);
$imgext1=strtolower( end($fileParts) );
if($imgext1!='tiff' && $imgext1!='pdf' && $imgext1!='jpg' && $imgext1!='jpeg' && $imgext1!='tif') {
return "ssv_valid_extensions";
}
} else {
return "ssv_valid_size_2";
}
} else {
return "ssv_valid_required";
}
if ($_FILES['doc5']!='') {
if($_FILES['doc5']['size']<=7242880) {
$fileParts= explode('.', $_FILES['doc5']['name']);
$imgext1=strtolower( end($fileParts));
if ($imgext1!='tiff' && $imgext1!='pdf' && $imgext1!='jpg' && $imgext1!='jpeg' && $imgext1!='tif') {
return "ssv_valid_extensions";
}
} else {
return "ssv_valid_size_2";
}
} else {
return "ssv_valid_required";
}
if ($_FILES['doc6']!='') {
if($_FILES['doc6']['size']<=7242880) {
$fileParts= explode('.', $_FILES['doc6']['name']);
$imgext1=strtolower( end($fileParts));
if($imgext1!='tiff' && $imgext1!='pdf' && $imgext1!='jpg' && $imgext1!='jpeg' && $imgext1!='tif') {
return "ssv_valid_extensions";
}
} else {
return "ssv_valid_size_2";
}
} else {
return "ssv_valid_required";
}
if ($_FILES['doc7']!='') {
if ($_FILES['doc7']['size']<=7242880) {
$fileParts= explode('.', $_FILES['doc7']['name']);
$imgext1=strtolower( end($fileParts) );
if($imgext1!='tiff' && $imgext1!='pdf' && $imgext1!='jpg' && $imgext1!='jpeg' && $imgext1!='tif') {
return "ssv_valid_extensions";
}
} else {
return "ssv_valid_size_2";
}
} else {
return "ssv_valid_required";
}
if ($_FILES['doc9']['size']>0) {
if($_FILES['doc9']['size']<=7242880) {
$fileParts= explode('.', $_FILES['doc9']['name']);
$imgext1=strtolower( end($fileParts));
if($imgext1!='tiff' && $imgext1!='pdf' && $imgext1!='jpg' && $imgext1!='jpeg' && $imgext1!='tif') {
return "ssv_valid_extensions";
}
} else {
return "ssv_valid_size_2";
}
}
if ($_FILES['doc10']['size']>0) {
if($_FILES['doc10']['size']<=7242880) {
$fileParts= explode('.', $_FILES['doc10']['name']);
$imgext1=strtolower( end($fileParts) );
if($imgext1!='tiff' && $imgext1!='pdf' && $imgext1!='jpg' && $imgext1!='jpeg' && $imgext1!='tif') {
return "ssv_valid_extensions";
}
} else {
return "ssv_valid_size_2";
}
}
$validated_inputs = array(
"fullName" => $fullName,
"dob" => $dob,
"employer" => $employer,
"Salary" => $Salary,
"email" => $email,
"mobile" => $mobile,
"loanamount" => $loanamount,
//"loantype" => $loantype,
"workadrress" => $workadrress,
"id_name" => $id_name,
"id_mobile" => $id_mobile,
"id_relation" => $id_relation,
"id2_name" => $id2_name,
"id2_mobile" => $id2_mobile,
"id2_relation" => $id2_relation,
"id3_name" => $id3_name,
"id3_mobile" => $id3_mobile,
"id3_relation" => $id3_relation,
"house_no" => $house_no,
"street" => $street,
"area" => $area,
"emirate" => $emirate,
"city" => $city,
"district" => $district,
"doc1" => $_FILES['doc1'],
"doc2" => $_FILES['doc2'],
"doc3" => $_FILES['doc3'],
"doc4" => $_FILES['doc4'],
"doc5" => $_FILES['doc5'],
"doc6" => $_FILES['doc6'],
"doc7" => $_FILES['doc7'],
//"doc8" => $_FILES['doc8'],
"doc9" => $_FILES['doc9'],
"doc10" => $_FILES['doc10']
);
return $validated_inputs;
}
}
I have increased the
upload_max_filesize to 30 MB
memory_limit 265
post_max_size 30 MB
Any reason for that ?
Thanks.
Your webserver may have a limit set as well. In Nginx for example it is the client_max_body_size directive (docs). You should have this line in your server {} block:
client_max_body_size 30M;
In Apache you have LimitRequestBody 104800 (docs), though usually that is set to 2 GB by default.
You should be careful by changing the memory limit by the way. You may hit your server limits sooner then you wish for.
I have a post form on front end where users can post (post_type = product) from the form. As a part of it I have tried implementing few server side validations as in below code. The issue is that the validations are all working fine but the data is getting saved on form submission even when the validation fails.
Ideally the form submission should fail when there is a field validation failure.
I am not sure if $hasError = true is working or not, there might be a very simple logic I am missing which I am not getting. Any help regarding this?
Thanks in advance.
$postTitleError = '';
if (isset($_POST['submitted']) && isset($_POST['post_nonce_field']) && wp_verify_nonce($_POST['post_nonce_field'], 'post_nonce')) {
if (trim($_POST['postTitle']) === '') {
$postTitleError = 'msg 1';
$hasError = true;
}
if (trim($_POST['postCat1']) === '') {
$postTitleError = 'msg2';
$hasError = true;
}
if (trim($_POST['postPrice']) === '') {
$postTitleError = 'msg3';
$hasError = true;
}
if (trim($_POST['postTime']) === '') {
$postTitleError = 'msg4';
$hasError = true;
}
if (trim($_POST['postTimeMin']) === '') {
$postTitleError = 'msg5';
$hasError = true;
}
if (trim($_POST['postContent']) === '') {
$postTitleError = 'msg6';
$hasError = true;
}
<?php
//$postTitleError = '';
$resultArr = array();
$error_msg = false;
if (isset($_POST['submitted']) && isset($_POST['post_nonce_field']) && wp_verify_nonce($_POST['post_nonce_field'], 'post_nonce')) {
if (isset($_POST['postTitle']) && !empty($_POST["postTitle"])) {
//$postTitleError = 'msg 1';
//$hasError = true;
$postTitle=$_POST['postTitle'];
}
else
{
$resultArr['status'] = 'failure';
$resultArr['error_msg_postTitle']= "msg 1";
$error_msg = true;
}
if (isset($_POST['postCat1']) && !empty($_POST["postCat1"]) ) {
// $postTitleError = 'msg2';
// $hasError = true;
$postCat1=$_POST['postCat1'];
}
else
{
$resultArr['status'] = 'failure';
$resultArr['error_msg_postCat1']= "msg2";
$error_msg = true;
}
if (isset($_POST['postPrice']) && !empty($_POST["postPrice"]) ) {
// $postTitleError = 'msg3';
//$hasError = true;
$postPrice=$_POST['postPrice'];
}
else
{
$resultArr['status'] = 'failure';
$resultArr['error_msg_postPrice']= "msg3";
$error_msg = true;
}
if (isset($_POST['postTime']) && !empty($_POST["postTime"]) ) {
//$postTitleError = 'msg4';
//$hasError = true;
$postTime=$_POST['postTime'];
}
else
{
$resultArr['status'] = 'failure';
$resultArr['error_msg_postTime']= "msg4";
$error_msg = true;
}
if (isset($_POST['postTimeMin']) && !empty($_POST["postTimeMin"]) ) {
// $postTitleError = 'msg5';
// $hasError = true;
$postTimeMin=$_POST['postTimeMin'];
}
else
{
$resultArr['status'] = 'failure';
$resultArr['error_msg_postTimeMin']= "msg5";
$error_msg = true;
}
if (isset($_POST['postContent']) && !empty($_POST["postContent"]) ) {
//$postTitleError = 'msg6';
// $hasError = true;
$postContent=$_POST['postContent'];
}
else
{
$resultArr['status'] = 'failure';
$resultArr['error_msg_postContent']= "msg6";
$error_msg = true;
}
if($error_msg == false)
{
//here publish post code
}
else
{
//here Error message prine
}
?>
I am using opencart 2.0 and i have made custom registeration page in opencart.After successful registration i am redirecting to myaccount page but it will redirect to login page.
Can anybody help me in this to sort out the problem?
Here's my controller code :-
public function register() {
$this->load->language('checkout/checkout');
$json = array();
//if (!$json) {
$this->load->model('account/customer');
if ((utf8_strlen(trim($this->request->post['firstname'])) < 1) || (utf8_strlen(trim($this->request->post['firstname'])) > 32)) {
$json['error']['firstname'] = $this->language->get('error_firstname');
}
/*if ((utf8_strlen(trim($this->request->post['lastname'])) < 1) || (utf8_strlen(trim($this->request->post['lastname'])) > 32)) {
$json['error']['lastname'] = $this->language->get('error_lastname');
}*/
if ((utf8_strlen($this->request->post['email']) > 96) || !preg_match('/^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/', $this->request->post['email'])) {
$json['error']['email'] = $this->language->get('error_email');
}
if ($this->model_account_customer->getTotalCustomersByEmail($this->request->post['email'])) {
$json['error']['warning'] = $this->language->get('error_exists');
}
if ((utf8_strlen($this->request->post['telephone']) < 3) || (utf8_strlen($this->request->post['telephone']) > 32)) {
$json['error']['telephone'] = $this->language->get('error_telephone');
}
/*if ((utf8_strlen(trim($this->request->post['address_1'])) < 3) || (utf8_strlen(trim($this->request->post['address_1'])) > 128)) {
$json['error']['address_1'] = $this->language->get('error_address_1');
}
if ((utf8_strlen(trim($this->request->post['city'])) < 2) || (utf8_strlen(trim($this->request->post['city'])) > 128)) {
$json['error']['city'] = $this->language->get('error_city');
}
$this->load->model('localisation/country');
$country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']);
if ($country_info && $country_info['postcode_required'] && (utf8_strlen(trim($this->request->post['postcode'])) < 2 || utf8_strlen(trim($this->request->post['postcode'])) > 10)) {
$json['error']['postcode'] = $this->language->get('error_postcode');
}
if ($this->request->post['country_id'] == '') {
$json['error']['country'] = $this->language->get('error_country');
}
if (!isset($this->request->post['zone_id']) || $this->request->post['zone_id'] == '') {
$json['error']['zone'] = $this->language->get('error_zone');
}*/
if ((utf8_strlen($this->request->post['password']) < 4) || (utf8_strlen($this->request->post['password']) > 20)) {
$json['error']['password'] = $this->language->get('error_password');
}
if ($this->request->post['confirm'] != $this->request->post['password']) {
$json['error']['confirm'] = $this->language->get('error_confirm');
}
/*if ($this->config->get('config_account_id')) {
$this->load->model('catalog/information');
$information_info = $this->model_catalog_information->getInformation($this->config->get('config_account_id'));
if ($information_info && !isset($this->request->post['agree'])) {
$json['error']['warning'] = sprintf($this->language->get('error_agree'), $information_info['title']);
}
} */
// Customer Group
if (isset($this->request->post['customer_group_id']) && is_array($this->config->get('config_customer_group_display')) && in_array($this->request->post['customer_group_id'], $this->config->get('config_customer_group_display'))) {
$customer_group_id = $this->request->post['customer_group_id'];
} else {
$customer_group_id = $this->config->get('config_customer_group_id');
}
// Custom field validation
$this->load->model('account/custom_field');
$custom_fields = $this->model_account_custom_field->getCustomFields($customer_group_id);
foreach ($custom_fields as $custom_field) {
if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['location']][$custom_field['custom_field_id']])) {
$json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
}
}
//}
//if (!$json) {
$customer_id = $this->model_account_customer->addCustomer($this->request->post);
// Clear any previous login attempts for unregistered accounts.
$this->model_account_customer->deleteLoginAttempts($this->request->post['email']);
$this->session->data['account'] = 'register';
$this->load->model('account/customer_group');
$customer_group_info = $this->model_account_customer_group->getCustomerGroup($customer_group_id);
/*if ($customer_group_info && !$customer_group_info['approval']) {
$this->customer->login($this->request->post['email'], $this->request->post['password']);
// Default Payment Address
$this->load->model('account/address');
$this->session->data['payment_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
if (!empty($this->request->post['shipping_address'])) {
$this->session->data['shipping_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
}
} else {*/
$json['redirect'] = $this->url->link('account/account');
//}
unset($this->session->data['guest']);
unset($this->session->data['shipping_method']);
unset($this->session->data['shipping_methods']);
unset($this->session->data['payment_method']);
unset($this->session->data['payment_methods']);
// Add to activity log
$this->load->model('account/activity');
$activity_data = array(
'customer_id' => $customer_id,
'name' => $this->request->post['firstname'] . ' ' . $this->request->post['lastname']
);
$this->model_account_activity->addActivity('register', $activity_data);
//}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
i am validating three types of input(string,email,url):
string-validating:
if ($_POST['string'] != "") {
$string = filter_var($_POST['string'], FILTER_SANITIZE_STRING);
if ($string != "") {
// valid
} else {
// not valid
}
} else {
// empty
}
email-validating:
if ($_POST['email'] != "") {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
// valid
} else {
// not valid
} else {
// empty
}
url-validating:
if ($_POST['url'] != "") {
$url = filter_var($_POST['url'], FILTER_SANITIZE_URL);
if (filter_var($url, FILTER_VALIDATE_URL)) {
// valid
} else {
// not valid
}
} else {
// empty
}
After doing this checks i use PDO - Prepared statements to inserate in database.
You think this is secure enough or did i missed some points?
Hope for your answers, thanks and greetings!
i have a simple contact form that i have included a honeypot input field.
i would like the form to redirect to a webpage if the field is filled out.
i tried the below code, but it is giving me an error:
the AJAX request failed!
so i know i have done something wrong. i'm sure it is simple.
thanks
the php code:
if(!empty($_POST["e-mail"])) header('Location: blankman.html');exit;
the form input:
<input type="text" name="e-mail" id="e-mail"/>
here is the full php code:
<?php
if(!empty($_POST["e-mail"])) header('Location: blankman.html');exit;
// Clean up the input values
foreach($_POST as $key => $value) {
if(ini_get('magic_quotes_gpc'))
$_POST[$key] = stripslashes($_POST[$key]);
$_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}
// Assign the input values to variables for easy reference
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
// Test input values for errors
$errors = array();
if(strlen($name) < 2) {
if(!$name) {
$errors[] = "missing your name";
} else {
$errors[] = "your name must be 2 characters";
}
}
if(!$email) {
$errors[] = "missing your email";
} else if(!validEmail($email)) {
$errors[] = "you must enter a valid email";
}
if(strlen($message) < 3) {
if(!$message) {
$errors[] = "missing your message";
} else {
$errors[] = "oops! your message is not long enough";
}
}
if($errors) {
// Output errors and die with a failure message
$errortext = "";
foreach($errors as $error) {
$errortext .= "<li>".$error."</li>";
}
$response = array(
"success" => false,
"content" => "<span class='failure'><ul>". $errortext ."</ul></span>"
);
die(json_encode($response));
}
// Send the email *********** enter your email address and message info ***
$to = "myemail#myemail.com";
$subject = "Website message from: $name";
$message = "From:\n$name\n\nEmail:\n$email\n\nMessage:\n$message";
$headers = "From: $email";
mail($to, $subject, $message, $headers);
// Die with a success message
$response = array(
"success" => true,
"content" => "<span class='success'><li>Thank you! Your message has been sent :).</li></span>"
);
die(json_encode($response));
// A function that checks to see if
// an email is valid
function validEmail($email)
{
$isValid = true;
$atIndex = strrpos($email, "#");
if (is_bool($atIndex) && !$atIndex)
{
$isValid = false;
}
else
{
$domain = substr($email, $atIndex+1);
$local = substr($email, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64)
{
// local part length exceeded
$isValid = false;
}
else if ($domainLen < 1 || $domainLen > 255)
{
// domain part length exceeded
$isValid = false;
}
else if ($local[0] == '.' || $local[$localLen-1] == '.')
{
// local part starts or ends with '.'
$isValid = false;
}
else if (preg_match('/\\.\\./', $local))
{
// local part has two consecutive dots
$isValid = false;
}
else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
{
// character not valid in domain part
$isValid = false;
}
else if (preg_match('/\\.\\./', $domain))
{
// domain part has two consecutive dots
$isValid = false;
}
else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
str_replace("\\\\","",$local)))
{
// character not valid in local part unless
// local part is quoted
if (!preg_match('/^"(\\\\"|[^"])+"$/',
str_replace("\\\\","",$local)))
{
$isValid = false;
}
}
if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
{
// domain not found in DNS
$isValid = false;
}
}
return $isValid;
}
?>
here is the javascript:
<script>
$(document).ready(function () {
$("#contactform").submit(function (e) {
e.preventDefault();
var t = $(this).attr("action");
var n = $(this).serialize();
$.post(t, n, null, "json").done(function (e) {
if (e.success) {
$("#success").html(e.content);
$("#contactform,#error").hide()
} else {
$("#error").html(e.content)
}
}).fail(function () {
alert("The AJAX request failed!")
})
})
})
</script>
Do not show whatever different behavior if honeypot is filled. This way you are screaming to a spamer with BIG RED LETTERS: "Here is a honeypot! Investigate and write a workaround!"
Always respond to a spam request EXACTLY the same way as to a regular one:
if(!empty($_POST["e-mail"])) {
$response = array(
"success" => true,
"content" => "<span class='success'><li>Thank you! Your message has been sent :).</li></span>"
);
die(json_encode($response));
}