php - Codeigniter - sessions not working through controller and view -
i'm trying make login using sessions in codeigniter @ time username , password match, can't it. i'm doing this:
controller:
public function __construct() { parent::__construct(); $this->load->model('main_select'); $this->load->helper('url'); $this->load->library('session'); } ...code when username , password match: if($pass === $user){ $this->session->set_userdata(array( 'user_id' => $login['id_user'], ));//we create session 'user_id' }
here supposed created session called 'user_id' in view doesn't work, have this:
if( !$this->session->userdata('id_user') ){ //see content //first content }else{ //see other //second content }
but see same content('second content').
trying destroy (but not working):
public function logout() { //session_unset(); // destroy session //session_destroy(); $this->session->unset_userdata('id_user'); header("location: ".base_url() ); }
what doing wrong? thanks
edit1:
$password = md5( $this->input->post('inputpassword') ); $login = $this->login_select->get_username($username); //si no coincide if( $login['password'] !== $password ) {}
note : use database handle user logins. (code related database login check)
in database create table user
, add 2 fields.
- username
- password
add user logins it
then in code
public function __construct() { parent::__construct(); $this->load->model('main_select'); $this->load->helper('url'); $this->load->library('session'); } // logging public function loging() { $user = mysql_real_escape_string($_post['username']); $pass = md5(mysql_real_escape_string($_post['password'])); $validate = $this->main_select->validate_user($user,$pass); if(empty($validate) || $validate>1) { //not valid user //redirect login page $this->load->view('loging'); } else { //valid user //set session $array = array('user_id' => '$user'); $this->session->set_userdata(); //redirect normal page $this->load->view('home_page'); } } //logout public function logout() { $result= $this->session->sess_destroy(); if ((isset($result))) { header("location: ".base_url() ); } else { } }
in model
public function validate_user($user,$pass) { $query = $this->db->query("select * user username= '$user' , password='$pass'"); $result = $query->result_array(); $count = count($result); return $count; }
Comments
Post a Comment