Php best way to show notification after redirect

I need to display a notification message after the header redirect page. I am using this function:

function Redirect($url) {
if(!headers_sent()) {
    //If headers not sent yet... then do php redirect
    header('Location: '.$url);
    exit;
} else {
    //If headers are sent... do javascript redirect... if javascript disabled, do html redirect.
    echo '<script type="text/javascript">';
    echo 'window.location.href="'.$url.'";';
    echo '</script>';
    echo '<noscript>';
    echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
    echo '</noscript>';
    exit;
}
   }

I am inserting an error in php SESSIONas follows:

session_start();
$_SESSION['errors'] = array();

...

array_push($_SESSION['errors'], "<span style = 'color:green;'>Success!</span>");
Redirect("somepage.php"); // OR header("Location: somepage.php");

And to display the notification:

if(isset($_SESSION['errors']) && count($_SESSION['errors']) > 0) {
    foreach($_SESSION['errors'] as $k => $v)
        echo($v . "<br/>");
    unset($_SESSION['errors']);
}

Send notice in SESSIONsafe and good? if not, what is the best way to display a notification after the header redirects ?!

+4
source share
2 answers

I wrote a library only for this type of project https://github.com/tamtamchik/simple-flash .

After installation, you can do this:

// put message not session
flash('Some error message', 'error');

// print it after redirect
echo flash()->display(); 

Bootstrap .

Example

+1

, . -, . php cookie session. Cookie , . :

if ($oNotification->hasNotification('key'))
{
$oNotification->showNotification('key');
}

, Yii Framework ""

0

All Articles