PHP实现的登录,注册及密码修改功能分析

2025-05-29 0 102

本文实例讲述了php实现登录注册密码修改功能的方法。分享给大家供大家参考,具体如下:

这里介绍注册登录,修改密码的界面布局与功能实现:

1.登录

PHP实现的登录,注册及密码修改功能分析

2.忘记密码

PHP实现的登录,注册及密码修改功能分析

3.免费注册

PHP实现的登录,注册及密码修改功能分析

页面布局:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90
<div id="views" class="views">

<div id="view-login" class="page-view view-login active">

<present name="wxuser">

<div id="wxuser" class="form-group text-center">

<div>

<img src="{sh:$wxuser.headimgurl}">

</div>

<h4 class="nickname">{sh:$wxuser.nickname}</h4>

</div>

</present>

<!--登录-->

<div id="login" class="step">

<h4 class="popup-title login">登录</h4>

<div class="go-forget">忘记密码</div>

<form class="form-horizontal" role="form" type="get">

<div class="form-group">

<label>手机号码</label>

<input type="tel" name="tel" class="form-item" id="tel_num" placeholder="请输入手机号码" value="">

</div>

<div class="form-group">

<label>登录密码</label>

<input type="password" name="password" class="form-item" placeholder="请填写密码">

</div>

<div class="js-help-info error"></div>

</form>

<div class="popup-options">

<button type="button" class="btn btn-block btn-success js-login">确认</button>

</div>

<div class="go-register">免费注册</div>

</div>

<!--注册-->

<div id="register" class="step" style="display:none;">

<h4 class="popup-title">注册账号</h4>

<form role="form" class="form-horizontal">

<div class="form-group">

<label>手机号码</label>

<input type="tel" name="tel" class="form-item" id="tel_num" placeholder="请输入手机号码" value="">

</div>

<div class="form-group form-group-r">

<label>验证码</label>

<button class="btn-sm btn-white js-sms-code" type="button">获取验证码</button>

<input type="text" placeholder="请填写验证码" class="form-item" name="smscode" />

</div>

<div class="form-group">

<label>登录密码</label>

<input type="password" placeholder="设置登录密码" class="form-item" name="password" maxlength="30">

</div>

<div class="form-group">

<label>确认密码</label>

<input type="password" placeholder="确认登录密码" class="form-item" name="re_password" maxlength="30">

</div>

<div class="js-help-info error">

</div>

</form>

<div class="popup-options">

<button type="button" class="btn btn-block btn-success js-register">确认</button>

</div>

<div class="go-login">立即登录</div>

</div>

<!--修改密码-->

<div id="changepwd" class="step" style="display:none;">

<h4 class="popup-title">修改密码</h4>

<form role="form" class="form-horizontal">

<div class="form-group">

<label>手机号码</label>

<input type="tel" name="tel" class="form-item" id="tel_num" placeholder="请输入手机号码" value="">

</div>

<div class="form-group form-group-r">

<label>验证码</label>

<button class="btn-sm btn-white js-sms-excode" type="button">获取验证码</button>

<input type="text" placeholder="请填写验证码" class="form-item" name="smscode" />

</div>

<div class="form-group">

<label>新密码</label>

<input type="password" placeholder="设置登录密码" class="form-item" name="password" maxlength="30">

</div>

<div class="form-group">

<label>确认密码</label>

<input type="password" placeholder="确认登录密码" class="form-item" name="re_password" maxlength="30">

</div>

<div class="js-help-info error">

</div>

</form>

<div class="popup-options">

<button type="button" class="btn btn-block btn-success js-changepwd">确认</button>

</div>

<div class="go-login">立即登录</div>

</div>

</div>

</div>

js处理:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294
<script type="text/javascript">

var tel = '';

