What is the 'correct' way to gather $_POST input from my form via CodeIgniter/PHP?
This is more of a theoretical question than a specific one.
I have a form set up with CodeIgniter's Form Validation class. I have some rules being run, for example:
$this->form_validation->set_rules('address_line_1', 'Address Line 1', 'required|xss_clean|trim');
I eve开发者_高级运维ntually want to put the address_line_1
data into my Database. This is where I'm a little confused. It seems there are several ways of fetching $_POST
data from within CodeIgniter:
$address = $_POST['address_line_1'];
$address = $this->input->post('address_line_1');
$address = $this->form_validation->set_value('address_line_1');
$address = set_value('address_line_1);
So which way is the 'correct' way?
Whilst I'm sure several of these assumptions are wrong, I've been led to believe that...
$_POST
is unsanitised by CodeIgniter's security (I'm confident about this one)$this->input->post()
will sanitise the data (to a certain extent), but won't have applied any Form Validation prepping rules$this->form_validation->set_value()
is the same asset_value()
, but......
set_value()
is intended to re-populate form inputs via theirvalue=""
element.
Which of my assumptions are correct and which are wrong? And what is the way I should be pulling through $_POST
data when I'm prepping it with Form Validation? The Form Validation documentation is ambiguous when it comes to this. None of the examples ever show it actually passing input data onto a model, for example.
Thanks!
Jack
They are all different, or they wouldn't all exist.
$_POST['foo']
is unprotected and raw output. BAD. Don't touch. etc.$this->input->post('foo')
escaped and XSSified input. Defaults toFALSE
instead of erroring.$this->form_validation->set_value()
this will take the validated output, which may have been modified through the validation rules. For example, if you add "trim" as a validation rule, the validated content will be trimmed.set_value()
just an alias of the method above. People don't like to use $this in their views.
This is all in the documentation.
精彩评论