我需要在头部重定向页面后显示通知消息。我使用以下函数进行页面重定向:
我在php的
将通知放在
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;
}
}
我在php的
SESSION
中插入错误,具体操作如下:session_start();
$_SESSION['errors'] = array();
...
array_push($_SESSION['errors'], "<span style = 'color:green;'>Success!</span>");
Redirect("somepage.php"); // OR header("Location: somepage.php");
对于显示通知:
if(isset($_SESSION['errors']) && count($_SESSION['errors']) > 0) {
foreach($_SESSION['errors'] as $k => $v)
echo($v . "<br/>");
unset($_SESSION['errors']);
}
将通知放在
SESSION
中是一种安全而好的方式吗?如果不是,那么在重定向后显示通知的最佳方式是什么?!