$(function() {

var check = {

checkpwd: function(password) {

if (typeof password == 'undefined' || password == '') {

return false;

}

return true;

},

checksmscode: function(code) {

if (typeof code == 'undefined' || code == '') {

return false;

}

return true;

},

validtel: function(value) {

return /^((\\+86)|(86))?(1)\\d{10}$/.test('' + value);

}

}

//登录

$(".js-login").click(function() {

var tel = $("#login").find("input[name='tel']").val();

if (!check.validtel(tel)) {

$('.js-help-info').html('请输入正确的手机号'); //**提示下个页面还有

return false;

}

var password = $("#login").find("input[name='password']").val();

if (!check.checkpwd(password)) {

$('.js-help-info').html('请输入密码');

return false;

}

$('.js-login').attr("disabled", "disabled");

$.ajax({

url: "{sh::u('home/userlogin')}",

type: 'post',

datatype: "json",

data: {

tel: tel,

password: password

},

success: function(response) {

if (response.result) {

location.href = response.href;

} else {

settimeout(function() {

$('.js-login').removeattr("disabled");

}, 500);

$('.js-help-info').html(response.error);

}

},

error: function() {

$('.js-help-info').html("请求失败");

}

});

});

//注册

$(".js-register").click(function() {

var tel = $("#register").find("input[name='tel']").val();

if (!check.validtel(tel)) {

$('.js-help-info').html('请输入正确的手机号'); //**提示下个页面还有

return false;

}

var password = $("#register input[name='password']").val();

var smscode = $("#register input[name='smscode']").val();

var re_password = $("#register input[name='re_password']").val();

if (!check.checksmscode(smscode)) {

$('.js-help-info').html('请输入验证码');

return false;

}

if (!check.checkpwd(password)) {

$('.js-help-info').html('请输入登录密码');

return false;

}

if (!check.checkpwd(re_password)) {

$('.js-help-info').html('请输入确认密码');

return false;

} else if (password != re_password) {

$('.js-help-info').html('两次输入的密码不一致');

return false;

}

$('.js-login').attr("disabled", "disabled");

$.ajax({

url: "{sh::u('home/userregister')}",

type: 'post',

datatype: "json",

data: {

tel: tel,

password: password,

smscode: smscode

},

success: function(response) {

if (response.result) {

location.href = response.href;

} else {

settimeout(function() {

$('.js-login').removeattr("disabled");

}, 500);

$('.js-help-info').html(response.error);

}

},

error: function() {

$('.js-help-info').html("请求失败");

}

});

});

//发送验证码

$('.js-sms-code').click(function() {

var tel = $('#register #tel_num').val();

if (!check.validtel(tel)) {

$('.js-help-info').html('请输入正确的手机号'); //**提示下个页面还有

return false;

}

// 检测是否已经注册

$.ajax({

url: "{sh::u('home/checktel')}",

type: 'post',

datatype: "json",

async: false,

data: {

tel: tel

},

success: function(json) {

checkres = json.status;

},

error: function(json) {

$('.js-help-info').html("发送失败");

}

});

if (checkres == 1) {

$('.js-help-info').html("已是注册用户");return false;

}

if (checkres == 3) {

$('.js-help-info').html("错误的请求");return false;

}

$(this).attr("disabled", "disabled").html("<span style='color:#666'><span id='countdown'>60</span>s 后再试</span>");

countdown();

$.ajax({

url: "{sh::u('home/sendsmscode')}",

type: 'post',

datatype: "json",

data: {

tel: tel

},

success: function() {},

error: function() {

$('.js-help-info').html("发送失败");

}

});

});

//修改密码

$('.go-forget').click(function() {

var tel = $('#login #tel_num').val();

$("#login").hide();

$("#register").hide();

$("#changepwd").show();

$("#changepwd #tel_num").val(tel).focus();

$('.js-help-info').html('');

});

//免费注册

$('.go-register').click(function() {

var tel = $('#login #tel_num').val();

$("#login").hide();

$("#changepwd").hide();

$("#register").show();

$("#register #tel_num").val(tel).focus();

$('.js-help-info').html('');

});

//立即登录

$('#changepwd .go-login').click(function() {

var tel = $('#changepwd #tel_num').val();

$("#register").hide();

$("#changepwd").hide();

$("#login").show();

$("#login #tel_num").val(tel).focus();

$('.js-help-info').html('');

});

//立即登录

$('#register .go-login').click(function() {

var tel = $('#register #tel_num').val();

$("#register").hide();

$("#changepwd").hide();

$("#login").show();

$("#login #tel_num").val(tel).focus();

$('.js-help-info').html('');

});

$('.js-changepwd').click(function() {

var tel = $("#changepwd").find("input[name='tel']").val();

if (!check.validtel(tel)) {

$('.js-help-info').html('请输入正确的手机号'); //**提示下个页面还有

return false;

}

var password = $("#changepwd input[name='password']").val();

var smscode = $("#changepwd input[name='smscode']").val();

var re_password = $("#changepwd input[name='re_password']").val();

if (!check.checksmscode(smscode)) {

$('#changepwd .js-help-info').html('请输入验证码');

return false;

}

if (!check.checkpwd(password)) {

$('#changepwd .js-help-info').html('请输入新密码');

return false;

}

if (!check.checkpwd(re_password)) {

$('#changepwd .js-help-info').html('请输入确认密码');

return false;

} else if (password != re_password) {

$('#changepwd .js-help-info').html('两次输入的密码不一致');

return false;

}

$.ajax({

url: "{sh::u('home/changepwd')}",

type: "post",

datatype: "json",

data: {

tel: tel,

password: password,

smscode: smscode

},

success: function(response) {

if (response.result) {

location.href = response.href;

} else {

settimeout(function() {

$('.js-login').removeattr("disabled");

}, 500);

$('.js-help-info').html(response.error);

}

},

error: function() {

$('.js-help-info').html("请求失败");

}

});

});

//发送短信修改密码

$('.js-sms-excode').click(function() {

var tel = $('#changepwd #tel_num').val();

if (!check.validtel(tel)) {

$('.js-help-info').html('请输入正确的手机号'); //**提示下个页面还有

return false;

}

// 检测是否已经注册

$.ajax({

url: "{sh::u('home/checktel')}",

type: 'post',

datatype: "json",

async: false,

data: {

tel: tel

},

success: function(json) {

checkres = json.status;

},

error: function(json) {

$('.js-help-info').html("发送失败");

}

});

if (checkres == 2) {

$('.js-help-info').html("号码尚未注册");return false;

}

if (checkres == 3) {

$('.js-help-info').html("错误的请求");return false;

}

$(this).attr("disabled", "disabled").html("<span style='color:#666'><span id='countdown'>60</span>s 后再试</span>");

countdown();

$.ajax({

url: "{sh::u('home/sendsmsexcode')}",

type: 'post',

datatype: "json",

data: {

tel: tel

},

success: function(data) {},

error: function() {

$('.js-help-info').html("请求失败");

}

});

});

});

