2012-12-14 6 views
0

저는 CI에서 새로운 학습자이고이 CI 단순 로그인 라이브러리 (see here)을 따르며, 데이터베이스에 더 많은 필드를 추가하려는 경우를 제외하고는이 모든 간단한 라이브러리 만 있으면됩니다. 테이블을 만들려면 어떻게해야하고 새로 추가 된 필드를 가져올 수 있습니까?Codeigniter는 simplelogin 라이브러리의 필드를 추가합니다.

이 라이브러리에는 'id', 'username', 'email'및 'password'필드 만 포함되어 있으며 'address'와 같은 필드를 추가하고 사용자 생성에 사용하려면 어떻게해야합니까? $this->simplelogin->create('user', '[email protected]', 'password', 'address', true);보기에서 $this->simplelogin->get_data_user('address');을 가져 옵니까?

감사합니다.

답변

1

당신은 변경할 수 있습니다

  • 추가 특성

    비공개 $ address_field = '주소'; 데이터베이스 // 주소

// 데이터베이스에 address

Changge 기능) (만드는 현장이 있어야합니다 있는지 확인하십시오

function create($user = '', $email = '', $password = '', $address = '', $auto_login = TRUE) 
{ 
     // Check data is set 
     if ($user == '' || $password == '' || $email == '') 
      return FALSE; 

     // Email or User already exists | Probably will not need to check the `address` 
     $this->CI->db->where($this->user_field, $user); 
     $this->CI->db->or_where($this->email_field, $email); 
     $query = $this->CI->db->get($this->user_table); 
     if ($query->num_rows() > 0) 
      return FALSE; 

     // Create user into the database 
     $data = array($this->user_field=>$user, $this->email_field=>$email, $this->password_field=>crypt($password, $this->salt), $this->address_field=>$address); 
     if (!$this->CI->db->insert($this->user_table, $data)) 
      return FALSE; 

     // Automatically login to created account 
     if ($auto_login) {   
      $this->CI->session->sess_destroy(); 
      $this->CI->session->sess_create(); 
      $this->CI->session->set_userdata(array('username'=>$user, 'email'=>$email,'address'=>$address)); 
     } 

     return TRUE; // Created! 
    } 

Changge 기능 get_data_user을() :

function get_data_user($param = 'username') { // default is get session username 
     $sess = $this->CI->session->userdata($param); 
     if (!$sess) 
      return ''; 

     return $sess; 
    } 

로그인 기능에서 라인 106 변경 :

$this->CI->session->set_userdata(array('username'=>$row[$this->user_field], 'email'=>$row[$this->email_field],'address'=>$row[$this->address_field])); // Set session data 
+0

제공된 예제에서 필드를 확장하는 데 꽤 유연한 방법입니다. – conmen

0

난 당신이 테이블

<?php 

if (!defined('BASEPATH')) 
    exit('No direct script access allowed'); 
/* 
    SimpleLogin 0.0.3 https://launchpad.net/simplelogincodeigniter 
    A CodeIgniter 2.X library for do a login system simple 
    Author: costales http://launchpad.net/~costales 
    Based on Anthony Graddy & Alex Dunae & Hitesh Ubharani's versions 
    Licensed under LGPL3 
*/ 

class Simplelogin { 

    private $CI; 
    private $user_table  = 'users'; 
    private $user_field  = 'username'; 
    private $email_field = 'email'; 
    private $address_field = 'address'; // add the name of the field in the database 
    private $password_field = 'password'; 
    private $salt   = '$2a$07$R.gJbYU2N.FmA4hPp1y2CN$'; 

    public function __construct() { 
     $this->CI = & get_instance(); 
    } 

    /* Create a user account 
    * 
    * @access public 
    * @param string 
    * @param string 
    * @param string 
    * @param bool 
    * @return bool 
    */ 

    function create($user = '', $email = '', $password = '', $address = '', $auto_login = TRUE) { 
     // Check data is set 
     if ($user == '' || $password == '' || $email == '') 
      return FALSE; 

     // Email or User already exists 
     $this->CI->db->where($this->user_field, $user); 
     $this->CI->db->or_where($this->email_field, $email); 
     $query = $this->CI->db->get($this->user_table); 
     if ($query->num_rows() > 0) 
      return FALSE; 

     // Create user into the database 
     $data = array(
      $this->user_field  => $user, 
      $this->email_field => $email, 
      $this->address_field => $address, 
      $this->password_field => crypt($password, $this->salt) 
     ); 
     if (!$this->CI->db->insert($this->user_table, $data)) 
      return FALSE; 

     // Automatically login to created account 
     if ($auto_login) { 
      $this->CI->session->sess_destroy(); 
      $this->CI->session->sess_create(); 
      $this->CI->session->set_userdata(array(
       'username' => $user, 
       'email' => $email, 
       'address' => $address 
      )); 
     } 

     return TRUE; // Created! 
    } 

    /* Delete user 
    * 
    * @access public 
    * @param integer 
    * @return bool 
    */ 

    function delete($username = '') { 
     if ($username == '') 
      return FALSE; 

     $data = array($this->user_field => $username); 
     if ($this->CI->db->delete($this->user_table, $data)) 
      return TRUE; // Deleted 
     else 
      return FALSE; // Not deleted 
    } 

    /* Login user 
    * 
    * @access public 
    * @param  string 
    * @param  string 
    * @return bool 
    */ 

    function login($user = '', $password = '') { 
     // Data was sent 
     if ($user == '' OR $password == '') 
      return FALSE; 

     // Check if already logged in 
     if ($this->CI->session->userdata('username') == $user) 
      return TRUE; 

     // Check user exists 
     $data = array($this->user_field => $user); 
     $query   = $this->CI->db->get_where($this->user_table, $data); 
     if ($query->num_rows() != 1) 
      return FALSE; 

     // Check against password 
     $row = $query->row_array(); 
     if (crypt($password, $this->salt) != $row[$this->password_field]) 
      return FALSE; 

     $this->CI->session->sess_destroy(); // Destroy old session 
     $this->CI->session->sess_create(); // Create a fresh, brand new session 

     $this->CI->session->set_userdata(
       array(
        'username' => $row[$this->user_field], 
        'email' => $row[$this->email_field], 
        'address' => $row[$this->address_field] 
     )); // Set session data 

     return TRUE; // Login was successful 
    } 

    /* Logout user 
    * 
    * @access public 
    * @return void 
    */ 

    function logout() { 
     $this->CI->session->sess_destroy(); //Destroy session 
    } 

    /* Check if the user is logged 
    * @access public 
    * @return bool 
    */ 

    function is_logged() { 
     if ($this->CI->session->userdata('username')) 
      return TRUE; 
     else 
      return FALSE; 
    } 

    /* Get current username or email 
    * @access public 
    * @param string 
    * @return string 
    */ 

    function get_data_user($param = 'username') { 
     if ($param == 'username') 
      return $this->CI->session->userdata('username'); 
     if ($param == 'email') 
      return $this->CI->session->userdata('email'); 
     if ($param == 'address') 
      return $this->CI->session->userdata('address'); 
     return ''; 
    } 

    /* Change password for a user 
    * @access public 
    * @param string 
    * @param string 
    * @param string 
    * @return bool 
    */ 

    function change_password($user = '', $old_password = '', $new_password = '') { 
     // Check data is set 
     if ($user == '' || $old_password == '' || $new_password == '') 
      return FALSE; 

     // Check old password for this user 
     $data = array($this->user_field  => $user, $this->password_field => crypt($old_password, $this->salt)); 
     $query    = $this->CI->db->get_where($this->user_table, $data); 
     if ($query->num_rows() != 1) 
      return FALSE; 

     // Update password 
     $data = array($this->password_field => crypt($new_password, $this->salt)); 
     $this->CI->db->where($this->user_field, $user); 
     if (!$this->CI->db->update($this->user_table, $data)) 
      return FALSE; 

     return TRUE; 
    } 

    /* Change email for a user 
    * @access public 
    * @param string 
    * @param string 
    * @return bool 
    */ 

    function change_email($user = '', $new_email = '') { 
     // Check data is set 
     if ($user == '' || $new_email == '') 
      return FALSE; 

     // Update email 
     $data = array($this->email_field => $new_email); 
     $this->CI->db->where($this->user_field, $user); 
     if (!$this->CI->db->update($this->user_table, $data)) 
      return FALSE; 

     // Set new internal email 
     $this->CI->session->set_userdata(array('email' => $new_email)); 
     return TRUE; 
    } 

} 
에 주소 필드를 추가 할 필요가 염두에 베어 그 처럼 뭔가 라이브러리 Simplelogin.php을 수정해야 그것을

을 할 수있는 쉬운 방법이 없다 두렵다

관련 문제