‘MODEL_FIELD_FLAG‘ => TRUE,//表单加密开关
‘MODEL_FIELD_NAME_PRE‘ => ‘mlm_‘,//表单加密前缀
‘MODEL_FIELD_EMCODE‘ => ‘md5‘,//加密方式
‘MODEL_FIELD_EMCODE_KEY‘ => ‘GAD@DFVGFasfdgA‘//加密key
2.控制器层
$model 继承基础类 BaseModel BaseRelationModel 任意一个的自定义模型 初始化对象
$data[‘_field‘] = $model->fields_name_encode();//获取数据库加密字段数组
$this->assign(‘data‘, $data);
3.视图表单
<input type="text" name="{$data._field.title}" value="{$data.title}" maxlength="50"/>
4.表单提交控制器
可直接调用$model->create_decode(); 做的工作是解析加密字段至对应数据库字段值 可替代模型create自带方法,
也可手动调用$model->fields_data_decode(I(‘post.‘), true);解密数据字段值
/**
* 解密加密数组
* @param type $data 提交数组
* @param type $flag 过滤数组非数据库字段 开关
* @return type
*/
public function fields_data_decode($data = array(), $flag = false) {
...
}
效果
基础模型类
<?php namespace Root\Model; use Think\Model; /** * 模型基础类 */ class BaseModel extends Model { /** * 解密加密数组 * @param type $data 提交数组 * @param type $flag 过滤数组非数据库字段 开关 * @return type */ public function fields_data_decode($data = array(), $flag = false) { if (!C('MODEL_FIELD_FLAG')) { return $data; } if (empty($data)) { $data = I('post.'); } elseif (is_object($data)) { $data = get_object_vars($data); } $fields = $this->getDbFields(); $table_name = $this->getTableName(); // 检查字段映射 $encode_method = function_exists(C('MODEL_FIELD_EMCODE')) ? C('MODEL_FIELD_EMCODE') : 'md5'; //加密方法 $encode_method_key = C('MODEL_FIELD_EMCODE_KEY') . $table_name; $field_name_pre = C('MODEL_FIELD_NAME_PRE'); if (!empty($fields)) { foreach ($fields as $key => $val) { $key_field = $field_name_pre . call_user_func($encode_method, $val . $encode_method_key); if (isset($data[$key_field])) { $data[$val] = $data[$key_field]; unset($data[$key_field]); } } if ($flag) { foreach ($data as $k => $v) { if (!in_array($k, $fields)) { unset($data[$k]); } } } } return $data; } public function fields_name_encode() { $fields = $this->getDbFields(); $table_name = $this->getTableName(); // 检查字段映射 $encode_method = function_exists(C('MODEL_FIELD_EMCODE')) ? C('MODEL_FIELD_EMCODE') : 'md5'; //加密方法 $encode_method_key = C('MODEL_FIELD_EMCODE_KEY') . $table_name; $field_name_pre = C('MODEL_FIELD_NAME_PRE'); if (!empty($fields)) { foreach ($fields as $key => $val) { if (C('MODEL_FIELD_FLAG')) { $fields[$val] = $field_name_pre . call_user_func($encode_method, $val . $encode_method_key); } else { $fields[$val] = $val; } unset($fields[$key]); } } return $fields; } public function create_decode($data = '', $type = '') { // 如果没有传值默认取POST数据 if (empty($data)) { $data = I('post.'); } elseif (is_object($data)) { $data = get_object_vars($data); } $data = $this->fields_data_decode($data); return $this->create($data, $type); } }