24

嗨,我有一个在 CodeIgniter 中开发的网站。

在我的一个页面中,我在搜索表单后使用 CodeIgniter 的分页。

在这种情况下,我将传递的搜索值存储到我的会话中,$_POST因为如果我有更多结果单击下一页,则搜索会保留搜索值。

但是,例如,当我更改页面时,我想返回索引,并且在返回我的搜索表单页面后,会话已经创建并使用会话的值进行查询。更改页面时如何销毁或取消设置会话的值?这可能吗?

在我的模型函数中,我检查会话值是否不同于 0 并且存在,如果为真,我使用会话值进行查询。

这是我的控制器(国家是存储到会话中的值)

public function region_list(){
        $this->load->model('backend/Nation_model');
        $this->load->library("pagination");

        if($_POST)
        {
            if (isset($_POST['ricerca'])){

                $nation = $this->input->post('nation');
                if(strlen($nation) > 0){
                   $this->session->set_userdata('nation',$nation);
                }

                $config = array();
                $config["base_url"] = base_url() . "index.php/backend/region/region_list";
                $config["total_rows"] = $this->Region_model->countRegionSearch();
                $config["per_page"] = 10;
                $config["uri_segment"] = 4;

                $this->pagination->initialize($config);

                $page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
                $data["regionlist"] = $this->Region_model->regionSearch($config["per_page"], $page);
                $data["links"] = $this->pagination->create_links();
                $data["nationlist"] = $this->Nation_model->nationList();

                $this->load->view('backend/region_list_view',$data);
            }
        }
        else
        {
            $config = array();
            $config["base_url"] = base_url() . "index.php/backend/region/region_list";
            $config["total_rows"] = $this->Region_model->countRegion();
            $config["per_page"] = 10;
            $config["uri_segment"] = 4;

            $this->pagination->initialize($config);

            $page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
            $data["links"] = $this->pagination->create_links();
            $data["regionlist"] = $this->Region_model->regionList($config["per_page"], $page);
            $data["nationlist"] = $this->Nation_model->nationList();

            $this->load->view('backend/region_list_view',$data);
        }
    }

这是我要搜索的模型:

function regionList($limit=null, $start=null) {
        $nation_id = $this->session->userdata('nation');
        if ($this->session->userdata('language')=="it")
            $this->db->select('region.id, region.name_it as name,nation.id as nation_id, nation.name_it as nation_name');
        if ($this->session->userdata('language')=="en")
            $this->db->select('region.id, region.name_en as name,nation.id as nation_id, nation.name_en as nation_name');
        $this->db->from('region');
        $this->db->join('nation', 'region.nation_id = nation.id','left');
        if((isset($nation_id))&&($nation_id!=0))
            $this->db->where('region.nation_id', $nation_id);
        $this->db->order_by("name", "asc");
        $this->db->limit($limit, $start);
        $query = $this->db->get();
        $region = array();
        foreach ($query->result() as $row)
            array_push($region, $row);

        return $region;     
    }

    function countRegion() {
        $nation_id = $this->session->userdata('nation');
        if ($this->session->userdata('language')=="it")
            $this->db->select('region.id, region.name_it as name,nation.id as nation_id, nation.name_it as nation_name');
        if ($this->session->userdata('language')=="en")
            $this->db->select('region.id, region.name_en as name,nation.id as nation_id, nation.name_en as nation_name');
        $this->db->from('region');
        $this->db->join('nation', 'region.nation_id = nation.id','left');
        if((isset($nation_id))&&($nation_id!=0))
            $this->db->where('region.nation_id', $nation_id);
        $this->db->order_by("name", "asc");
        $query = $this->db->get();
        return $query->num_rows();  
    }

    public function regionSearch($limit=null, $start=null){
        $nation_id = $this->session->userdata('nation');
        if ($this->session->userdata('language')=="it"){
            $this->db->select('*,region.id, region.name_it as name,nation.id as nation_id, nation.name_it as nation_name');
            if($this->input->post('name')!="")
                $this->db->where('region.name_it LIKE "%'.$this->input->post('name').'%"');
        }

        if ($this->session->userdata('language')=="en"){
            $this->db->select('*,region.id, region.name_en as name,nation.id as nation_id, nation.name_en as nation_name');
            if($this->input->post('name')!="")
                $this->db->where('region.name_en LIKE "%'.$this->input->post('name').'%"');
        }
        $this->db->from('region');
        $this->db->join('nation', 'region.nation_id = nation.id','left');
        if((isset($nation_id))&&($nation_id!=0))
            $this->db->where('region.nation_id', $nation_id);
        $this->db->order_by("name", "asc");
        $this->db->limit($limit, $start);
        $query = $this->db->get();
        $region = array();
        foreach ($query->result() as $row)
            array_push($region, $row);

        return $region; 
     }

     public function countRegionSearch(){
        $nation_id = $this->session->userdata('nation');
        if ($this->session->userdata('language')=="it"){
            $this->db->select('*,region.id, region.name_it as name,nation.id as nation_id, nation.name_it as nation_name');
            if($this->input->post('name')!="")
                $this->db->where('region.name_it LIKE "%'.$this->input->post('name').'%"');
        }

        if ($this->session->userdata('language')=="en"){
            $this->db->select('*,region.id, region.name_en as name,nation.id as nation_id, nation.name_en as nation_name');
            if($this->input->post('name')!="")
                $this->db->where('region.name_en LIKE "%'.$this->input->post('name').'%"');
        }
        $this->db->from('region');
        $this->db->join('nation', 'region.nation_id = nation.id','left');
        if((isset($nation_id))&&($nation_id!=0))
            $this->db->where('region.nation_id', $nation_id);
        $this->db->order_by("name", "asc");

        $query = $this->db->get();
        return $query->num_rows();      
     }
4

6 回答 6

67

回答你的问题:

如何销毁或取消设置会话的值?

我可以帮助你:

$this->session->unset_userdata('some_name');

对于多个数据,您可以:

$array_items = array('username' => '', 'email' => '');

$this->session->unset_userdata($array_items);

并销毁会话:

$this->session->sess_destroy();

现在对于页面更改部分 (在我的脑海中)

您可以将分页器的配置“anchor_class”设置为您想要的名。

之后,只需使用jquery onclick 检查该类,该类将向控制器函数发送一个提示,该控制器函数将取消设置用户会话。

于 2013-01-11T03:17:32.647 回答
7

您应该使用 set_flashdata,而不是使用 set_userdata。

根据 CI 用户指南:

CodeIgniter 支持“flashdata”,即仅可用于下一个服务器请求的会话数据,然后会自动清除。这些非常有用,通常用于信息或状态消息(例如:“记录 2 已删除”)。

http://ellislab.com/codeigniter/user-guide/libraries/sessions.html

于 2013-01-11T01:42:36.217 回答
4
$session_data = array('username' =>"shashikant");
$this->session->set_userdata('logged_in', $session_data);

$this->session->unset_userdata('logged_in');
于 2016-07-19T11:39:02.383 回答
2
$this->session->unset_userdata('session_value');
于 2017-10-18T12:06:01.180 回答
1

我使用旧的 PHP 方式..它取消设置所有会话变量,并且不需要在数组中指定它们中的每一个。在取消设置变量后,我们销毁会话。

session_unset();
session_destroy();
于 2019-06-18T11:17:34.643 回答
-1

我使用旧的 PHP 方式..它取消设置所有会话变量,并且不需要在数组中指定它们中的每一个。在取消设置变量后,我们销毁会话

于 2019-10-04T13:39:28.857 回答