[PHP] Implementation of flash messages

Sep 6, 2020 PHP

I will summarize what I learned about PHP as a memorandum.

Now that we have implemented the flash message in our portfolio, we will record how it is implemented.

#Flash message implementation As a whole flow, if there is an empty form, the corresponding error message is stored, The flow is to display an error message on the login screen.

First, check if the form is empty and process it. Now you can store the value in the error message when the form is empty.


//login_check.php (file in the login process)

if (empty ($ staff_code)) {
$ error_messages ['code'] = "Enter code";
  }

if (empty ($ staff_pass)) {
$ error_messages ['pass'] = "Enter password";
  }

As an aside, if you hash the value with md5 etc., the form will contain the value even if it is empty and you will not be able to process it with empty.


// $ staff_pass is empty

$ staff_pass = md5 ($ staff_pass);

// $ staff_pass has a value

if (empty ($ staff_pass)) {
$ error_messages ['pass'] = "Enter password";
  }

// The above process is not executed

Once the value is stored in error_masseages, it’s time to store the value in the session. At this time, if you want to change the color etc. depending on the type of flash message, also define $ flash_type.


//login_check.php

if (isset ($ _SESSION ['flash'])) {
$ flash_messages = $ _SESSION ['flash'] ['message'];
$ flash_type = $ _SESSION ['flash'] ['type'];
  }
  unset ($ _SESSION ['flash']);

Finally, a flash message will be displayed on the login screen.


//login_form.php

    <? php if (isset ($ flash_messages)):?>
      <? php foreach ((array) $ flash_messages as $ message):?>
        <p class = "flash_message <? = $ flash_type?>"> <? = $ Message?> </ P>
      <? php endforeach?>
    <? php endif?>

Implementation screen

image.pngimage.png