php Codeigniter 未设置会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14233595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter unset session
提问by Alessandro Minoccheri
Hi I have a site develop in CodeIgniter.
嗨,我有一个在 CodeIgniter 中开发的站点。
In one of my page I'm using pagination of CodeIgniter after a search form.
在我的一个页面中,我在搜索表单后使用 CodeIgniter 的分页。
In this case I store into my session the search value passed by $_POSTbecause if I have more result clicking the next page the search keppe the searching value.
在这种情况下,我将传递的搜索值存储到我的会话中,$_POST因为如果单击下一页有更多结果,搜索将保留搜索值。
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.
在我的模型函数中,我检查会话值是否不同于 0 并存在,如果为真,我会使用会话值进行查询。
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();
}
回答by mamdouh alramadan
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 paginatorequal to the classnameyou want.
您可以设置配置“anchor_class”中的分页程序等于类名你想要的。
after that just check it with jquery onclick for that classwhich will send a head up to the controller function that will unset the user session.
之后,只需使用jquery onclick 对该类进行检查,该类将向控制器函数发送一个抬头,该函数将取消设置用户会话。
回答by Edgar Nadal
Instead of use set_userdata you should use set_flashdata.
您应该使用 set_flashdata 而不是使用 set_userdata。
According to CI user guide:
根据 CI 用户指南:
CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared. These can be very useful, and are typically used for informational or status messages (for example: "record 2 deleted").
CodeIgniter 支持“flashdata”,或仅可用于下一个服务器请求的会话数据,然后会自动清除。这些可能非常有用,通常用于信息或状态消息(例如:“记录 2 已删除”)。
http://ellislab.com/codeigniter/user-guide/libraries/sessions.html
http://ellislab.com/codeigniter/user-guide/libraries/sessions.html
回答by shashikant parmar
$session_data = array('username' =>"shashikant");
$this->session->set_userdata('logged_in', $session_data);
$this->session->unset_userdata('logged_in');
回答by Gill
$this->session->unset_userdata('session_value');
回答by Ashish Gaikwad
I use the old PHP way..It unsets all session variables and doesn't require to specify each one of them in an array. And after unsetting the variables we destroy the session.
我使用旧的 PHP 方式..它取消设置所有会话变量并且不需要在数组中指定每个变量。在取消设置变量后,我们销毁会话。
session_unset();
session_destroy();
回答by Jagdish
I use the old PHP way..It unsets all session variables and doesn't require to specify each one of them in an array. And after unsetting the variables we destroy the session
我使用旧的 PHP 方式..它取消设置所有会话变量并且不需要在数组中指定每个变量。在取消设置变量后,我们销毁会话

