我的控制器中有2个函数,我正在使用第一个函数向第二个函数发送会话。我所说的是:
public function search()
{
$search_term = $this->input->post('search_term');
$this->session->set_userdata('search_term', $search_term);
redirect('search_result/');
}
和第二个功能:
function search_result()
{
$search_term = $this->session->userdata('search_term');
$data['search_term'] = $search_term;
$this->load->view('includes/template', $data);
}
一切都很好,但问题是,我想阻止直接访问search_result()
功能。我的意思是,当用户直接调用search_term
时,我想取消设置search_result()
会话。我该怎么办?!
答案 0 :(得分:0)
您可以使用flashdata:http://ellislab.com/codeigniter/user-guide/libraries/sessions.html
CodeIgniter支持“flashdata”或仅可用于下一个服务器请求的会话数据,然后自动清除。这些非常有用,通常用于信息或状态消息
这就是你正在寻找的代码:
public function search()
{
$search_term = $this->input->post('search_term');
$this->session->set_flashdata('search_term', $search_term);
redirect('search_result/');
}
function search_result()
{
$search_term = $this->session->flashdata('search_term') ? $this->session->flashdata('search_term') : '';
$data['search_term'] = $search_term;
$this->load->view('includes/template', $data);
}
答案 1 :(得分:0)
有很多方法可以做到。这是一些
如果存在,则附加一个get参数,然后搜索else重定向回来
public function search()
{
$search_term = $this->input->post('search_term');
$this->session->set_userdata('search_term', $search_term);
redirect('search_result?status=1');
}
public function search_result()
{
$status = $this->input->get('status');
if(status == 1){
$search_term = $this->session->userdata('search_term');
$data['search_term'] = $search_term;
$this->load->view('includes/template', $data);
}else{
$this->session->unset_userdata('search_term');
redirect('search');
}
}
保护你seach_result功能,不要让用户直接调用它。
使用_将为您完成。
Here是您可以阅读的链接。
public function search()
{
$search_term = $this->input->post('search_term');
$this->session->set_userdata('search_term', $search_term);
$this->_search_result($search_term);
}
function _search_result($keyword)
{
if(strlen($keyword)>0){
$data['search_term'] = $keyword;
$this->load->view('includes/template', $data);
}else{
redirect('search');
}
}
答案 2 :(得分:0)
在搜索功能
中redirect('search_result/'.$this->session->userdata('search_term'));
<_>在search_result函数中
function search_result()
{
if($this->uri->segment(3))
{
$search_term = $this->session->userdata('search_term');
$data['search_term'] = $search_term;
$this->load->view('includes/template', $data);
}
else
{
redirect('search_result/','refresh');
}
}
希望它会对你有所帮助。如果你遇到任何问题,请告诉我。