Using Flash Messages Everywhere

Let's look at how we can add a flash message on any page we want.

Creating a new flash message script

Due to the nature of these messages “flashing” in front of the user and then disappearing, these messages are called flash messages. It is a handy feature that we want to have on pretty much every page. Wherever we add a flash message to the $_SESSION variable, and wherever the browser gets redirected to, we should always see the message on that page. In Project Structure, we will solve this problem with templates. For now, let’s solve it with a slightly simpler trick.

First, we move the code for showing the flash message to a new script, called _flash_message.php, inside the public/ directory:

Get hands-on with 1200+ tech skills courses.