• We just launched and are currently in beta. Join us as we build and grow the community.

How to Create a Flash Message in PHP using Session Tutorial

bboy1

Rookie
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
77
Likes
36
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial, you will learn how to create a Flash Messages in PHP Language using PHP Session. The Flash Message that I am talking about is like the CodeIgniter's Flashdata Function. It allows the developer to store a temporary messages or data that can be access on the next request and after use.

How do Flash Messages Works?

The Flash Messages works something like the CodeIgniter's Flashdata Function where the developer can use it to store the process responses, error responses, messages, and other string data. This data will be stored using the PHP Session and when the data has called, the Flash Message stored in the session will b automatically removed.

Here, we will create a simple PHP application that has multiple buttons that triggers to save a Flash Message. After the successful saving of the flash message, the system will be redirected back to the page referrer and displays the message.

Getting Started

Download XAMPP as your local web server to run our PHP Script. After Installing the virtual server, open the XAMPP's Control Panel and start the Apache Server.

Download Bootstrap v5 for the interface design of the application that we'll be creating. After that move the library directory to the folder where you will store the source code on your end.

Creating The Interface

Below is the script of our Main Page which contains the elements codes for displaying flash messages and other displays. save this file as index.php.

  1. <?php

    require_once

    (

    'function.php'

    )

    ?>
  2. <!DOCTYPE html>
  3. <html lang="en">

  4. <head>
  5. <meta charset="UTF-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>Flash Messages</title>
  9. <link rel="stylesheet" href="./Font-Awesome-master/css/all.min.css">
  10. <link rel="stylesheet" href="./css/bootstrap.min.css">
  11. <script src="./js/bootstrap.min.js"></script>
  12. <style>
  13. :root {
  14. --bs-success-rgb: 71, 222, 152 !important;
  15. }

  16. html,
  17. body {
  18. height: 100%;
  19. width: 100%;
  20. font-family: Apple Chancery, cursive;
  21. }

  22. .btn-info.text-light:hover,
  23. .btn-info.text-light:focus {
  24. background: #000;
  25. }
  26. </style>
  27. </head>

  28. <body class="bg-light">
  29. <nav class="navbar navbar-expand-lg navbar-dark bg-dark bg-gradient" id="topNavBar">
  30. <div class="container">
  31. <a class="navbar-brand" href="https://sourcecodester.com">
  32. Sourcecodester
  33. </a>

  34. <div>
  35. <b class="text-light">Flash Messages</b>
  36. </div>
  37. </div>
  38. </nav>
  39. <div class="container py-5" id="page-container">
  40. <h4 class="text-center"><b>Flash Messages Like CodeIgniter's Flashdata Functions.</b></h4>

  41. <!-- Displaying Flash Data if exist -->
  42. <?php

    if

    (

    isset

    (

    $_SESSION

    [

    '_flashdata'

    ]

    )

    )

    :

    ?>
  43. <!-- Looping All Flash messages -->
  44. <?php

    foreach

    (

    $_SESSION

    [

    '_flashdata'

    ]

    as

    $key

    =>

    $val

    )

    :

    ?>
  45. <div class="my-2 px-3 py2 alert alert-<?=

    $key

    ?>

    ">
  46. <div class="d-flex align-items-center">
  47. <div class="col-11">
  48. <!-- Displaying the Flash Message -->
  49. <p><?=

    get_flashdata(

    $key

    )

    ?>

    </p>
  50. </div>
  51. <div class="col-1 text-end">
  52. <button class="btn-close" type="button" onclick="this.closest('.alert').remove()"></button>
  53. </div>
  54. </div>
  55. </div>
  56. <?php

    endforeach

    ;

    ?>
  57. <!-- Looping All Flash messages -->
  58. <?php

    endif

    ;

    ?>
  59. <!-- Displaying Flash Data if exist -->


  60. <div class="col-lg-12 pt-5">
  61. <div class="row">
  62. <div class="col-md-3 px-2">
  63. <a href="set_messages.php?msg=info" class="btn btn-lg rounded-pill btn-pill btn-info">Show an Info Message</a>
  64. </div>
  65. <div class="col-md-3 px-2">
  66. <a href="set_messages.php?msg=success" class="btn btn-lg rounded-pill btn-pill btn-success">Show a Success Message</a>
  67. </div>
  68. <div class="col-md-3 px-2">
  69. <a href="set_messages.php?msg=error" class="btn btn-lg rounded-pill btn-pill btn-danger">Show an Error Message</a>
  70. </div>
  71. <div class="col-md-3 px-2">
  72. <a href="set_messages.php?msg=multiple" class="btn btn-lg rounded-pill btn-pill btn-default border">Show a Multiple Message</a>
  73. </div>
  74. </div>
  75. </div>
  76. </div>


  77. </body>

  78. </html>