function countdown() { // 递归 验证码倒计时

settimeout(function() {

var time = $("#countdown").text();

if (time == 1) {

$('.js-sms-code').removeattr("disabled");

$('.js-sms-code').html("发送验证码");

$('.js-sms-excode').removeattr("disabled");

$('.js-sms-excode').html("发送验证码");

} else {

$("#countdown").text(time - 1);

countdown();

}

}, 1000);

}

</script>

php后台处理:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168
//用户登录

public function userlogin() {

if(is_ajax && !$this->member) {

$tel = $this->_post('tel', 'trim');

$password = $this->_post('password', 'trim,md5');

$member = m('member')->where(array('tel' => $tel))->find();

if ($member && $member['password'] === $password) {

//检测是否存在微信用户需要绑定

if ($member['wxuser_id'] == 0 && $this->wxuser) {

m('member')->where(array('id' => $member['id']))->save(array('wxuser_id' => $this->wxuser_id));

}

$href = session(lastrequest);

session(member, $member['id']);

session(lastrequest, null);

$this->ajaxreturn(array('result' => true, 'href' => $href ? $href : u('member/index')));

} else {

if (empty($member)) {

$this->ajaxreturn(array('result' => false, 'error' => '手机号尚未注册.'));

} else {

$this->ajaxreturn(array('result' => false, 'error' => '密码不正确.'));

}

}

} else {

$this->ajaxreturn(array('result' => false, 'error' => '非法请求.'));

}

}

// 用户退出

public function userlogout() {

session(wxuser, null);

session(member, null);

$this->success('退出成功',u('store/member/index'));

}

// 用户注册

public function userregister() {

$tel = $this->_post('tel', 'trim');

$password = $this->_post('password', 'trim,md5');

$smscode = $this->_post('smscode', 'trim');

$session_smscode = session($this->smscode);

$user_exit = m('member')->where(array('tel' => $tel))->find();

if (!preg_match("/1[3458]{1}\\d{9}$/", $tel) && $user_exit) {

$this->ajaxreturn(array('result' => false, 'error' => '手机号不合法'));

}

$membermodel = m('member');

// 检测是否已注册

$member = $membermodel-> where(array('tel' =>$tel,'status'=>1))->find();

if (!empty($member)) {

$this->ajaxreturn(array('result' => false, 'error' => '已是注册用户'));

}

if (time() > $session_smscode['time'] || $smscode != $session_smscode['code']) {

$this->ajaxreturn(array('result' => false, 'error' => '验证码不正确')); //--调试,先把验证功能关闭

}

$data = array('tel' => $tel, 'password' => $password, 'wxuser_id' => intval($this->wxuser_id), 'addtime' => time());

$insert_id = $membermodel->add($data);

if ($insert_id) {

$href = session(lastrequest);

session(member, $insert_id); //*****只是一个id值

$this->ajaxreturn(array('result' => true, 'href' => $href ? $href : u('member/index')));

} else {

$this->ajaxreturn(array('result' => false, 'error' => '操作失败', 'msg' => m('member')->geterror()));

}

}

//用户更改密码

