codeigniter 就像核心 php 中的 flashdata [英] codeigniter like flashdata in core php
问题描述
有什么方法可以像在 codeigniter 中一样创建 Flash 会话数据,
我想在核心 php 中创建它.
is there any way to create flash session data like in codeigniter,
i want to create it in core php.
我不想使用 GET 方法,使用 url 传递变量会使我的应用程序出现问题.
那么,我该怎么做?
I don't want to use GET method, passing variable with url makes problem in my application.
so, how can i do this?
推荐答案
使用 PHP 会话创建 Flash 消息类非常容易.
Its pretty easy to create a flash message class with PHP sessions.
class FlashMessage {
public static function render() {
if (!isset($_SESSION['messages'])) {
return null;
}
$messages = $_SESSION['messages'];
unset($_SESSION['messages']);
return implode('<br/>', $messages);
}
public static function add($message) {
if (!isset($_SESSION['messages'])) {
$_SESSION['messages'] = array();
}
$_SESSION['messages'][] = $message;
}
}
确保您先调用 session_start()
.然后你可以使用 FlashMessage::add('...');
Make sure you are calling session_start()
first. Then you can add messages using FlashMessage::add('...');
然后,如果您重定向,您可以在下次渲染页面时渲染消息echo FlashMessage::render()
.这也将清除消息.
Then if you redirect, you can render the messages next time you render a page echo FlashMessage::render()
. Which will also clear the messages.
参见 http://php.net/manual/en/features.sessions.php
这篇关于codeigniter 就像核心 php 中的 flashdata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!