Codeigniter未设置会话 [英] Codeigniter unset session

查看:102
本文介绍了Codeigniter未设置会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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



在这种情况下,我将由 $ _ POST 传递的搜索值存储到我的会话中,因为如果我有更多结果点击下一页搜索keppe



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



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



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

  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->国家模型 - > 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作为名称,country.id作为nation_id,nation.name_it作为nation_name');
if($ this-> session-> userdata('language')==en)
$ this-> db-> select('region.id,region.name_en作为名称,country.id作为nation_id,nation.name_en作为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->其中('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作为名称,country.id作为nation_id,nation.name_it作为nation_name');
if($ this-> session-> userdata('language')==en)
$ this-> db-> select('region.id,region.name_en作为名称,country.id作为nation_id,nation.name_en作为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->其中('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->其中('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->其中('region.nation_id',$ nation_id)
$ this-> db-> order_by(name,asc);

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


解决方案

/ p>


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


我可以用这个帮助你:

  $ this-> session-> unset_userdata '); 

,对于多个数据,您可以:

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

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

并销毁会话:

  $ this-> session-> sess_destroy(); 

现在对于页面更改部分 >:


您可以设置 paginator 等于 configanchor_class classname


之后,只需使用 jquery onclick ,它将向上发送将取消设置用户会话的控制器功能。


Hi I have a site develop in CodeIgniter.

In one of my page I'm using pagination of CodeIgniter after a search form.

In this case I store into my session the search value passed by $_POST because if I have more result clicking the next page the search keppe the searching value.

But when I change page for example I want to return to the index and after return to my search form page the session is already created and make the query with the value of the session. How can I destroy or unset the value of the session when I change page? Is this possible?

Into my model function I check if the session value is different from 0 and exist, if true I make a query with the session value.

This is my controller (nation is the value to store into the session)

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);
        }
    }

and this is my model to search:

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();      
     }

解决方案

answering to your question:

How can I destroy or unset the value of the session?

I can help you by this:

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

and for multiple data you can:

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

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

and to destroy the session:

$this->session->sess_destroy();

Now for the on page change part (on the top of my mind):

you can set the config "anchor_class" of the paginator equal to the classname you want.

after that just check it with jquery onclick for that class which will send a head up to the controller function that will unset the user session.

这篇关于Codeigniter未设置会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