EcShop二次开发系列教程–用户注册

  按照之前的教程总纲,这篇是这个系列的第一篇教程,EcShop会员系统模块下的用户注册。Web2.0时代下的网站,拥有一个会员系统是一个标配,注册自然是这个系统的第一步,也是很简单一个小功能了。会员注册的流程基本的思路都是一样的:

  用户提交注册信息->JS验证(前端验证)->后台验证->写入数据

不管什么网站,看着它注册流程的步骤或源码多复杂,都离不开这个注册的大概流程,本来很简单的注册之前所以变的复杂,更多只是因为安全的考虑现在的“黑客”无聊的太多了。所以在网站建设的过程中,用户注册或者其他用用户提交信息的地方,最好前端和后台都要做严格的验证。知道了这个注册流程,也就知道了EcShop用户注册的流程了,只是EcShop为了让网站管理者即使在不懂代码的情况下,也能完成一些个性化的注册需要使得它的源码看似复杂了一些。

进入网站后台我们可以看到EcShop的会员注册设置也就是添加一些让用户添加的选项,这个每一个网站可以根据自己需求来选中了。

ecshop-user-01

说了那么多,下面才是我们二次开发要关注的东西^_^!

第一步:用户点击注册按钮,浏览器向user.php提交act=register参数,

  后台代码

 1 /* 显示会员注册界面 */
 2 if ($action == 'register')
 3 {
 4     if ((!isset($back_act)||empty($back_act)) && isset($GLOBALS['_SERVER']['HTTP_REFERER']))
 5     {
 6         $back_act = strpos($GLOBALS['_SERVER']['HTTP_REFERER'], 'user.php') ? './index.php' : $GLOBALS['_SERVER']['HTTP_REFERER'];
 7     }
 8     /* 取出注册扩展字段 */
 9     $sql = 'SELECT * FROM ' . $ecs->table('reg_fields') . ' WHERE type < 2 AND display = 1 ORDER BY dis_order, id';
10     $extend_info_list = $db->getAll($sql);
11     $smarty->assign('extend_info_list', $extend_info_list);
12 
13     /* 验证码相关设置 */
14     if ((intval($_CFG['captcha']) & CAPTCHA_REGISTER) && gd_version() > 0)
15     {
16         $smarty->assign('enabled_captcha', 1);
17         $smarty->assign('rand',            mt_rand());
18     }
19 
20     /* 密码提示问题 */
21     $smarty->assign('passwd_questions', $_LANG['passwd_questions']);
22 
23     /* 增加是否关闭注册 */
24     $smarty->assign('shop_reg_closed', $_CFG['shop_reg_closed']);
25 //    $smarty->assign('back_act', $back_act);
26     $smarty->display('user_passport.dwt');
27 }

 

其中 取出注册字段就是后台在网站管理后台会员注册选项设置的字段,EcShop默认必须填写的字段使用户名 邮箱 还有密码,其他的就在后台设置。跳转到user_passport.dwt模版以后如果,会先判断用户注册功能是否关闭,这个使在网站后台系统设置中设置,

  系统设置->商店设置->网店设置

