按照之前的教程總綱,這篇是這個系列的第一篇教程,EcShop會員系統模塊下的用戶注冊。Web2.0時代下的網站,擁有一個會員系統是一個標配,注冊自然是這個系統的第一步,也是很簡單一個小功能了。會員注冊的流程基本的思路都是一樣的:
用戶提交注冊信息->JS驗證(前端驗證)->后台驗證->寫入數據
不管什么網站,看着它注冊流程的步驟或源碼多復雜,都離不開這個注冊的大概流程,本來很簡單的注冊之前所以變的復雜,更多只是因為安全的考慮現在的“黑客”無聊的太多了。所以在網站建設的過程中,用戶注冊或者其他用用戶提交信息的地方,最好前端和后台都要做嚴格的驗證。知道了這個注冊流程,也就知道了EcShop用戶注冊的流程了,只是EcShop為了讓網站管理者即使在不懂代碼的情況下,也能完成一些個性化的注冊需要使得它的源碼看似復雜了一些。
進入網站后台我們可以看到EcShop的會員注冊設置也就是添加一些讓用戶添加的選項,這個每一個網站可以根據自己需求來選中了。
說了那么多,下面才是我們二次開發要關注的東西^_^!
第一步:用戶點擊注冊按鈕,瀏覽器向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
本文作者:羅登