在 CodeIgniter 3 中重定向后不会清除 Flashdata
Flashdata is not cleared after a redirect in CodeIgniter 3
我正在使用 CodeIgniter 3,我在该项目上设置的 flashdata 即使在重定向后也没有清除。刷新页面或再次访问页面后仍然显示。我该如何解决这个问题?
控制器中的一个函数。
public function updateDonor($donor)
{
$birthDate = $this->input->post('donordob');
$currentDate = date("Y-m-d");
$age = date_diff(date_create($birthDate), date_create($currentDate));
$this->form_validation->set_rules('donorname', 'Donor Name', 'required');
$this->form_validation->set_rules('donornic', 'Donor NIC', 'required');
$this->form_validation->set_rules('donordob', 'Donor DOB', 'required');
$this->form_validation->set_rules('donorweight', 'Donor Weight', 'required');
$this->form_validation->set_rules('donormobile', 'Donor Mobile', 'required');
if ($this->form_validation->run()) {
$data = [
'DonorName' => $this->input->post('donorname'),
'DonorNIC' => $this->input->post('donornic'),
'DonorDOB' => $this->input->post('donordob'),
'DonorAge' => $age->format("%y"),
'DonorWeight' => $this->input->post('donorweight'),
'DonorMobile' => $this->input->post('donormobile'),
];
$this->load->model('Donor_Model');
$data['donor'] = $this->Donor_Model->updateDonor($data, $donor);
$this->session->set_flashdata('donorupdated', 'Donor detailed updated successfully!');
redirect(base_url('index.php/staff/viewdonors'));
} else {
$this->editDonors($donor);
}
}
视图中的代码
<?php if ($this->session->flashdata('donorupdated')) { ?>
<script>
alertify.set('notifier', 'position', 'top-right');
alertify.success("<?php echo $this->session->flashdata('donorupdated'); ?>")
</script>
<?php } ?>
如果您不这样做,请按照以下步骤操作。
CREATE TABLE IF NOT EXISTS `ci_sessions` (
`id` varchar(128) NOT NULL,
`ip_address` varchar(45) NOT NULL,
`timestamp` int(10) unsigned NOT NULL DEFAULT 0,
`data` blob NOT NULL,
KEY `ci_sessions_timestamp` (`timestamp`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
然后里面autoload.php
添加 $autoload['libraries'] = array('session');
请参考一下Session Library
然后里面config.php
.
$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200
$config['sess_save_path'] = 'ci_sessions';
$config['sess_match_ip'] = TRUE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = TRUE;
查看代码
<?php if($this->session->flashdata('donorupdated')) { ?>
echo $this->session->flashdata('donorupdated');
<?php } ?>
只需在 <script>
标签后添加 unset($_SESSION['donorupdated']);
,如下所示
<?php if ($this->session->flashdata('donorupdated')) { ?>
<script>
alertify.set('notifier', 'position', 'top-right');
alertify.success("<?php echo $this->session->flashdata('donorupdated'); ?>")
</script>
<?php unset($_SESSION['donorupdated']); ?>
<?php } ?>
我正在使用 CodeIgniter 3,我在该项目上设置的 flashdata 即使在重定向后也没有清除。刷新页面或再次访问页面后仍然显示。我该如何解决这个问题?
控制器中的一个函数。
public function updateDonor($donor)
{
$birthDate = $this->input->post('donordob');
$currentDate = date("Y-m-d");
$age = date_diff(date_create($birthDate), date_create($currentDate));
$this->form_validation->set_rules('donorname', 'Donor Name', 'required');
$this->form_validation->set_rules('donornic', 'Donor NIC', 'required');
$this->form_validation->set_rules('donordob', 'Donor DOB', 'required');
$this->form_validation->set_rules('donorweight', 'Donor Weight', 'required');
$this->form_validation->set_rules('donormobile', 'Donor Mobile', 'required');
if ($this->form_validation->run()) {
$data = [
'DonorName' => $this->input->post('donorname'),
'DonorNIC' => $this->input->post('donornic'),
'DonorDOB' => $this->input->post('donordob'),
'DonorAge' => $age->format("%y"),
'DonorWeight' => $this->input->post('donorweight'),
'DonorMobile' => $this->input->post('donormobile'),
];
$this->load->model('Donor_Model');
$data['donor'] = $this->Donor_Model->updateDonor($data, $donor);
$this->session->set_flashdata('donorupdated', 'Donor detailed updated successfully!');
redirect(base_url('index.php/staff/viewdonors'));
} else {
$this->editDonors($donor);
}
}
视图中的代码
<?php if ($this->session->flashdata('donorupdated')) { ?>
<script>
alertify.set('notifier', 'position', 'top-right');
alertify.success("<?php echo $this->session->flashdata('donorupdated'); ?>")
</script>
<?php } ?>
如果您不这样做,请按照以下步骤操作。
CREATE TABLE IF NOT EXISTS `ci_sessions` (
`id` varchar(128) NOT NULL,
`ip_address` varchar(45) NOT NULL,
`timestamp` int(10) unsigned NOT NULL DEFAULT 0,
`data` blob NOT NULL,
KEY `ci_sessions_timestamp` (`timestamp`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
然后里面autoload.php
添加 $autoload['libraries'] = array('session');
请参考一下Session Library
然后里面config.php
.
$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200
$config['sess_save_path'] = 'ci_sessions';
$config['sess_match_ip'] = TRUE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = TRUE;
查看代码
<?php if($this->session->flashdata('donorupdated')) { ?>
echo $this->session->flashdata('donorupdated');
<?php } ?>
只需在 <script>
标签后添加 unset($_SESSION['donorupdated']);
,如下所示
<?php if ($this->session->flashdata('donorupdated')) { ?>
<script>
alertify.set('notifier', 'position', 'top-right');
alertify.success("<?php echo $this->session->flashdata('donorupdated'); ?>")
</script>
<?php unset($_SESSION['donorupdated']); ?>
<?php } ?>