Welcome Guest, Not a member yet? Register   Sign In
Session Bug CI v1.7.2
#1

[eluser]Nicholas Hanks[/eluser]
I tried to solve this problem for hours. Seems like it is bug in CI. When the form is valid it should set session variable "request" = "yes" it does that however CI fails to read it and removes it...weird.

Once of form is submitted it should not render form view again but it does why???? I think it is bug.
Code:
public function requestForm()
    {
           if($this->session->userdata('received') !== false)
        {
            $v = $this->session->userdata('received');
            
            if(strcmp($v,'yes') == 0)
            {
                $data['header'] = "Re-Submisson Attempt";
                $data['message'] = "We have already received your information. Our sales representative will contact you soon.";
                $this->load->view('message', $data);
            }
            
            else
            {
                echo 'dfgdfgddgdfg';
                die();
            }
        }
        
        else
        {
             $this->load->helper('form');
            $this->load->helper('string');
            
            $this->session->unset_userdata('token');
            
            $token = random_string('alnum', 16);
        
            $this->session->set_userdata('token',$token);
            $data['token'] = $this->session->userdata('token');
            $this->load->view('form',$data);          
        }
    }
    
    public function submitRequestForm()
    {
        $result = array();
        $this->load->library('form_validation');
        $token = $this->session->userdata('token');
        
        //Set Validation
        $this->form_validation->set_rules('email', 'Email', 'trim|valid_email|required|max_length[42]|matches[email]|xss_clean');
        $this->form_validation->set_rules('confirmEmail', 'Confirm Email', 'trim|valid_email|required|max_length[42]|xss_clean');
        $this->form_validation->set_rules('firstname', 'First Name', 'trim|required|max_length[42]|xss_clean');
        
        
        if(!isset($_POST['contactBy']))
        {
            $result['contactBy'] = false;
        }
        
        if(isset($_POST['contactBy']))
        {
            if(!is_array($_POST['contactBy']))
            {
                $result['contactBy'] = false;
            }
            
            else
            {
                if ((!in_array('By Email',$_POST['contactBy'])) && (!in_array('By Phone',$_POST['contactBy'])))
                {
                    $result['contactBy'] = false;
                }
            }
        }
        
        if(!isset($_POST['country']))
        {
            $result['country'] = false;
        }
        
        if(!isset($_POST['type']))
        {
            $result['type'] = false;
        }
        
        if(!isset($_POST['token']))
        {
            $result['token'] = false;
        }
        
        if(isset($_POST['token']))
        {
            if($token === false)//token was never set so
            {
                echo 'Here';
                $result['token'] = false;
            }
            
            else
            {
                if(strcmp($_POST['token'],$token) !== 0)
                {
                    echo $token;
                    $result['token'] = false;
                }
            }
        }
                
        $data['result'] = $result;
        
        if (($this->form_validation->run() == false) || (count($result) !== 0))
        {
            $this->load->helper('string');
            $this->load->helper('form');
            
              $token = random_string('alnum', 16);
              
              $this->session->unset_userdata('token');
               $this->session->set_userdata('token',$token);
               $data['token'] = $this->session->userdata('token');
            
               $this->load->view('form',$data);        
        }
        
        else
        {
            $contact = implode(', ',$_POST['contactBy']);
            
            
            $this->session->unset_userdata('token');
            $this->session->set_userdata('received','yes');
            $data['header'] = "Submission Sucessful";
            $data['message'] = "Your information has been received. Our sales representative will contact you soon.";
            $this->load->view('message', $data);
        }        
    }
#2

[eluser]Pascal Kriete[/eluser]
If this controller works as I think it does, then the logic doesn't quite pan out. Wouldn't you want the resubmission check in the submitRequestForm function?

Also, how are you getting back to the form? Hitting the back button? In that case the browser will probably pull from it's page cache - I would suggest adding a redirect after submission.
#3

[eluser]Nicholas Hanks[/eluser]
Thank you very much for the response. I am sorry I should have explained little bit about the controller but here's how it works.

User will request form from ---> requestForm()

before dispatching form view it checks whether user already submitted one or not

If yes the it will set the token and will render a view otherwise it will send message you submitted from once already.


User will submit form in -----> submitRequestForm()

As usual does some validation and if it passes then it sets the session $this->session->set_userdata('received','yes'); and renders a thank you message. If validation fails it send form again with validation error entact.

That's it...

Now I solved this problem. Here what I found
When you enable $db['default']['cache_on'] = TRUE;
Session things sometime gets messed up. So, I had to do this
Code:
if ($this->sess_use_database === TRUE)
        {
            $this->CI->db->cache_off();
            $this->CI->db->where('session_id', $session['session_id']);

            if ($this->sess_match_ip == TRUE)
            {
                $this->CI->db->cache_off();
                $this->CI->db->where('ip_address', $session['ip_address']);
            }

            if ($this->sess_match_useragent == TRUE)
            {
                $this->CI->db->cache_off();
                $this->CI->db->where('user_agent', $session['user_agent']);
            }
           $this->CI->db->cache_off();
            $query = $this->CI->db->get($this->sess_table_name);

            // No result?  Kill it!
            if ($query->num_rows() == 0)
            {
                $this->sess_destroy();
                return FALSE;
            }

            // Is there custom data?  If so, add it to the main session array
            $row = $query->row();
            if (isset($row->user_data) AND $row->user_data != '')
            {
                $custom_data = $this->_unserialize($row->user_data);

                if (is_array($custom_data))
                {
                    foreach ($custom_data as $key => $val)
                    {
                        $session[$key] = $val;
                    }
                }
            }
            $this->CI->db->cache_on();
        }
from library Session.php




Theme © iAndrew 2016 - Forum software by © MyBB