Codeigniter会话挂起 [英] Codeigniter session hanging

查看:125
本文介绍了Codeigniter会话挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用codeigniter为我们的内部intranet。

So I'm using codeigniter for our internal intranet.

通常在进行数据库选择,更新,删除等操作时,我使用ajax并使用jquery更新页面而不重定向。

Usually when doing database selections, updates, deletes etc, I use ajax, and use jquery to update the page without redirecting.

我需要实际提交一个表单,而不是依赖于ajax。一旦数据库操作完成,我使用codeigniters会话设置一些会话数据,然后将用户重定向回概述页面,它将从会话中检索信息并将其显示为成功消息。

I have a need to actually submit a form rather than relying on ajax. Once the database actions have been done, I set some session data using codeigniters sessions, then redirect the user back to the overview page, where it will retrieve information from the session and display it as a 'success' message.

我可以设置数据很好:

$this->session->set_userdata('msg_title', 'Success');
$this->session->set_userdata('msg_content', 'Some success message here');

我也知道我应该使用 flashdata

当我使用flashdata设置数据时,或者当我尝试使用 unset_userdata取消设置userdata时

When I set data using flashdata, or when I try to unset userdata using unset_userdata(), the browser hangs.

我知道这两个函数,因为当我删除 set_flashdata() unset_userdata()行,脚本工作正常。

I know its these two functions, because when I remove the set_flashdata() or unset_userdata() lines, the script works fine.

没有错误,错误日志上没有条目,控制台日志中没有任何内容。同一服务器上的其他网站(子域)工作正常。只有当我使用这些功能,它似乎挂了浏览器。

There are no errors, no entries on the error log, nothing in the console log. Other websites on the same server (subdomains) work fine. Its only when I used these functions, it seems to hang the browser.

我使用Chrome 32.0.1700.107,我也测试了它在IE8,遇到相同的问题。

I'm using Chrome 32.0.1700.107, and I've also tested it in IE8, which experiences the same problem.

我已经发布了以下代码的相关部分:

I've posted the relevant sections of code below:

.php - My View

body.php - My View

<?php if($this->session->userdata('msg_reply')): ?>
    <p class="big-message <?php echo $this->session->userdata('msg_colour_class'); ?>">
        <strong><?php echo $this->session->userdata('msg_title'); ?></strong><br>
        <?php echo $this->session->userdata('msg_message'); ?></strong>
    </p>

    <?php
        $this->session->unset_userdata('msg_reply'); 
        $this->session->unset_userdata('msg_colour_class');
        $this->session->unset_userdata('msg_title');
        $this->session->unset_userdata('msg_message');
    ?>

<?php endif; ?>

我的控制器

$this->session->set_userdata(array(
    'msg_reply' => true, 
    'msg_colour_class' => 'green-gradient', 
    'msg_title' => 'Contacts Updated', 
    'msg_message' => 'You have successfully updated ' . $i . ' out of ' . $t . ' contacts.'
));

redirect('my_department/student_absence');

摘要:


  1. 我知道我应该使用 set_flashdata(),但此函数会挂起浏览器

  2. set_userdata ()可正常工作,并在会话中创建值

  3. unset_userdata

  4. 删除 set_flashdata() unset_userdata()会停止问题,但也会阻止我设置flashdata或删除正常的会话数据。

  1. I know I should be using set_flashdata(), but this function hangs the browser
  2. set_userdata() works fine, and creates the values in the session
  3. unset_userdata() hangs the browser
  4. There are NO error messages anywhere.
  5. Removing instances of set_flashdata() or unset_userdata() stops the problem, but also stops me from being able to set flashdata or remove normal session data.

EDIT

我自动加载会话库,以便每个页面都可用。
此外,尝试取消设置控制器中的会话数据会挂起浏览器。

I autoload the session library so it's available for each page. Also, trying to unset the session data in the controller hangs the browser.

在等待页面完成加载后,我发现 unset_userdata()正在工作,但它花了很长时间来做任何事情。

After waiting for the page to finish loading, I found that unset_userdata() is working, but its taking ages to do anything.

现在我运行reload脚本3次,

I've ran the reload script 3 times now, and each time, it's taken exactly 2 minutes to reload the page.

推荐答案

正如Hashem之前提到的,使用多个调用会话管理方法增加了很多开销。此外,尝试在控制器中而不是在视图中管理会话。为了访问codeigniter对象,通常绑定到控制器和模型中的$ this上下文,你必须通过get_instance()获取一个引用到该对象,进一步增加开销。

As has been mentioned before by Hashem, using multiple calls to session management methods adds a lot of overhead. Additionally, try managing your sessions in the controller instead of the view. In order to get access to the codeigniter object, commonly bound to the $this context in Controllers and Models, you have to grab a reference via get_instance() to that object, adding further overhead.

也就是说,将会话管理移动到控制器本身,这应该应该做。您应该在会话对象中设置并传递到视图中的唯一方法是维护会话所需的数据,并提供干净的用户体验。

That said, move your session management to the controller itself, as should be done anyway. The only things you should be setting in your session object and passing into the view is the data needed to maintain your session, and provide a clean user experience.

给定消息你似乎在传球,flashdata似乎是最合适的路线。所以,而不是以下:

Given the messages you seem to be passing, flashdata seems the most appropriate route. So, instead of the following:

$this->session->set_userdata('msg_title', 'Success');
$this->session->set_userdata('msg_content', 'Some success message here');

您应该使用

$this->session->set_flashdata('msg_title', 'Success');
$this->session->set_flashdata('msg_content', 'Some success message here');

在下一个请求时清除闪存数据,因此调用unset不是必要的,从而进一步降低开销。只有应该在整个用户会话中保留的项目才能使用set_userdata()进行存储

Flash data is cleared upon the next request, so calling unset is not necessary, reducing overhead further. Only those items that should persist across an entire user session should be stored using set_userdata()

如果您需要其他信息或有不清楚的地方,请告诉我们很乐意更新答案。

Please let me know if you need additional information or if something is unclear and I will be happy to update the answer.

这篇关于Codeigniter会话挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