[英]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.