下面有个是否关闭设置,如果设置为是, 那么$_CFG['shop_reg_closed’]的值为1,在模版中将显示 用户注册已经关闭,设置为否,就正常的显示了注册用户需要填写的注册表单。

第二步:填写信息的过程,使用了ajax对用户的信息进行了前端的验证,验证方法在user.js 对用户的信息验证的方法都在这里面,只需要做简单的修改,就能完成我们想要的验证方式。

第三步:通过了前端验证之后,在后台进一步处理(user.php),

  1 /* 注册会员的处理 */
  2 elseif ($action == 'act_register')
  3 {
  4     /* 增加是否关闭注册 */
  5     if ($_CFG['shop_reg_closed'])
  6     {
  7         $smarty->assign('action',     'register');
  8         $smarty->assign('shop_reg_closed', $_CFG['shop_reg_closed']);
  9         $smarty->display('user_passport.dwt');
 10     }
 11     else
 12     {
 13         include_once(ROOT_PATH . 'includes/lib_passport.php');
 14 
 15         $username = isset($_POST['username']) ? trim($_POST['username']) : '';
 16         $password = isset($_POST['password']) ? trim($_POST['password']) : '';
 17     $email    = isset($_POST['email']) ? trim($_POST['email']) : '';
 18     $other['mobile_phone'] = isset($_POST['extend_field5']) ? $_POST['extend_field5'] : '';
 19         $gender    = isset($_POST['sex']) ? trim($_POST['sex']) : '';
 20         $other['msn'] = isset($_POST['extend_field1']) ? $_POST['extend_field1'] : '';
 21         $other['qq'] = isset($_POST['extend_field2']) ? $_POST['extend_field2'] : '';
 22         $other['office_phone'] = isset($_POST['extend_field3']) ? $_POST['extend_field3'] : '';
 23         $other['home_phone'] = isset($_POST['extend_field4']) ? $_POST['extend_field4'] : '';
 24         $sel_question = empty($_POST['sel_question']) ? '' : compile_str($_POST['sel_question']);
 25         $passwd_answer = isset($_POST['passwd_answer']) ? compile_str(trim($_POST['passwd_answer'])) : '';
 26         $back_act = isset($_POST['back_act']) ? trim($_POST['back_act']) : '';
 27 
 28         if(empty($_POST['agreement']))
 29         {
 30             show_message($_LANG['passport_js']['agreement']);
 31         }
 32         if (strlen($username) < 3)
 33         {
 34             show_message($_LANG['passport_js']['username_shorter']);
 35         }
 36 
 37         if (strlen($password) < 6)
 38         {
 39             show_message($_LANG['passport_js']['password_shorter']);
 40         }
 41 
 42         if (strpos($password, ' ') > 0)
 43         {
 44             show_message($_LANG['passwd_balnk']);
 45         }
 46     if(use_name_type($username)){
 47         $email    = $username;
 48     }else{
 49         $other['mobile_phone'] = $username;
 50     }
 51         /* 验证码检查 */
 52         if ((intval($_CFG['captcha']) & CAPTCHA_REGISTER) && gd_version() > 0)
 53         {
 54             if (empty($_POST['captcha']))
 55             {
 56                 show_message($_LANG['invalid_captcha'], $_LANG['sign_up'], 'user.php?act=register', 'error');
 57             }
 58 
 59             /* 检查验证码 */
 60             include_once('includes/cls_captcha.php');
 61 
 62             $validator = new captcha();
 63             if (!$validator->check_word($_POST['captcha']))
 64             {
 65                 show_message($_LANG['invalid_captcha'], $_LANG['sign_up'], 'user.php?act=register', 'error');
 66             }
 67         }
 68         if (register($username, $password, $email, $gender,$other) !== false)
 69         {
 70             /*把新注册用户的扩展信息插入数据库*/
 71             $sql = 'SELECT id FROM ' . $ecs->table('reg_fields') . ' WHERE type = 0 AND display = 1 ORDER BY dis_order, id';   //读出所有自定义扩展字段的id
 72             $fields_arr = $db->getAll($sql);
 73 
 74             $extend_field_str = '';    //生成扩展字段的内容字符串
 75             foreach ($fields_arr AS $val)
 76             {
 77                 $extend_field_index = 'extend_field' . $val['id'];
 78                 if(!empty($_POST[$extend_field_index]))
 79                 {
 80                     $temp_field_content = strlen($_POST[$extend_field_index]) > 100 ? mb_substr($_POST[$extend_field_index], 0, 99) : $_POST[$extend_field_index];
 81                     $extend_field_str .= " ('" . $_SESSION['user_id'] . "', '" . $val['id'] . "', '" . compile_str($temp_field_content) . "'),";
 82                 }
 83             }
 84             $extend_field_str = substr($extend_field_str, 0, -1);
 85 
 86             if ($extend_field_str)      //插入注册扩展数据
 87             {
 88                 $sql = 'INSERT INTO '. $ecs->table('reg_extend_info') . ' (`user_id`, `reg_field_id`, `content`) VALUES' . $extend_field_str;
 89                 $db->query($sql);
 90             }
 91 
 92             /* 写入密码提示问题和答案 */
 93             if (!empty($passwd_answer) && !empty($sel_question))
 94             {
 95                 $sql = 'UPDATE ' . $ecs->table('users') . " SET `passwd_question`='$sel_question', `passwd_answer`='$passwd_answer'  WHERE `user_id`='" . $_SESSION['user_id'] . "'";
 96                 $db->query($sql);
 97             }
 98             /* 判断是否需要自动发送注册邮件 */
 99             if ($GLOBALS['_CFG']['member_email_validate'] && $GLOBALS['_CFG']['send_verify_email'])
100             {
101                 send_regiter_hash($_SESSION['user_id']);
102             }
103             $ucdata = empty($user->ucdata)? "" : $user->ucdata;
104             show_message(sprintf($_LANG['register_success'], $username . $ucdata), array($_LANG['back_up_page'], $_LANG['profile_lnk']), array($back_act, 'user.php'), 'info');
105         }
106         else
107         {
108             $err->show($_LANG['sign_up'], 'user.php?act=register');
109         }
110     }
111 }

这里就是对用户提交信息的后台过滤处理,正确之后跳转首页,错误返回错误页面!EcShop用户注册涉及到的文件代码就这几个,在二次开发的过程根据自己的需求进行修改即可,没有什么比较难以理解的代码,修改起来很简单了!

有问题请加QQ:310810604

本文作者罗登

转载请注明原文链接:http://blog.cnsolomo.com/ld/open/109.html

posted @ 2013-11-05 11:36  罗登  阅读(3295)  评论(0编辑  收藏  举报