当前位置: 移动技术网 > IT编程>数据库>MSSQL > 如何验证会员系统中用户的邮箱是否真实存在

如何验证会员系统中用户的邮箱是否真实存在

2017年12月08日  | 移动技术网IT编程  | 我要评论

灭神记txt,方绮雪,威客 猪八戒

在开发网站时,我们需要对用户注册的邮箱进行核对与验证,用户填写的邮箱是否有效邮箱。

好吧,我们先从数据库入手,修改用户表让用户有填写email的字段,添加了2个字段:

alter table [dbo].[users]
add [email] varchar(100) null, 
[isverify] bit not null default(0)
 select * from [dbo].[users]

由于你需要做2个功能,一个是要求用户验证邮箱有效性,也有可以以邮箱来让用户修改用户密码。因此需要创建一个表来存储这2个类型的数据:

create table [dbo].[requestactiontype]
(
 [type] nvarchar(2) not null primary key,
 [description] nvarchar(30) null
)
go
insert into [dbo].[requestactiontype] ([type],[description])
values ('v',n'验证邮箱是否有效。'),
('c',n'用户修改密码')

接下来,你还需要创建另外一张表,是记用户请求的事件,记录用户的一些信息,如帐号,邮箱,链接有时效性等:

create table [dbo].[userrequestaction](
 [type] nvarchar(2) not null foreign key references [dbo].[requestactiontype] ([type]),
 [token] [uniqueidentifier] not null default(newid()),
 [account] [nvarchar](30) not null,
 [email] [nvarchar](150) not null,
 [expire] [datetime] not null default (dateadd(day,(1),current_timestamp)),
)
go
source code

当用户更改邮箱成功时,需要同进对[isverify] 更改为false。因此你需要对最开始的表写一个触发器:

create trigger [dbo].[tri_users_update] 
on [dbo].[users]
for update
as
declare @u_nbr nvarchar(20),@isverify bit
declare @old_email varchar(100),@new_email varchar(100)
select @new_email = [email] from inserted
select @u_nbr = [u_nbr],@old_email = [email],@isverify = [isverify] from deleted
if @isverify = 1 and (len(isnull(@new_email,'')) = 0 or @new_email <> @old_email)
 update [dbo].[users] set [isverify] = 0 where [u_nbr] = @u_nbr
go
source code

当用户发出验证邮箱或是更改密码时,让程序执行下面的存储过程:

create procedure [dbo].[usp_userrequestaction_request]
(
 @type nvarchar(2),
 @u_nbr nvarchar(20)
)
as
if not exists(select top 1 1 from [dbo].[users] where [u_nbr] = @u_nbr)
begin
 raiserror(n'帐号错误或不存存在,请联系系统管理员。',16,1)
 return
end
declare @email nvarchar(100)
select @email = [email] from [dbo].[users] where [u_nbr] = @u_nbr
if exists(select top 1 1 from [dbo].[userrequestaction] where [type] = @type and [account] = @u_nbr and [email] = @email)
 update [dbo].[userrequestaction] set [token] = newid(),[expire] = dateadd(day,(1),current_timestamp) where [type] = @type and [account] = @u_nbr and [email] = @email
else
 insert into [dbo].[userrequestaction] ([type],[account],[email]) values (@type,@u_nbr,@email)
go
source code

用户验证邮箱有效性,是在登录之后进行的,因此只需要点击“验证”铵钮即可,系统即发送验证的邮件至用户的邮箱中。
另外,当用户忘记密码时,是在没有登录系统之下进行的,因此需要输入用户的帐号才能进行下一步。

均是使用这个存储过程[dbo].[usp_userrequestaction_request]。

接下来的流程是,用户会打开他的邮箱,查阅刚刚系统发送的邮件。邮件内容就是看实际需求了,如提示用户,是不是自己本人操作,安全性等,这些都不是怎样重要,重要的是那一条链接。

指示用户点击链接。这个链接会导上到网站一个页面。当到这个页面时,系统会在这页面进行一些程序处理,检查链接有效性,时间是否过期,如果一切没有问题,会进更新isverify字段为ture.

如果是用户忘记密码的话,在用户点击链接,系统也会检有效性,没有期,面会出现更改密码的form,让用户进行更改全新的密码。

ok,还差2个存储过程,第一个是更新isverify字段值:

create procedure [dbo].[usp_users_updateisverifyfield]
(
  @token nvarchar(36)
)
as
if exists(select top 1 1 from [dbo].[userrequestaction] where [token] = @token and [expire] >= current_timestamp)
begin
  declare @account nvarchar(30)
  select @account = [account] from [dbo].[userrequestaction] where [token] = @token
  update [dbo].[users] set [isverify] = 1 where [u_nbr] = @account
  update [dbo].[userrequestaction] set [expire] = dateadd(day,-1,current_timestamp) where [token] = @token
end
go
source code

另一个是resetpassword的,重设密码:

create procedure [dbo].[usp_users_resetpassword]
(
  @token nvarchar(36),
  @password nvarchar(100)
)
as
if exists(select top 1 1 from [dbo].[userrequestaction] where [token] = @token and [expire] >= current_timestamp)
begin
  declare @account nvarchar(30)
  select @account = [account] from [dbo].[userrequestaction] where [token] = @token
  declare @pwd varbinary(max) = encryptbypassphrase('insus#sec!%y',@password)
  update [dbo].[users] set [pwd] = @pwd where [u_nbr] = @account 
  update [dbo].[userrequestaction] set [expire] = dateadd(day,-1,current_timestamp) where [token] = @token
end
else
begin
  raiserror(n'无法更改密码,请联系客服或网络管理员。',16,1)
  return
end
source code

数据库方面开发就这样子,程序方面看你自己发挥了。

以上所述是小编给大家介绍的验证会员系统中用户的邮箱是否真实存在的方法,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网