public function changepwd(){

$tel = $this->_post('tel','trim');

$password = $this ->_post('password','trim');

$smscode = $this ->_post('smscode','trim');

$session_smscode = session($this ->smscode);

if (time() > $session_smscode['time'] || $smscode != $session_smscode['code']) {

$this->ajaxreturn(array('result' => false, 'error' => '验证码不正确')); //--调试成功

}

$data = array('password' => md5($password), 'addtime' => time());

$membermodel = m('member');

// 检测是否已注册

$member = $membermodel-> where(array('tel' =>$tel,'status'=>1))->find();

if (empty($member)) {

$this->ajaxreturn(array('result' => false, 'error' => '号码尚未注册'));

}

if ($membermodel->where(array('tel'=> $tel))->save($data)) {

$href = session(lastrequest);

session(member, $member['id']);

$this->ajaxreturn(array('result' => true, 'href' => $href ? $href : u('member/index')));

} else {

$this->ajaxreturn(array('result' => false, 'error' => '操作失败', 'msg' => m('member')->geterror()));

}

}

// ajax检测号码是否注册

public function checktel() {

$tel = $this->_post('tel', 'trim');

if (is_ajax && preg_match("/1[3458]{1}\\d{9}$/",$tel)) {

$membermodel = m('member');

$member = $membermodel->where(array('tel'=>$tel,'status'=>1))->find();

if (!empty($member)) {

$this->ajaxreturn(array('status' => 1, 'info' => '已注册'));

} else {

$this->ajaxreturn(array('status' => 2, 'info' => '未注册'));

}

} else {

$this->ajaxreturn(array('status' => 3, 'info' => '错误的请求'));

}

}

//发送注册验证码

public function sendsmscode() {

session($this->smstime, null);

$smstime = session($this->smstime);

$tel = $this->_post('tel', 'trim');

if (is_ajax && (!$smstime || time() > $smstime) && preg_match("/1[3458]{1}\\d{9}$/",$tel)) {

$smscode = rand(1000, 9999);

//发送验证码

require lib_path . 'org/taobao-sdk-php/topsdk.php';

$c = new topclient;

$c->appkey = '23307560'; // 原23294081

$c->secretkey = '21ef24dd4c51e20693c5db0983c433e7'; // 原0402169f466d8fed780e7f07edd25177

$req = new alibabaaliqinfcsmsnumsendrequest;

$req->setsmstype("normal");

$req->setsmsfreesignname("注册验证");

$req->setsmsparam('{"code":"'. $smscode .'","product":"【多多助店宝】"}');

$req->setrecnum("{$tel}");

$req->setsmstemplatecode("sms_5056863");

$resp = $c->execute($req);

if(!$resp->code) {

//设置发送限制时间

session($this->smstime, time() + 50);

//设置验证码5分钟内有效

session($this->smscode, array('code' => $smscode, 'time' => time() + 600));

} else {

//发送失败写入日志文件

$log = date('y-m-d h:i:s') . " 发送失败 sub_code:{$resp->sub_code} sub_msg:{$resp->sub_msg}" . php_eol;

file_put_contents(runtime_path . 'log/smscode.log', $log, file_append);

}

$this->ajaxreturn(array('result' => !$resp->code));

} else {

$this->ajaxreturn(array('result' => false, 'error' => '错误的请求'));

}

}

//发送修改密码验证码

public function sendsmsexcode(){

session($this->smstime, null);

$smstime = session($this->smstime);

$tel = $this->_post('tel', 'trim');

if (is_ajax && (!$smstime || time() > $smstime) && preg_match("/1[3458]{1}\\d{9}$/",$tel)) {

$smscode = rand(1000, 9999);

//发送验证码

require lib_path . 'org/taobao-sdk-php/topsdk.php';

$c = new topclient;

$c->appkey = '23307560'; // 原23294081

$c->secretkey = '21ef24dd4c51e20693c5db0983c433e7'; // 原0402169f466d8fed780e7f07edd25177

$req = new alibabaaliqinfcsmsnumsendrequest;

$req->setsmstype("normal");

$req->setsmsfreesignname("变更验证"); //短信签名固定,不可以换其他字

$req->setsmsparam('{"code":"'. $smscode .'","product":"【多多助店宝】"}');

$req->setrecnum("{$tel}");

$req->setsmstemplatecode("sms_5056861");

$resp = $c->execute($req);

if(!$resp->code) {

//设置发送限制时间

session($this->smstime, time() + 50);

//设置验证码5分钟内有效

session($this->smscode, array('code' => $smscode, 'time' => time() + 600));

} else {

//发送失败写入日志文件

$log = date('y-m-d h:i:s') . " 发送失败 sub_code:{$resp->sub_code} sub_msg:{$resp->sub_msg}" . php_eol;

file_put_contents(runtime_path . 'log/smscode.log', $log, file_append);

}

$this->ajaxreturn(array('result' => !$resp->code));

} else {

$this->ajaxreturn(array('result' => false, 'error' => '错误的请求'));

}

}

小结:

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

希望本文所述对大家php程序设计有所帮助。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 PHP实现的登录,注册及密码修改功能分析 https://www.kuaiidc.com/95741.html

相关文章

发表评论
暂无评论