0

我的控制器中有 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()。我应该怎么办?!

4

3 回答 3

0

有很多方法可以做到这一点。这里有一些

如果存在则附加一个get参数然后搜索否则重定向回来

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 函数,不要让用户直接调用它。使用 _ 将为您完成。
是您可以阅读的链接。

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');
    }
}
于 2013-09-14T13:09:06.513 回答
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);
}
于 2013-09-14T12:40:46.303 回答
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');
    }
}

希望对您有所帮助。如果您遇到任何问题,请告诉我。

于 2013-09-14T14:33:43.580 回答