0

PHP Controller:

$this->form_validation->set_rules('phone', $this->language->get_text('phone', 'global'), 'max_length[0]');

I need to put an IF condition for the above line code to return this:

if (The_Above_Line_Code is NOT empty)
{
$this->output->set_status_header(400);
exit;
}

So, if the input field don't contain characters it's OK and the contact form to work properly, but if contain characters, then should return a blank page (set_status_header(400)).

PS: It's a way to combat spam in contact form.

Stan
  • 37
  • 8

1 Answers1

1
$this->form_validation->set_rules('phone', $this->language->get_text('phone', 'global'), 'max_length[0]|numeric');

If it's not a number then form validation fails. I don't see the logic in serving a 400.

https://www.codeigniter.com/userguide3/libraries/form_validation.html?highlight=form%20validate#rule-reference


Update

After understanding your reasoning better you can simply do this:

if (!empty($this->input->post('phone'))) {
    show_404(); // sets header + exits
}

You can even use show_404() (CI function) as a way to log the error: show_404('bot detected', true);.

Alex
  • 9,215
  • 8
  • 39
  • 82
  • Alex, this input field is not a phone field. It is a "blank and hidden" field ( https://stackoverflow.com/questions/3622433/how-effective-is-the-honeypot-technique-against-spam ). What i've implement WORKS pretty good, but i wish to implement what i wrote above. When a bot put any type of string in that field to return 400 error code not the reload the page contact. This field is hidden through CSS. – Stan Aug 06 '18 at 17:07
  • ahhh ok makes alot more sense. what threw me off was the form validation. – Alex Aug 06 '18 at 18:48
  • Thanks for the updated! That is what i need. Cheers! – Stan Aug 06 '18 at 20:15