Search code examples
phpurlencodeimplode

Need best way to send error messages from One Webpage to another


What is the best way to send your messages like error messages from one php page to other php page. I do not want to use implode function, also i do not want messages to be displayed in address bar. Using this code $pageurl.= '?errors[]=' . implode('&errors[]=', array_map('urlencode', $errors)); My error messages generated by entering incorrect information by user got displayed in address bar, which is something i do not want. Kindly help.


Solution

  • Use session data. It is stored on the server and kept between page loads.

    Page that determines errors:

    <?php
    session_start():
    // something happens here to cause errors
    $_SESSION['my_error'] = array(
        'Field 1 is incorrect',
        'Field 3 is incorrect'
    );
    // whatever happens here to send user to next page
    

    Page that displays errors:

    <?php
    session_start();
    // check for set errors
    if (isset($_SESSION['my_error']) && !empty($_SESSION['my_error']))
    {
        foreach ($_SESSION['my_error'] as $error)
        {
            echo $error.'<br>';
        }
        // unset them if not needed anymore
        unset($_SESSION['my_error'];
    }