FriconiX
Free collection of beautiful vector icons for your web pages.

Part 8. Error Handling

Redirection

We will improve the error handling so that we can display a more user-friendly message. In the errorManager($message, $url=NULL) function, change the code so that the user is redirected instantaneously in no more after 5 seconds.

If the url passed in parameter is null, the user will be redirected to the page default home page.

Test your changes by directly loading the new.php page. You should be redirected to the home page of the site.

Sessions

In the errorManager function, instead of displaying the error message directly, we'll store it in a session variable that we'll display later.

Modify the errorManager function so that the message is stored in the $_SESSION['errorMessage'] variable.

Add session initialization to all of your PHP scripts except in the library.

Temporarily display the contents of the session variable on the home page and check that the message is recorded when you load directly the new.php script:

Error message displayed on the home page

Error display

In the lib\lib.php library, add a displayError() function. This function tests whether the $_SESSION['errorMessage] variable is empty or not.

If the variable contains a message, it displays the error message in a Alert Bootstrap.

In the home page (index.php) and in the shopping list page (list/index.php), add a call to the function before the title is displayed. Load the scripts new.php and list/add.php, the message should appear in accordance with the following illustration. You can add an icon before the text :

Error message displayed in an Alert Bootstrap

Clear the error message

Based on the example of the Bootstrap documentation, add a cross so that the user can delete the message:

A button to delete the error message

Make sure that when you click on the cross, the message disappears.

Erase the error (really)

Reload the page, what do you see? The error message comes back because it is always stored in the session variable.

Modify the displayError() function so that the session variable $_SESSION['errorMessage'] is deleted after the display. Test your change.

No more mistakes?

Create a shopping list and enter a few items. Enter a new item containing one or more HTML tags, for example :

What do you see?

Fix this problem with the htmlspecialchars function.

From the home page, create a new list called: "<!--".

Correct this problem using the same method as for item names.

See also


Last update : 02/03/2020