How to take specific data from table and store it in a cookie codeigniter php? -
i'm new codeigniter , php, few days only, need little help. i'm trying put data in cookie table can check redirect user after login. in table users
there 2 columns named admin
, company
1 or 0 if user or not, , wish insert information cookie.
function conformation in user_controler is:
function conformation(){ $this->load->model('user'); $q = $this->user->confr(); if($q){ $data = array( 'username' => $this->input->post('username'), 'admin' => $this->input->post($a = $this->user->getadmin), // 1/0 users column admin 'company' => $this->input->post($c = $this->user->getcomp), 'login' => true ); if( $a == 1 ){ //is admin redirect admin view $this->session->set_userdata($data); redirect('user_controler/useradm'); } if($c == 1){ //if company redirect company view $this->session->set_userdata($data); redirect('user_controler/usercomp'); } $this->session->set_userdata($data);// if common user redirect user view redirect('user_controler/userpro'); } else{ // if nothing above redirect login page redirect('user_controler/log'); } }
and in user model:
function getadmin{ $this->db->where('admin', 1); $a = $this->db->get('users'); } function getcomp{ $this->db->where('company', 1); $a = $this->db->get('users'); } function conf(){ $this->db->where('username', $this->input->post('username')); $this->db->where('password', $this->input->post('password')); $q = $this->db->get('users'); if($q->num_rows == 1 ){ return true; } }
also have site controller checking login
class site extends ci_controller{ function __construct() { parent::__construct(); $this->login(); } function login(){ $login = $this->session->userdata('login'); if(!isset($login) || login != true){ $this->log; die(); } } }
of course it's not working because should check these column other way don't know how. have enabled table ci_session
, it's work without admin
, company
.
hello , welcome stackoverflow.
here updates code (i have annotated changes):
function conformation(){ $this->load->model('user'); if($this->user->confr()){ //$q wasn't needed, using twice $user = $this->input->post('username'); //i have added referring couple of times. $data = array( 'username' => $user, 'admin' => $this->user->getadmin($user), // method questioning original form looking data never find - question model. 'company' => $this->user->getcomp($user), //same above 'login' => true ); $this->session->set_userdata($data); //it doesn't matter user is, shall set data start with. if($this->user->getadmin($user)){ //is admin redirect admin view redirect('user_controler/useradm'); } elseif($this->user->getcomp($user)){ //if company redirect company view redirect('user_controler/usercomp'); } else { //redirect non-privileged users. redirect('user_controler/userpro'); } } else{ // if nothing above redirect login page redirect('user_controler/log'); } }
users model:
function getadmin($user){ $this->db->where('username', $user); //before returning admin instead finds user $a = $this->db->get('users'); foreach($a $u) { if($u["admin"]==1) { return true; } //this finds if user admin or not, , function return value (true) } } function getcomp($user) { $this->db->where('username', $user); $a = $this->db->get('users'); foreach($a $u) { if($u["company"]==1) { return true; } } } //edited similar function above function conf(){ $this->db->where('username', $this->input->post('username')); $this->db->where('password', $this->input->post('password')); $q = $this->db->get('users'); if($q->num_rows == 1 ){ return true; } }
lastly login function:
function login(){ $login = $this->session->userdata('login'); if(!isset($login) || $login != true){ //you weren't referring $login variable $this->log; die(); } }
hopefully helps problems, let me know if need amendments.
Comments
Post a Comment