[php] Codeigniter unset session

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

This question is related to php codeigniter session

The answer is


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

Instead of use set_userdata you should use set_flashdata.

According to CI user guide:

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").

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


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


$session_data = array('username' =>"shashikant");
$this->session->set_userdata('logged_in', $session_data);

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

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.

session_unset();
session_destroy();

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to codeigniter

DataTables: Cannot read property 'length' of undefined How to set time zone in codeigniter? php codeigniter count rows CodeIgniter: 404 Page Not Found on Live Server Only variable references should be returned by reference - Codeigniter How to pass a parameter like title, summary and image in a Facebook sharer URL How to JOIN three tables in Codeigniter how to get the base url in javascript How to get id from URL in codeigniter? How to disable PHP Error reporting in CodeIgniter?

Examples related to session

What is the best way to manage a user's session in React? Spring Boot Java Config Set Session Timeout PHP Unset Session Variable How to kill all active and inactive oracle sessions for user Difference between request.getSession() and request.getSession(true) PHP - Session destroy after closing browser Get Current Session Value in JavaScript? Invalidating JSON Web Tokens How to fix org.hibernate.LazyInitializationException - could not initialize proxy - no Session How can I get session id in php and show it?