Welcome Guest, Not a member yet? Register   Sign In
Sessions not saved when changing page
#1

[eluser]gbar[/eluser]
Hello. I'm developing a crud, and i'm in the process of saving data from the search field.
I decided to use the session to save two data post, referring to the search term, and the table in which to search.
There are several posts in this forum about this topic, but I've not found something similar to my problem.

Controller code:

Code:
function authors($sortBy = 'id', $sortOrder = 'desc', $offset = 0)
        {
            $limit = 5;
            
            $data = array(
                'search' => $this->input->post('search'),
                'field' => $this->input->post('field')
            );
                
            $this->session->set_userdata($data);            
            
            $results = $this->authors_model->_retrieveAuthors($limit, $offset, $sortBy, $sortOrder);
            $data['records'] = $results['my_rows'];
            $data['numRows'] = $results['num_rows'];
            
            $config = array();
            $config['base_url'] = site_url('admin/sections/authors/' . $sortBy . '/' . $sortOrder);
            $config['total_rows'] = $data['numRows'];
            $config['per_page'] = $limit;
            $config['uri_segment'] = 6;
            $config['first_link'] = img(base_url() . 'img/first.png');
            $config['last_link'] = img(base_url() . 'img/last.png');
            $config['next_link'] = img(base_url() . 'img/next.png');
            $config['prev_link'] = img(base_url() . 'img/previous.png');
            $config['num_tag_open'] = '<span class="navigation">';
            $config['num_tag_close'] = '</span>';
            $config['cur_tag_open'] = '<span class="navigation_current">';
            $config['cur_tag_close'] = '</span>';

            $this->pagination->initialize($config);
            $data['pagination'] = $this->pagination->create_links();
}

Model code:

Code:
function _retrieveAuthors($limit, $offset, $sortBy, $sortOrder)
        {
            $search = $this->session->userdata('search');
            $field = $this->session->userdata('field');
            $key_field = $field == '' ? 'author' : $field;
            
            $this->db->like($key_field, $search);            
            $this->db->order_by($sortBy, $sortOrder);
            $this->db->limit($limit, $offset);
            $query = $this->db->get('authors');
            
            if($query->num_rows() > 0)
            {
            
            $data['my_rows'] = $query->result();
                    
            $this->db->like($key_field, $search);
            $query = $this->db->count_all_results('authors');
            $data['num_rows'] = $query;
            
            return $data;
            
            }
        }

View code:

Code:
&lt;?php echo form_open('admin/sections/authors'); ?&gt;
                        &lt;?php
                        $options = array(
                          'id' => 'Id',
                          'author' => 'Author',
                          'biography' => 'Biography',
                          'site' => 'Site Url'
                        );
                        ?&gt;
                        &lt;?php echo form_dropdown('field', $options, $field == '' ? 'author' : $field); ?&gt;
                        &lt;?php echo form_input('search', $this->session->userdata('search')); ?&gt;
                        &lt;?php echo form_submit('submit', 'Search'); ?&gt;
                    &lt;?php echo form_close(); ?&gt;

Everything works fine, in the search field if I select a word, I get a list of records with that search term. But when I click on the second page, I lose the session data back and i see all records in the database.

I also tried to use flashdata, I get a strange result. At first click on the form, nothing happens, the second click I get the list of records containing the term, it works until the second page. from the third page I lose the data.

someone has an idea?

Thanks
#2

[eluser]Alur[/eluser]
In your controller code i think you should try:

Code:
if ($this->input->post('search')) {
    $data = array(
        'search' => $this->input->post('search'),
        'field' => $this->input->post('field')
    );
    $this->session->set_userdata($data);
}

this way you will not overwrite the search term when you move to the second page...

hope it works!
#3

[eluser]cideveloper[/eluser]
Are your sessions working properly. Set up a new test controller an input the code below.

Code:
$session_id = $this->session->userdata('session_id');
echo "My Session ID is $session_id";

Then go the page. The refresh the page. Is the session id the same? This answer will give a clue as to what is going on.
#4

[eluser]InsiteFX[/eluser]
You also need to update the database session table the one in the documentation is incorrect!
They changed the size of the user_agent field...
Code:
-- --------------------------------------------------------------

--
-- Table structure for CodeIgniter ci_sessions.
--
DROP TABLE IF EXISTS `ci_sessions`;

CREATE TABLE IF NOT EXISTS  `ci_sessions` (
  `session_id`    varchar(40)           DEFAULT '0' NOT NULL,
  `ip_address`    varchar(16)           DEFAULT '0' NOT NULL,
  `user_agent`    varchar(120)                      NOT NULL,
  `last_activity` int(10)      unsigned DEFAULT 0   NOT NULL,
  `user_data`     text,
  PRIMARY KEY (session_id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;

-- -------------------------------------------------------------
--  `user_data`    text,        COMMENT - maximum length of 65535 characters.
--  `user_data`    mediumtext,  COMMENT - maximum length of 16777215 characters.
--  `user_data`    longtext,    COMMENT - maximum length of 4294967295 characters.

What happens is if the table is not updated it will lose the session because the user_agent will not match.

InsiteFX
#5

[eluser]gbar[/eluser]
Hello, thank you everyone for your useful answers. I solved with the suggestion of Alur, that way the thing works perfectly as I want.
I just have to solve a small problem when I want to undo the effects of research, and then return to displaying all records in the table.
I think the correct behavior is to click again on the button of the form, with the search field blank.
Has no effect, the search area is repopulated by the previous term, and then restart the search. In practice this code, which would be the most natural, does not work:

Code:
if ($this->input->post('search')) {
                $data = array(
                    'search' => $this->input->post('search'),
                    'field' => $this->input->post('field')
                );
                $this->session->set_userdata($data);
            }
            
            if($this->input->post('search') == '')
            {
                $this->session->unset_userdata('search');    
                $this->session->unset_userdata('field');
            }

While if I write this:

Code:
if ($this->input->post('search')) {
                $data = array(
                    'search' => $this->input->post('search'),
                    'field' => $this->input->post('field')
                );
                $this->session->set_userdata($data);
            }
            
            if($this->input->post('search') == 'delete')
            {
                $this->session->unset_userdata('search');    
                $this->session->unset_userdata('field');
            }

and then I type the word 'delete', but it could be any other word, I get the desired effect. Maybe I should put as the ascii code of a null or something.

Thanks anyway to all.

p.s. for InsiteFX, I updated the table




Theme © iAndrew 2016 - Forum software by © MyBB