php重定向后显示通知的最佳方式
发布时间:2020-12-13 22:54:06 所属栏目: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
我需要在标题重定向页面后显示通知消息.我使用此功能重定向页面:
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中发出通知是安全又好的方法吗?如果没有,在标题重定向后显示通知的最佳方法是什么?! 解决方法
我为这类项目编写了一个库
https://github.com/tamtamchik/simple-flash.
安装完成后,您可以执行以下操作: // put message not session flash('Some error message','error'); // print it after redirect echo flash()->display(); 它将生成Bootstrap友好警报消息. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |