我正在尝试向用户发送包含链接的电子邮件(就像许多网站一样),当他们点击此链接时,他们的帐户将被激活。我究竟如何在Php中做到这一点?
答案 0 :(得分:5)
我会这样做:
1)完成注册后,将帐户标记为非活动状态(待定确认),并创建两个随机字符串。
2)将两个字符串存储在数据库中并将其与用户关联。
3)通过电子邮件发送指向用户的链接,该链接包含指向您网站页面的链接,并包含两个字符串。
例如:www.mysite.com/confirm.php?auth1=j0832r2&auth2=fji4j32ion
4)让您的页面检查两个代码是否匹配,如果匹配,则将该帐户标记为活动。
希望这有帮助。
答案 1 :(得分:3)
以下是我用来发送验证邮件的代码部分。
//create login hash
$hash = hash('whirlpool', $user->authentication->password . time() . $user->authentication->salt);
if( !Quick_Login::add($hash, $user->userid, time() + 3600, 0) )
{
// die
}
//load email template
ob_start();
include('templates/account_create.html');
$body = ob_get_clean();
if( Mail::sendMail($user->contact->email, 'no-reply-automator@domain.com', "Email Verification", $body) )
{
//redirect to login
throw new RedirectBrowserException("/index.php?code=6");
}
Mail
类为a simple mailer that I built,您可以轻松使用php mail()
函数。
此处加载的电子邮件模板是:
<html>
<body>
<div style="width: 600px; border: 2px solid #E9EBF6; margin: auto; font-size: 16px; color: #555555;">
<h1 style="margin: 0; padding: 8px; background-color: #E9EBF6; text-align: center;">
Hello, <?=$user->fname;?>!
</h1>
<div style="overflow: hidden; padding: 8px; padding-top: 0; background-color: #F5F6FB;">
<p>You are receiving this email because you (or someone pretending to be you!) has signed up for a new account on the Domain System.</p>
<p>If you would like to verify this email account (and you must in order to use the system), please <a href="http://domain.com/components/authentication/verify.php?code=<?=$hash;?>">click this link</a>.</p>
<p>If you don't know what this is about, or you don't want the account, simply do nothing.</p>
<p>The quick login link above is a one-time access pass to your account. Please use the link to verify your email address and complete your account signup.</p>
<br />
<p>Thanks!</p>
<p>-Domain</p>
</div>
</div>
</body>
</html>
他们点击的链接转到此脚本(verify.php
):
<?php
set_include_path('../../backbone:../../global:../../jquery:../../components:../../content:../../images:../../model:../../render:../../scripts:../../styles');
require_once('RedirectBrowserException.php');
require_once('User.php');
require_once('Session.php');
require_once('Quick_Login.php');
setSession(0, '/');
$code = isset($_GET['code']) ? $_GET['code'] : null;
if( $code )
{
$ql = Quick_Login::getByHash($code);
if( $ql )
{
$user = User::getByID($ql->userid);
$user->disabled = 0;
$user->save();
setSessionVar('active', true);
setSessionVar('roleid', $user->authentication->role->roleid);
setSessionVar('userid', $user->userid);
$ql->used = 1;
$ql->save();
throw new RedirectBrowserException("/home.php?code=0");
}
else
{
throw new RedirectBrowserException('/index.php?code=9');
}
}
else
{
throw new RedirectBrowserException('/index.php?code=9');
}
?>
您会在最后一个脚本中看到它在其帐户中设置disabled=0
,并将其登录(在会话中设置active
,并指定他们的roleid
和{会议{1}}。
希望这有帮助。
userid
)verify.php?code=sdflnsdlknsge98y32598swob
)。如果它有效,它会在用户的帐户上设置一个标记,表明他们已经验证了他们的电子邮件地址。答案 2 :(得分:1)
我做了以下事情。
password_hash($user_password, PASSWORD_DEFAULT);
$confirm_code = bin2hex(random_bytes(32));
此外,您不应该从他们的确认链接自动登录某人。这是一个巨大的安全违规行为。任何人都可以在激活前点击该链接,突然他们可以完全访问该帐户。始终强制用户在确认后登录。
答案 3 :(得分:0)
我多次这样做的方式是在注册过程中,将“Active”的成员表中的一个字段保持为bool(0或1)0为no,1为yes。在注册过程中将它们标记为0.此外,您还需要一个注册码字段。或者,您可以创建一个新的用户标识/注册码表并进行比较。
当用户注册时,创建一个唯一的代码。记录该代码并向他们发送电子邮件。当他们点击电子邮件中的链接时,请使用代码检查表格,如果它与数据库中的代码匹配,请将该用户的活动字段更改为1
答案 4 :(得分:0)
当他们注册时,为数据库添加一个唯一的ID用于他们的用户名。同时将“活动”列设置为0.要向他们发送电子邮件,请使用PHP mail()功能。在链接到yoursite.com/activate?id=uniqueid的电子邮件中有一个链接。获取activate.php上的ID,进行一些检查,并将数据库中的“active”列设置为1。