php 在 CodeIgniter 中发送数据和重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1837467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending data along with a redirect in CodeIgniter
提问by Alex Mcp
I have a simple C (of CRUD) function, and I'd like to send a message (error or success) along with my redirect from the "insert" function I have written up. Is there a way to adhere a POST field with a redirect?
我有一个简单的 C(CRUD)函数,我想发送一条消息(错误或成功)以及我编写的“插入”函数的重定向。有没有办法通过重定向来遵守 POST 字段?
In pseudo code I have:
在伪代码中,我有:
function view_all{
//set up some initial variables
$this->load->view(viewing_page, $data)
}
function insert{
if ($this->db->insert(my_table, $_POST)){
$message = "All's well";
}
else {
$message = "whoops!";
}
redirect(view_all);
}
So the viewing_page ideally would have something like
所以 view_page 理想情况下会有类似的东西
if (isset($message)){
echo $message
}
So on the first time through, I don't see any message, and when/if there's an insert, it pops up the same page with the message. Thanks!
所以在第一次通过时,我没有看到任何消息,并且当/如果有插入,它会弹出与消息相同的页面。谢谢!
回答by Tim Lytle
I believe redirectuses header(). If so, I don't believe you can send data along with a location header. You could accomplish the same thing using session vars or (not as good) appending a query string to the location URL.
我相信redirect使用header(). 如果是这样,我不相信您可以将数据与位置标头一起发送。您可以使用会话变量或(不太好)将查询字符串附加到位置 URL 来完成相同的事情。
For an 'accepted' way to do this in CodeIgniter look a little more than halfway down the session classdocumentation page.
对于在 CodeIgniter 中执行此操作的“可接受”方式,请查看会话类文档页面的一半以上。
CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared. These can be very useful, and are typically used for informational or status messages (for example: "record 2 deleted").
CodeIgniter 支持“flashdata”,或仅可用于下一个服务器请求的会话数据,然后会自动清除。这些可能非常有用,通常用于信息或状态消息(例如:“记录 2 已删除”)。
This (now deleted - here's an archived version) post on flash messagescovers both the query string and the session var method.
这个(现已删除 - 这是一个存档版本)关于 flash 消息的帖子涵盖了查询字符串和会话 var 方法。
Update: To summarize the now deleted post, it showed both urlencoding a message and appending as a query string (example from post):
更新:总结现在已删除的帖子,它显示了对消息进行 urlencoding 并附加为查询字符串(来自帖子的示例):
header('Location: http://www.example.com/index.php?message='.urlencode($message));
And setting a 'flash' variable using two frameworks (example from post):
并使用两个框架设置“flash”变量(帖子中的示例):
//Zend Framework
$flashMessenger = $this->_helper->FlashMessenger;
$flashMessenger->setNamespace('actionErrors');
$flashMessenger->addMessage($message);
//CakePHP
$this->Session->setFlash('Your post has been saved.');
$this->redirect('/news/index');
Of course you can do roughly the same thing using $_SESSIONdirectly (my example):
当然,您可以$_SESSION直接使用(我的示例)执行大致相同的操作:
//first request
$_SESSION['flash'] = 'This is a simple flash message.';
//next request
$flash = $_SESSION['flash'];
unset($_SESSION['flash']); //flash is one time only
回答by Shaolin
You can use Flashdata in the CI Session Class. This is what's said in the document:
您可以在 CI会话类中使用 Flashdata 。这是文档中所说的:
CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared. These can be very useful, and are typically used for informational or status messages (for example: "record 2 deleted").
CodeIgniter 支持“flashdata”,或仅可用于下一个服务器请求的会话数据,然后会自动清除。这些可能非常有用,通常用于信息或状态消息(例如:“记录 2 已删除”)。
Note: Flash variables are prefaced with "flash_" so avoid this prefix in your own session names.
注意:Flash 变量以“flash_”开头,因此请避免在您自己的会话名称中使用此前缀。
To add flashdata:
添加闪存数据:
$this->session->set_flashdata('item', 'value');
You can also pass an array to set_flashdata(), in the same manner as set_userdata().
您还可以set_flashdata()以与 相同的方式将数组传递给set_userdata()。
To read a flashdata variable:
读取 flashdata 变量:
$this->session->flashdata('item');
If you find that you need to preserve a flashdata variable through an additional request, you can do so using the keep_flashdata()function.
如果您发现需要通过附加请求保留 flashdata 变量,则可以使用该keep_flashdata()函数来实现。
$this->session->keep_flashdata('item');
回答by Tom Lagier
I would like to point out that CodeIgniter destroys the current session on logout. This makes it more difficult to pass along a message along the lines of "you have logged in/out" as you cannot use flash or session storage. If you need to pass a message when transitioning between logged in and out states, I suggest using memcached. Other options (mentioned above) are using URL query stringsand setting cookies.
我想指出 CodeIgniter 会在注销时破坏当前会话。由于您无法使用闪存或会话存储,这使得沿着“您已登录/退出”的方式传递消息变得更加困难。如果您需要在登录和退出状态之间转换时传递消息,我建议使用memcached。其他选项(如上所述)是使用 URL 查询字符串和设置 cookie。

