Skip to content
PHP Logo

How can the PHP script be modified to display the notification of a new message only once for the user?

To display the notification of a new message only once for the user, you can store a flag in the user's session to track whether the notification has been displayed. When the notification is displayed, set the flag in the session to true. Check this flag before displaying the notification to ensure it is only shown once.

php
session_start();

if (!isset($_SESSION['messageDisplayed'])) {
    echo "New message notification";
    $_SESSION['messageDisplayed'] = true;
}