当前位置:Gxlcms > PHP教程 > php实现修改密码的方法

php实现修改密码的方法

时间:2021-07-01 10:21:17 帮助过:64人阅读

php实现修改密码的方法:首先进行前端页面布局;然后创建“<h4 class="popup-title">修改密码</h4>”;接着通过js判断密码;最后通过php后台处理修改密码即可。

推荐:《PHP视频教程》

  • 该方法适用于任何品牌的电脑。

PHP实现登录,注册,密码修改

注册,登录,修改密码
1.登录

2.忘记密码

3.免费注册

页面布局

  1. <p id="views" class="views">
  2. <p id="view-login" class="page-view view-login active">
  3. <present name="wxuser">
  4. <p id="wxuser" class="form-group text-center">
  5. <p>
  6. <img src="{sh:$wxuser.headimgurl}">
  7. </p>
  8. <h4 class="nickname">{sh:$wxuser.nickname}</h4>
  9. </p>
  10. </present>
  11. <!--登录-->
  12. <p id="login" class="step">
  13. <h4 class="popup-title login">登录</h4>
  14. <p class="go-forget">忘记密码</p>
  15. <form class="form-horizontal" role="form" type="get">
  16. <p class="form-group">
  17. <label>手机号码</label>
  18. <input type="tel" name="tel" class="form-item" id="tel_num" placeholder="请输入手机号码" value="">
  19. </p>
  20. <p class="form-group">
  21. <label>登录密码</label>
  22. <input type="password" name="password" class="form-item" placeholder="请填写密码">
  23. </p>
  24. <p class="js-help-info error"></p>
  25. </form>
  26. <p class="popup-options">
  27. <button type="button" class="btn btn-block btn-success js-login">确认</button>
  28. </p>
  29. <p class="go-register">免费注册</p>
  30. </p>
  31. <!--注册-->
  32. <p id="register" class="step" style="display:none;">
  33. <h4 class="popup-title">注册账号</h4>
  34. <form role="form" class="form-horizontal">
  35. <p class="form-group">
  36. <label>手机号码</label>
  37. <input type="tel" name="tel" class="form-item" id="tel_num" placeholder="请输入手机号码" value="">
  38. </p>
  39. <p class="form-group form-group-r">
  40. <label>验证码</label>
  41. <button class="btn-sm btn-white js-sms-code" type="button">获取验证码</button>
  42. <input type="text" placeholder="请填写验证码" class="form-item" name="smscode" />
  43. </p>
  44. <p class="form-group">
  45. <label>登录密码</label>
  46. <input type="password" placeholder="设置登录密码" class="form-item" name="password" maxlength="30">
  47. </p>
  48. <p class="form-group">
  49. <label>确认密码</label>
  50. <input type="password" placeholder="确认登录密码" class="form-item" name="re_password" maxlength="30">
  51. </p>
  52. <p class="js-help-info error">
  53. </p>
  54. </form>
  55. <p class="popup-options">
  56. <button type="button" class="btn btn-block btn-success js-register">确认</button>
  57. </p>
  58. <p class="go-login">立即登录</p>
  59. </p>
  60. <!--修改密码-->
  61. <p id="changePwd" class="step" style="display:none;">
  62. <h4 class="popup-title">修改密码</h4>
  63. <form role="form" class="form-horizontal">
  64. <p class="form-group">
  65. <label>手机号码</label>
  66. <input type="tel" name="tel" class="form-item" id="tel_num" placeholder="请输入手机号码" value="">
  67. </p>
  68. <p class="form-group form-group-r">
  69. <label>验证码</label>
  70. <button class="btn-sm btn-white js-sms-excode" type="button">获取验证码</button>
  71. <input type="text" placeholder="请填写验证码" class="form-item" name="smscode" />
  72. </p>
  73. <p class="form-group">
  74. <label>新密码</label>
  75. <input type="password" placeholder="设置登录密码" class="form-item" name="password" maxlength="30">
  76. </p>
  77. <p class="form-group">
  78. <label>确认密码</label>
  79. <input type="password" placeholder="确认登录密码" class="form-item" name="re_password" maxlength="30">
  80. </p>
  81. <p class="js-help-info error">
  82. </p>
  83. </form>
  84. <p class="popup-options">
  85. <button type="button" class="btn btn-block btn-success js-changePwd">确认</button>
  86. </p>
  87. <p class="go-login">立即登录</p>
  88. </p>
  89. </p>
  90. </p>

