php - Codeigniter Session Sets 2nd time during login -
<?php class login_model extends ci_model { public function login() { $username = $_post['uname']; $password = $_post['pw']; $query = $this->db->get_where('test_logins', array('name' => $username, 'password' => $password)); $count = $query->num_rows() > 0; if ($count == 1) { foreach ($query->result_array() $row) { $exam_id=$row['id']; } $query2 = $this->db->get_where('candidates', array('email' =>$_post['email'])); $count2=$query2->num_rows(); //echo($count2); if($count2==1){ foreach ($query2->result_array() $row2) { $candidate_id=$row2['id']; $part1=$row2['part1']; $part2=$row2['part2']; $part3=$row2['part3']; $part4=$row2['part4']; } //set session $newdata1 = array( 'username' => $username, 'exam_id'=> $exam_id, 'candidate_id'=> $candidate_id, 'logged_in' => true ); $this->session->set_userdata($newdata1); if(empty($part1)){ return 'part1'; }else if(empty($part2)){ return 'part2'; }else if(empty($part3)){ return 'part3'; }else if(empty($part4)){ return 'part4'; } }else{ $data = array( 'name' => $_post['name'], 'email' => $_post['email'], 'exam_id' => $exam_id, 'contact' => $_post['mobile'], ); $this->db->insert('candidates', $data); $candidate_id=$this->db->insert_id(); $newdata = array( 'username' => $username, 'exam_id'=> $exam_id, 'candidate_id'=> $candidate_id, 'logged_in' => true ); $this->session->set_userdata($newdata); return 'part1'; }//else insert candidate }else { return false; } } } ?>
checking if login id , password correct. if yes checking if user registered exam or not, if registered user creating session checking part of exam remaining , redirecting parts controller, if not registered creating record , creating session redirecting 1st part of exam, session null on 1st attempt , gets set 2nd attempt.
the code mentioned in question having flaws write clean , in codeigniter way here plz try , let me know then. unable put whole code comment write answer :)
<?php class login_model extends ci_model { public function login() { $username = $this->input->post['uname']; $password = $this->input->post['pw']; $exam_id = 0; $candidate_id = 0; $query = $this->db->get_where('test_logins', array( 'name' => $username, 'password' => $password )); if ($query->num_rows() > 0) { $row = $query->row(); if (isset($row)) { $exam_id = $row->id; } $query2 = $this->db->get_where('candidates', array( 'email' => $this->input->post('email') )); if ($query2->num_rows() > 0) { $row2 = $query2->row(); if (isset($row2)) { $candidate_id = $row2->id; $part1 = $row2->part1; $part2 = $row2->part2; $part3 = $row2->part3; $part4 = $row2->part4; } if(empty($part1)) { return 'part1'; } else if(empty($part2)) { return 'part2'; } else if(empty($part3)) { return 'part3'; } else if(empty($part4)) { return 'part4'; } } else { $this->db->insert('candidates', array( 'name' => $this->input->post('name'), 'email' => $this->input->post('email'), 'exam_id' => $exam_id, 'contact' => $this->input->post('mobile') )); return 'part1'; } $this->session->set_userdata(array( 'username' => $username, 'exam_id'=> $exam_id, 'candidate_id'=> $candidate_id, 'logged_in' => true )); } else { return false; } } }
Comments
Post a Comment