问题
So I'm using codeigniter for our internal intranet.
Usually when doing database selections, updates, deletes etc, I use ajax, and use jquery to update the page without redirecting.
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.
I can set the data just fine:
$this->session->set_userdata('msg_title', 'Success');
$this->session->set_userdata('msg_content', 'Some success message here');
I also know I should be using flashdata
for examples like this, but there seems to be a bug.
When I set data using flashdata, or when I try to unset userdata using unset_userdata()
, the browser hangs.
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.
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:
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; ?>
My Controller
$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');
Summary:
- I know I should be using
set_flashdata()
, but this function hangs the browser set_userdata()
works fine, and creates the values in the sessionunset_userdata()
hangs the browser- There are NO error messages anywhere.
- Removing instances of
set_flashdata()
orunset_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.
After waiting for the page to finish loading, I found that unset_userdata()
is working, but its taking ages to do anything.
I've ran the reload script 3 times now, and each time, it's taken exactly 2 minutes to reload the page.
回答1:
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.
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');
You should use this
$this->session->set_flashdata('msg_title', 'Success');
$this->session->set_flashdata('msg_content', 'Some success message here');
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.
来源:https://stackoverflow.com/questions/21606594/codeigniter-session-hanging