js处理

  1. <script type="text/javascript">
  2. var tel = '';
  3. $(function() {
  4. var check = {
  5. checkPwd: function(password) {
  6. if (typeof password == 'undefined' || password == '') {
  7. return false;
  8. }
  9. return true;
  10. },
  11. checkSmscode: function(code) {
  12. if (typeof code == 'undefined' || code == '') {
  13. return false;
  14. }
  15. return true;
  16. },
  17. validTel: function(value) {
  18. return /^((\+86)|(86))?(1)\d{10}$/.test('' + value);
  19. }
  20. }
  21. //登录
  22. $(".js-login").click(function() {
  23. var tel = $("#login").find("input[name='tel']").val();
  24. if (!check.validTel(tel)) {
  25. $('.js-help-info').html('请输入正确的手机号'); //**提示下个页面还有
  26. return false;
  27. }
  28. var password = $("#login").find("input[name='password']").val();
  29. if (!check.checkPwd(password)) {
  30. $('.js-help-info').html('请输入密码');
  31. return false;
  32. }
  33. $('.js-login').attr("disabled", "disabled");
  34. $.ajax({
  35. url: "{sh::U('Home/userLogin')}",
  36. type: 'POST',
  37. dataType: "json",
  38. data: {
  39. tel: tel,
  40. password: password
  41. },
  42. success: function(response) {
  43. if (response.result) {
  44. location.href = response.href;
  45. } else {
  46. setTimeout(function() {
  47. $('.js-login').removeAttr("disabled");
  48. }, 500);
  49. $('.js-help-info').html(response.error);
  50. }
  51. },
  52. error: function() {
  53. $('.js-help-info').html("请求失败");
  54. }
  55. });
  56. });
  57. //注册
  58. $(".js-register").click(function() {
  59. var tel = $("#register").find("input[name='tel']").val();
  60. if (!check.validTel(tel)) {
  61. $('.js-help-info').html('请输入正确的手机号'); //**提示下个页面还有
  62. return false;
  63. }
  64. var password = $("#register input[name='password']").val();
  65. var smscode = $("#register input[name='smscode']").val();
  66. var re_password = $("#register input[name='re_password']").val();
  67. if (!check.checkSmscode(smscode)) {
  68. $('.js-help-info').html('请输入验证码');
  69. return false;
  70. }
  71. if (!check.checkPwd(password)) {
  72. $('.js-help-info').html('请输入登录密码');
  73. return false;
  74. }
  75. if (!check.checkPwd(re_password)) {
  76. $('.js-help-info').html('请输入确认密码');
  77. return false;
  78. } else if (password != re_password) {
  79. $('.js-help-info').html('两次输入的密码不一致');
  80. return false;
  81. }
  82. $('.js-login').attr("disabled", "disabled");
  83. $.ajax({
  84. url: "{sh::U('Home/userRegister')}",
  85. type: 'POST',
  86. dataType: "json",
  87. data: {
  88. tel: tel,
  89. password: password,
  90. smscode: smscode
  91. },
  92. success: function(response) {
  93. if (response.result) {
  94. location.href = response.href;
  95. } else {
  96. setTimeout(function() {
  97. $('.js-login').removeAttr("disabled");
  98. }, 500);
  99. $('.js-help-info').html(response.error);
  100. }
  101. },
  102. error: function() {
  103. $('.js-help-info').html("请求失败");
  104. }
  105. });
  106. });
  107. //发送验证码
  108. $('.js-sms-code').click(function() {
  109. var tel = $('#register #tel_num').val();
  110. if (!check.validTel(tel)) {
  111. $('.js-help-info').html('请输入正确的手机号'); //**提示下个页面还有
  112. return false;
  113. }
  114. // 检测是否已经注册
  115. $.ajax({
  116. url: "{sh::U('Home/checkTel')}",
  117. type: 'POST',
  118. dataType: "json",
  119. async: false,
  120. data: {
  121. tel: tel
  122. },
  123. success: function(json) {
  124. checkRes = json.status;
  125. },
  126. error: function(json) {
  127. $('.js-help-info').html("发送失败");
  128. }
  129. });
  130. if (checkRes == 1) {
  131. $('.js-help-info').html("已是注册用户");return false;
  132. }
  133. if (checkRes == 3) {
  134. $('.js-help-info').html("错误的请求");return false;
  135. }
  136. $(this).attr("disabled", "disabled").html("<span style='color:#666'><span id='countdown'>60</span>s 后再试</span>");
  137. countdown();
  138. $.ajax({
  139. url: "{sh::U('Home/sendSmscode')}",
  140. type: 'POST',
  141. dataType: "json",
  142. data: {
  143. tel: tel
  144. },
  145. success: function() {},
  146. error: function() {
  147. $('.js-help-info').html("发送失败");
  148. }
  149. });
  150. });
  151. //修改密码
  152. $('.go-forget').click(function() {
  153. var tel = $('#login #tel_num').val();
  154. $("#login").hide();
  155. $("#register").hide();
  156. $("#changePwd").show();
  157. $("#changePwd #tel_num").val(tel).focus();
  158. $('.js-help-info').html('');
  159. });
  160. //免费注册
  161. $('.go-register').click(function() {
  162. var tel = $('#login #tel_num').val();
  163. $("#login").hide();
  164. $("#changePwd").hide();
  165. $("#register").show();
  166. $("#register #tel_num").val(tel).focus();
  167. $('.js-help-info').html('');
  168. });
  169. //立即登录
  170. $('#changePwd .go-login').click(function() {
  171. var tel = $('#changePwd #tel_num').val();
  172. $("#register").hide();
  173. $("#changePwd").hide();
  174. $("#login").show();
  175. $("#login #tel_num").val(tel).focus();
  176. $('.js-help-info').html('');
  177. });
  178. //立即登录
  179. $('#register .go-login').click(function() {
  180. var tel = $('#register #tel_num').val();
  181. $("#register").hide();
  182. $("#changePwd").hide();
  183. $("#login").show();
  184. $("#login #tel_num").val(tel).focus();
  185. $('.js-help-info').html('');
  186. });
  187. $('.js-changePwd').click(function() {
  188. var tel = $("#changePwd").find("input[name='tel']").val();
  189. if (!check.validTel(tel)) {
  190. $('.js-help-info').html('请输入正确的手机号'); //**提示下个页面还有
  191. return false;
  192. }
  193. var password = $("#changePwd input[name='password']").val();
  194. var smscode = $("#changePwd input[name='smscode']").val();
  195. var re_password = $("#changePwd input[name='re_password']").val();
  196. if (!check.checkSmscode(smscode)) {
  197. $('#changePwd .js-help-info').html('请输入验证码');
  198. return false;
  199. }
  200. if (!check.checkPwd(password)) {
  201. $('#changePwd .js-help-info').html('请输入新密码');
  202. return false;
  203. }
  204. if (!check.checkPwd(re_password)) {
  205. $('#changePwd .js-help-info').html('请输入确认密码');
  206. return false;
  207. } else if (password != re_password) {
  208. $('#changePwd .js-help-info').html('两次输入的密码不一致');
  209. return false;
  210. }
  211. $.ajax({
  212. url: "{sh::U('Home/changePwd')}",
  213. type: "POST",
  214. dataType: "json",
  215. data: {
  216. tel: tel,
  217. password: password,
  218. smscode: smscode
  219. },
  220. success: function(response) {
  221. if (response.result) {
  222. location.href = response.href;
  223. } else {
  224. setTimeout(function() {
  225. $('.js-login').removeAttr("disabled");
  226. }, 500);
  227. $('.js-help-info').html(response.error);
  228. }
  229. },
  230. error: function() {
  231. $('.js-help-info').html("请求失败");
  232. }
  233. });
  234. });
  235. //发送短信修改密码
  236. $('.js-sms-excode').click(function() {
  237. var tel = $('#changePwd #tel_num').val();
  238. if (!check.validTel(tel)) {
  239. $('.js-help-info').html('请输入正确的手机号'); //**提示下个页面还有
  240. return false;
  241. }
  242. // 检测是否已经注册
  243. $.ajax({
  244. url: "{sh::U('Home/checkTel')}",
  245. type: 'POST',
  246. dataType: "json",
  247. async: false,
  248. data: {
  249. tel: tel
  250. },
  251. success: function(json) {
  252. checkRes = json.status;
  253. },
  254. error: function(json) {
  255. $('.js-help-info').html("发送失败");
  256. }
  257. });
  258. if (checkRes == 2) {
  259. $('.js-help-info').html("号码尚未注册");return false;
  260. }
  261. if (checkRes == 3) {
  262. $('.js-help-info').html("错误的请求");return false;
  263. }
  264. $(this).attr("disabled", "disabled").html("<span style='color:#666'><span id='countdown'>60</span>s 后再试</span>");
  265. countdown();
  266. $.ajax({
  267. url: "{sh::U('Home/sendSmsexcode')}",
  268. type: 'POST',
  269. dataType: "json",
  270. data: {
  271. tel: tel
  272. },
  273. success: function(data) {},
  274. error: function() {
  275. $('.js-help-info').html("请求失败");
  276. }
  277. });
  278. });
  279. });
  280. function countdown() { // 递归 验证码倒计时
  281. setTimeout(function() {
  282. var time = $("#countdown").text();
  283. if (time == 1) {
  284. $('.js-sms-code').removeAttr("disabled");
  285. $('.js-sms-code').html("发送验证码");
  286. $('.js-sms-excode').removeAttr("disabled");
  287. $('.js-sms-excode').html("发送验证码");
  288. } else {
  289. $("#countdown").text(time - 1);
  290. countdown();
  291. }
  292. }, 1000);
  293. }
  294. </script>