Creating The Main Function

Next, we will be creating the PHP file that contains the function of storing the flash messages, getting the data, and deleting the flash message. Save this file as function.php.

  1. <?php
  2. if

    (

    session_status(

    )

    ===

    PHP_SESSION_NONE)
  3. session_start

    (

    )

    ;


  4. // Sets a flash message data function
  5. function

    set_flashdata(

    $key

    =

    ""

    ,

    $value

    =

    ""

    )

    {
  6. if

    (

    !

    empty

    (

    $key

    )

    &&

    !

    empty

    (

    $value

    )

    )

    {
  7. $_SESSION

    [

    '_flashdata'

    ]

    [

    $key

    ]

    =

    $value

    ;
  8. return

    true

    ;
  9. }
  10. return

    false

    ;
  11. }

  12. // Getting the flashdata
  13. function

    get_flashdata(

    $key

    =

    ""

    )

    {
  14. if

    (

    !

    empty

    (

    $key

    )

    &&

    isset

    (

    $_SESSION

    [

    '_flashdata'

    ]

    [

    $key

    ]

    )

    )

    {
  15. $data

    =

    $_SESSION

    [

    '_flashdata'

    ]

    [

    $key

    ]

    ;
  16. unset

    (

    $_SESSION

    [

    '_flashdata'

    ]

    [

    $key

    ]

    )

    ;
  17. return

    $data

    ;
  18. }

    else

    {
  19. echo

    "<script> alert(' Flash Message \'{$key}

    \' is not defined. ')</script>"

    ;
  20. }
  21. }

Creating the Process File

The script below is the code is the sample PHP File that stores the flash messages and redirects to the Main Page. Save this file as set_messages.php.

  1. <?php

    require_once

    (

    'function.php'

    )

    ?>
  2. <?php
  3. if

    (

    !

    isset

    (

    $_GET

    [

    'msg'

    ]

    )

    )

    {
  4. echo

    "<script> alert('No message to set.'); location.href='./'</script>"

    ;
  5. }

  6. $msg

    =

    $_GET

    [

    'msg'

    ]

    ;

  7. switch

    (

    $msg

    )

    {
  8. case

    'info'

    ;
  9. set_flashdata(

    'info'

    ,

    "<i class='fa fa-info-circle'></i> This is a Sample Info Message."

    )

    ;
  10. break

    ;
  11. case

    'success'

    ;
  12. set_flashdata(

    'success'

    ,

    "<i class='fa fa-check'></i> This is a Sample Success Message."

    )

    ;
  13. break

    ;
  14. case

    'error'

    ;
  15. set_flashdata(

    'danger'

    ,

    "<i class='fa fa-exclamation-triangle'></i> This is a Sample Error Message."

    )

    ;
  16. break

    ;
  17. case

    'multiple'

    ;
  18. set_flashdata(

    'info'

    ,

    "<i class='fa fa-info-circle'></i> This is a Sample Info Message."

    )

    ;
  19. set_flashdata(

    'success'

    ,

    "<i class='fa fa-check'></i> This is a Sample Success Message."

    )

    ;
  20. set_flashdata(

    'danger'

    ,

    "<i class='fa fa-exclamation-triangle'></i> This is a Sample Error Message."

    )

    ;
  21. break

    ;
  22. default

    :
  23. set_flashdata(

    'danger'

    ,

    "Undefined Flash Message."

    )

    ;
  24. break

    ;
  25. }
  26. header

    (

    'location:./'

    )

    ;

And here's the sample snapshot of the application.

flash_messages.png


DEMO VIDEO

That's it! You can now test the application in your browser and see if we have met our goal on this tutorial. If ever you have encountered any errors, please review your source code by differentiating it from the source code I provided above. You can also test the working source code I created for this tutorial. You can download the source code zip file below this article.

That is the end of this tutorial. I hope you'll find this Flash Messages in PHP Tutorial useful for your current and future projects.

Happy Coding :)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top