如何通过在PHP CodeIgniter中连接用户信息来生成唯一键?

时间:2015-08-08 07:28:09

标签: php mysql codeigniter

我想根据用户提供的信息创建密钥。例如,如果用户插入他们的姓名,电话,国家和其他一些信息,那么我的代码应该连接一些字段并将一个唯一的密钥存储到我有字段密钥的同一个用户表中。例如:

  

JACK-691-INDIA-10-001

所以在这里,杰克将是用户插入的名称,691将是国家代码印度是国家名称,10是用户名,001是他的数据库ID。

在模型中我有一个函数get_new(),我在其中初始化数组中的所有变量,我想连接一些变量并形成一个字符串(key)以保存在关键字段中。

模特:

public function get_new(){
    $user = new stdClass();

//          $user->id = '';
    $user->sip_username='';
    $user->sip_password='';
    $user->key='';
    $user->allocation_block='';
    $user->name='';
    $user->email = '';      
    $user->password = '';
    $user->phone=''; 
    $user->user_num=''; 
    $user->address = '';
    $user->status = '';
    $user->country=''; 
    $user->created = '';
    $user->modified  = '';
    $user->balance = '';
    return $user;
    $this->session->set_userdata($data);

}

Controller Edit方法:

    public function edit ($id = NULL)
{
    // Fetch a user or set a new one
    if ($id) {
        $this->data['user'] = $this->reseller_m->get($id);
        count($this->data['user']) || $this->data['errors'][] = 'User could not be found';
    }
    else {
        $this->data['user'] = $this->reseller_m->get_new();
    }

    // Set up the form
    $rules = $this->reseller_m->rules_admin;
    $id || $rules['password']['rules'] .= '|required';
    $this->form_validation->set_rules($rules);

    // Process the form
    if ($this->form_validation->run() == TRUE) {

$ data = $ this-> reseller_m-> array_from_post(数组(' sip_username',' sip_password',' key',' allocation_block''名称''电子邮件''密码''电话''平衡&#39 ;, ' user_num''地址''国家''创建''改性''状态& #39));

        $data['password'] = $this->reseller_m->hash($data['password']);


        $key=$this->reseller_m->save($data, $id);

         $this->db->insert_id();

/ * $ key = $ this-> reseller_m-> save($ data,$ key);             $ data [' key'] = $ this-> reseller_m-> insert_item($ data [' name']。$ data [' phone']) ;

* /

        for($i=1; $i<=$data['user_num'];$i++)
            {
            $userdata=array('key'=>$key);
        // here users is taken name of user table with retailer_id is field
            $this->user_m->save($userdata,$id);
             }



        redirect('admin/reseller');
    }

    // Load the view
    $this->data['subview'] = 'admin/reseller/edit';
    $this->load->view('admin/_layout_main', $this->data);
}

3 个答案:

答案 0 :(得分:1)

为什么不,

 sha256( serialize( $user ));

不是说这样做一定是个好主意,但是。

答案 1 :(得分:1)

您可以使用任何散列函数为给定字符串生成唯一的散列。

一些受欢迎的人

您可以使用hash()尝试各种哈希函数。请注意,这些都不是要解密的。

答案 2 :(得分:1)

是的,您可以将所需的值传递给模型,连接并在那里创建字符串,将其存储到数据库中。

从控制器中,您可以生成密钥,然后将其传递给模型:

$key = 'JACK-691-INDIA-10-001' // you have to concatenate the proper string, what is shown here is just as an example.
$this->load->model('users'); //the model in which the get_new function exsists
$this->users->get_new($key);

型号:

public function get_new($key) {
    $user = new stdClass();

//          $user->id = '';
    $user->sip_username='';
    $user->sip_password='';
    $user->key='';
    $user->allocation_block='';
    $user->name='';
    $user->email = '';      
    $user->password = '';
    $user->phone=''; 
    $user->user_num=''; 
    $user->address = '';
    $user->status = '';
    $user->country=''; 
    $user->created = '';
    $user->modified  = '';
    $user->balance = '';
    return $user;
    $this->session->set_userdata($data);

}