Issue validating data of a contact form

Hello, I’m trying to validate a contact form, first I check if the first name = last name then I throw an error which works fine, then I verify the email address if the domain of that address is blacklisted, I have an array with all blacklisted domains.
So here is my test:
First name = last name + blacklist email address domain name = OK, it will redirect the user back to the homepage
First name is not equal to the Last name + Blacklist email address domain name = OK, I get redirected to the home page
First name is not equal to the last name + Not Blacklisted email address = NOT OK, it will throw an error : Email is a required field.

Here is my code:

 //Validate data
            if ($firstname != '') {
                if (!Validate::isName($firstname)) {
                    $this->errors[] = $this->module->l('First name is not valid.', 'createticket');
                }
            } else {
                $this->errors[] = $this->module->l('First name is required field.', 'createticket');
            }

            if ($lastname != '') {
                if (!Validate::isName($lastname)) {
                    $this->errors[] = $this->module->l('Last name is not valid.', 'createticket');
                }
				else if ($firstname == $lastname ) {
				$this->errors[] = $this->module->l('First name and last name matches, doesn\t make sense!', 'createticket');}
            } else {
                $this->errors[] = $this->module->l('Last name is required field.', 'createticket');
            }
			
			if ($email = (in_array(substr($email, strrpos($email, '@') + 1), $RefusedDomains))) {
			header('Location: https://google.com');}
			
            if ($email != '') {
                if (!Validate::isEmail($email)) {
                    $this->errors[] = $this->module->l('Email is not valid.', 'createticket');
                }
            } else {
                $this->errors[] = $this->module->l('Email is required field.', 'createticket');
            }
if ($email = (in_array(substr($email, strrpos($email, '@') + 1), $RefusedDomains))) {

Here you are overriding the value of $email with true or false.
You can leave only the condition:

if (in_array(substr($email, strrpos($email, '@') + 1), $RefusedDomains)) {

Thank you dear @chack1172 However doing that when I enter a blacklisted email address domain name, the ticket is still created and I receive the email, it should redirect the user to a web page

After the header location that changes the page, add exit;. Also if you change the page the code continue to execute if you don’t stop it.

1 Like

Thank you so much, that did the work

Sponsor our Newsletter | Privacy Policy | Terms of Service