登录codeigniter后如何获取用户ID

时间:2017-06-19 03:36:16

标签: php mysql codeigniter

我已成功创建了注册和登录系统。 我在登录表单中使用了用户名和密码,我想将用户标识放在其他形式,如隐藏字段

登录模型cliente.php:

public function login($username,$password){
    $this->db->select('IdCliente');
    $this->db->where('LoginCliente',$username);
    $this->db->where('PassCliente',$password);
    $q = $this->db->get('clientes');

    if($q->num_rows()>0){
        return true;
    }else{
        return false;
    }

登录控制器login.php:

   public function index(){
    if($this->session->userdata('LoginCliente')){
        redirect('profile');
    }
    if(isset($_POST['password'])){
        $this->load->model('cliente');
        if($this->cliente->loginemp($_POST['username'],$_POST['password'])){
            $this->session->set_userdata('Login',$_POST['username']);
            redirect('PanelIndex');
        }elseif(isset($_POST['password'])){
        $this->load->model('cliente');
        if($this->cliente->login($_POST['username'],$_POST['password'])){
            $this->session->set_userdata('LoginCliente',$_POST['username']);
            $this->session->userdata('IdCliente');
            redirect('profile');
        }else{
            redirect('login');              
        }
    }

    }

    $this->load->view('inicio/loginview');  

}

profile.php

public function index()
{


         $this->load->view("/clientes/clienteindex");

}

2 个答案:

答案 0 :(得分:0)

如果你想 IdCliente ,你需要先从登录功能返回它。

public function login($username,$password){
    $this->db->select('IdCliente,LoginCliente');
    $this->db->where('LoginCliente',$username);
    $this->db->where('PassCliente',$password);
    $q = $this->db->get('clientes');

    if($q->num_rows()>0){
        return $q->row_array();
    }else{
        return false;
    }
}

现在在索引功能:

$session_data = $this->cliente->login($_POST['username'],$_POST['password'])
if(isset($session_data) && !empty($session_data){            
        $this->session->userdata('session_data',$session_data);
        redirect('profile');
 }

在个人资料(在视图中):

$session_data = $this->session->userdata('session_data');
    $IdCliente= $session_data['IdCliente'];
    $LoginCliente= $session_data['LoginCliente'];

答案 1 :(得分:0)

从CI库文件夹中创建Simple_login.php文件并输入此代码。

android:clipToPadding="false"

然后修改你的控制器

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/******************
* SHAHBLOGGER.COM*
******************/

class Simple_login {

   // SET SUPER GLOBAL
   var $CI = NULL;

   /**
    * Class constructor
    *
    * @return   void
    */
   public function __construct() {
       $this->CI =& get_instance();
   }


   public function login($email, $password, $referer) {

       //check username and password
       $query = $this->CI->db->get_where('clientes',array('LoginCliente'=>$username,'PassCliente' => $password)); // better use md5($password)

       if($query->num_rows() == 1) {
           //let start query

           $row  = $query;
           $user = $row->row();
           $id   = $user->IdCliente;


           //set session user

           $this->CI->session->set_userdata('id', $id);

           redirect($referer);

       }else{

           //redirect them to you login page
           redirect(site_url('login'));
       }
        return false;
    }



   /* check If They're login. If yes, set userdata */
   public function check_login() {

       //check session id set or not set. if not set, they're not login
       if($this->CI->session->userdata('id') == '') {


           //they're not login so redirect them to login page
           redirect(site_url('login'));

       } 
   }



   // Unset session data
   public function logout() {   
       $this->CI->session->unset_userdata('id');
       redirect(site_url('login'));
   }
}
  1. 激活了Codeigniter Autoload功能。 Live Example

  2. 调用登录用户的ID使用此<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Login extends CI_Controller { //<<-- change 'Login' to your own class base on your controller file name public function index() { // Fungsi Login $valid = $this->form_validation; $username= $this->input->post('username'); $password = $this->input->post('password'); $referer = $_SERVER['HTTP_REFERER'];// change where you want user redirect after success login $valid->set_rules('username','Username','required'); $valid->set_rules('password','Password','required'); if($valid->run()) { $this->simple_login->login($username,$password, $referer); } // End login $this->load->view('you_login_view'); } public function logout(){ $this->simple_login->logout(); } }

  3. 只能使登录用户用户访问页面,将其粘贴到控制器$this->session->userdata('id');

  4. 更新:根据以下评论。我已将Query更改为修改后的Query(Codeigniter Active Record)以避免SQL注入