[英]How to generate unique keys by concatenating user information in PHP CodeIgniter?
我想根據用戶提供的信息創建密鑰。 例如,如果用戶插入了他們的姓名,電話,國家和其他信息,那么我的代碼應連接一些字段,並將唯一鍵存儲到我具有字段鍵的同一用戶表中。 例:
JACK-691-INDIA-10-001
因此,在這里,傑克是用戶插入的名稱,691是國家/地區代碼,印度是國家/地區名稱,10是用戶名,001是他的數據庫ID。
在模型中,我有一個函數get_new()
,在其中初始化數組中的所有變量,並希望連接一些變量並形成一個要保存在鍵字段中的字符串(鍵)。
該模型:
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);
}
控制器編輯方法:
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(array('sip_username','sip_password','key','allocation_block','name','email','password','phone','balance', 'user_num','address','country','created','modified','status'));
$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);
}
為什么不,
sha256( serialize( $user ));
不是說這樣做一定是個好主意,而是。
是的,您可以將所需的值傳遞給模型,進行連接並在那里創建字符串,然后將其存儲到數據庫。
您可以從控制器生成密鑰,然后將其傳遞給model:
$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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.