PHP Error Handling – Best Way to Pass Errors Between PHP Pages

error handlingoptimizationPHP

I had some ideas, but which one is the best (and why), and is it other way(s) to do this?

I thought about :
– Sessions
– GET Params, but if there is a lot of errors(for example a register form) it could be ugly.
– POST Params, but it needs a form with a JS redirection.

Objectively what is the best solution?

Best Answer

The de facto way to do this in PHP is using something called flash data, which uses sessions. In your validate.php you set the error message(s) in a session variable, then on your page.php you read from the session variable and delete it from the session (so that it doesn't keep showing up on that form).

This is used in most frameworks, for example Laravel and CodeIgniter.

Here's a Stack Overflow answer that shows some basic code in PHP. Or there are standalone libraries to help you such as PHP Flash Messages.