php后台处理

  1. //用户登录
  2. public function userLogin() {
  3. if(IS_AJAX && !$this->member) {
  4. $tel = $this->_post('tel', 'trim');
  5. $password = $this->_post('password', 'trim,md5');
  6. $member = M('Member')->where(array('tel' => $tel))->find();
  7. if ($member && $member['password'] === $password) {
  8. //检测是否存在微信用户需要绑定
  9. if ($member['wxuser_id'] == 0 && $this->wxuser) {
  10. M('Member')->where(array('id' => $member['id']))->save(array('wxuser_id' => $this->wxuser_id));
  11. }
  12. $href = session(LASTREQUEST);
  13. session(MEMBER, $member['id']);
  14. session(LASTREQUEST, null);
  15. $this->ajaxReturn(array('result' => true, 'href' => $href ? $href : U('Member/index')));
  16. } else {
  17. if (empty($member)) {
  18. $this->ajaxReturn(array('result' => false, 'error' => '手机号尚未注册.'));
  19. } else {
  20. $this->ajaxReturn(array('result' => false, 'error' => '密码不正确.'));
  21. }
  22. }
  23. } else {
  24. $this->ajaxReturn(array('result' => false, 'error' => '非法请求.'));
  25. }
  26. }
  27. // 用户退出
  28. public function userLogout() {
  29. session(WXUSER, null);
  30. session(MEMBER, null);
  31. $this->success('退出成功',U('Store/Member/index'));
  32. }
  33. // 用户注册
  34. public function userRegister() {
  35. $tel = $this->_post('tel', 'trim');
  36. $password = $this->_post('password', 'trim,md5');
  37. $smscode = $this->_post('smscode', 'trim');
  38. $session_smscode = session($this->smscode);
  39. $user_exit = M('Member')->where(array('tel' => $tel))->find();
  40. if (!preg_match("/1[3458]{1}\d{9}$/", $tel) && $user_exit) {
  41. $this->ajaxReturn(array('result' => false, 'error' => '手机号不合法'));
  42. }
  43. $memberModel = M('Member');
  44. // 检测是否已注册
  45. $member = $memberModel-> where(array('tel' =>$tel,'status'=>1))->find();
  46. if (!empty($member)) {
  47. $this->ajaxReturn(array('result' => false, 'error' => '已是注册用户'));
  48. }
  49. if (time() > $session_smscode['time'] || $smscode != $session_smscode['code']) {
  50. $this->ajaxReturn(array('result' => false, 'error' => '验证码不正确')); //--调试,先把验证功能关闭
  51. }
  52. $data = array('tel' => $tel, 'password' => $password, 'wxuser_id' => intval($this->wxuser_id), 'addtime' => time());
  53. $insert_id = $memberModel->add($data);
  54. if ($insert_id) {
  55. $href = session(LASTREQUEST);
  56. session(MEMBER, $insert_id); //*****只是一个id值
  57. $this->ajaxReturn(array('result' => true, 'href' => $href ? $href : U('Member/index')));
  58. } else {
  59. $this->ajaxReturn(array('result' => false, 'error' => '操作失败', 'msg' => M('Member')->getError()));
  60. }
  61. }
  62. //用户更改密码
  63. public function changePwd(){
  64. $tel = $this->_post('tel','trim');
  65. $password = $this ->_post('password','trim');
  66. $smscode = $this ->_post('smscode','trim');
  67. $session_smscode = session($this ->smscode);
  68. if (time() > $session_smscode['time'] || $smscode != $session_smscode['code']) {
  69. $this->ajaxReturn(array('result' => false, 'error' => '验证码不正确')); //--调试成功
  70. }
  71. $data = array('password' => md5($password), 'addtime' => time());
  72. $memberModel = M('Member');
  73. // 检测是否已注册
  74. $member = $memberModel-> where(array('tel' =>$tel,'status'=>1))->find();
  75. if (empty($member)) {
  76. $this->ajaxReturn(array('result' => false, 'error' => '号码尚未注册'));
  77. }
  78. if ($memberModel->where(array('tel'=> $tel))->save($data)) {
  79. $href = session(LASTREQUEST);
  80. session(MEMBER, $member['id']);
  81. $this->ajaxReturn(array('result' => true, 'href' => $href ? $href : U('Member/index')));
  82. } else {
  83. $this->ajaxReturn(array('result' => false, 'error' => '操作失败', 'msg' => M('Member')->getError()));
  84. }
  85. }
  86. // ajax检测号码是否注册
  87. public function checkTel() {
  88. $tel = $this->_post('tel', 'trim');
  89. if (IS_AJAX && preg_match("/1[3458]{1}\d{9}$/",$tel)) {
  90. $memberModel = M('Member');
  91. $member = $memberModel->where(array('tel'=>$tel,'status'=>1))->find();
  92. if (!empty($member)) {
  93. $this->ajaxReturn(array('status' => 1, 'info' => '已注册'));
  94. } else {
  95. $this->ajaxReturn(array('status' => 2, 'info' => '未注册'));
  96. }
  97. } else {
  98. $this->ajaxReturn(array('status' => 3, 'info' => '错误的请求'));
  99. }
  100. }
  101. //发送注册验证码
  102. public function sendSmscode() {
  103. session($this->smstime, null);
  104. $smstime = session($this->smstime);
  105. $tel = $this->_post('tel', 'trim');
  106. if (IS_AJAX && (!$smstime || time() > $smstime) && preg_match("/1[3458]{1}\d{9}$/",$tel)) {
  107. $smscode = rand(1000, 9999);
  108. //发送【阿里大鱼】的验证码
  109. require LIB_PATH . 'ORG/Taobao-sdk-php/TopSdk.php';
  110. $c = new TopClient;
  111. $c->appkey = '23307560'; // 原23294081
  112. $c->secretKey = '21ef24dd4c51e20693c5db0983c433e7'; // 原0402169f466d8fed780e7f07edd25177
  113. $req = new AlibabaAliqinFcSmsNumSendRequest;
  114. $req->setSmsType("normal");
  115. $req->setSmsFreeSignName("注册验证");
  116. $req->setSmsParam('{"code":"'. $smscode .'","product":"【多多助店宝】"}');
  117. $req->setRecNum("{$tel}");
  118. $req->setSmsTemplateCode("SMS_5056863");
  119. $resp = $c->execute($req);
  120. if(!$resp->code) {
  121. //设置发送限制时间
  122. session($this->smstime, time() + 50);
  123. //设置验证码5分钟内有效
  124. session($this->smscode, array('code' => $smscode, 'time' => time() + 600));
  125. } else {
  126. //发送失败写入日志文件
  127. $log = date('Y-m-d H:i:s') . " 发送失败 sub_code:{$resp->sub_code} sub_msg:{$resp->sub_msg}" . PHP_EOL;
  128. file_put_contents(RUNTIME_PATH . 'Log/smscode.log', $log, FILE_APPEND);
  129. }
  130. $this->ajaxReturn(array('result' => !$resp->code));
  131. } else {
  132. $this->ajaxReturn(array('result' => false, 'error' => '错误的请求'));
  133. }
  134. }
  135. //发送修改密码验证码
  136. public function sendSmsexcode(){
  137. session($this->smstime, null);
  138. $smstime = session($this->smstime);
  139. $tel = $this->_post('tel', 'trim');
  140. if (IS_AJAX && (!$smstime || time() > $smstime) && preg_match("/1[3458]{1}\d{9}$/",$tel)) {
  141. $smscode = rand(1000, 9999);
  142. //发送【阿里大鱼】的验证码
  143. require LIB_PATH . 'ORG/Taobao-sdk-php/TopSdk.php';
  144. $c = new TopClient;
  145. $c->appkey = '23307560'; // 原23294081
  146. $c->secretKey = '21ef24dd4c51e20693c5db0983c433e7'; // 原0402169f466d8fed780e7f07edd25177
  147. $req = new AlibabaAliqinFcSmsNumSendRequest;
  148. $req->setSmsType("normal");
  149. $req->setSmsFreeSignName("变更验证"); //短信签名固定,不可以换其他字
  150. $req->setSmsParam('{"code":"'. $smscode .'","product":"【多多助店宝】"}');
  151. $req->setRecNum("{$tel}");
  152. $req->setSmsTemplateCode("SMS_5056861");
  153. $resp = $c->execute($req);
  154. if(!$resp->code) {
  155. //设置发送限制时间
  156. session($this->smstime, time() + 50);
  157. //设置验证码5分钟内有效
  158. session($this->smscode, array('code' => $smscode, 'time' => time() + 600));
  159. } else {
  160. //发送失败写入日志文件
  161. $log = date('Y-m-d H:i:s') . " 发送失败 sub_code:{$resp->sub_code} sub_msg:{$resp->sub_msg}" . PHP_EOL;
  162. file_put_contents(RUNTIME_PATH . 'Log/smscode.log', $log, FILE_APPEND);
  163. }
  164. $this->ajaxReturn(array('result' => !$resp->code));
  165. } else {
  166. $this->ajaxReturn(array('result' => false, 'error' => '错误的请求'));
  167. }
  168. }

小结:

1.注册与修改密码用到了短信验证。
阿里大鱼值得信赖。
2.安全起见,前端ajax验证。后端亦进行验证。
3.流程合理,切换自如。
4.功能全面,登录,注册,密码修改齐全。

以上就是php实现修改密码的方法的详细内容,更多请关注gxlcms其它相关文章!

人气教程排行