ESC Array in $_POST from a form

While esc() is often used in this way, it is actually not really correct usage. You should use validators to make sure you get the correct input in your forms and discard incorrect input. Then escape data when you output it in templates etc.

If you do it like this, you can store your $_POST array in the session without further ado.

If you want to apply some cleaning up, e.g. trim whitespace whitespace, you have to do this on the individual $_POST items:

$data = [
   'name' => trim($_POST['name']),
   'email' => trim($_POST['email'])
];

Note that this is only an example, it shouldn’t be necessary if you apply proper validators that prevent receiving such data in the first place. Have a look at Kirby’s validator class.

Then store the $data array in the session:

 $_SESSION['registrationdata'] = $data;
1 Like