PHP电子邮件验证链接的最简单方法

时间:2010-09-25 18:19:49

标签: php mysql email verification

我的网站上已经有一个高级用户登录/注册系统(colemansystems.psm2.co.uk)。但是,我希望向新用户发送电子邮件以验证其电子邮件地址。如果他们没有点击该链接,他们将无法访问他们的帐户。我对PHP和MySQL缺乏经验,所以请深入解释。

编辑:我用于verify.php文件的代码(用户点击GET的链接(例如,verify.php?d=51773199320))

$secret = $_GET['d'];
$result = mysql_query("SELECT valid FROM users WHERE secret=$secret");
while ($row = mysql_fetch_array($result))
{
    $valid = $row['valid'];
}
if ($valid == "") {
    echo"There seems to be a problem with the verification code.<br><br><br><br><br>";
}
elseif ($valid == "1")
{
    echo"Your account is already verified.<br><br><br><br><br>";
}
else
{
    mysql_query("UPDATE users SET valid = '1' WHERE secret=$secret");  
    echo "Thank you, your account is now verified and you are free to use the exclusive features!<br><br><br><br><br><br>";
}

这安全吗?

3 个答案:

答案 0 :(得分:30)

最简单的方法是不注册未经验证的用户。

向他们询问电子邮件地址并发送包含此地址的链接的电子邮件,该地址用哈希密封。收到此链接后,您可以开始注册过程。

像这样的东西

$secret = "35onoi2=-7#%g03kl";
$email = urlencode($_POST['email']);
$hash = MD5($_POST['email'].$secret);
$link = "http://example.com/register.php?email=$email&hash=$hash";

并在register.php向注册表单中添加2个隐藏字段 - 电子邮件和哈希,将其收到的值存储在GET中。

最后,流程注册和检查,

if (md5($_POST['email'].$secret) == $_POST['hash']) {
    //Continue registration.
}

答案 1 :(得分:0)

最容易为谁 - 用户,编码器,电脑? 你在优化什么 - 按键的数量,代码的大小,用户体验?

最简单的代码可能不安全。 在发送信件之前,您应该检查电子邮件地址的正确性。

答案 2 :(得分:0)

注册后创建一个散列字符串并将其保存到临时用户表中使用此代码将散列字符串发送到用户电子邮件地址

if(isset($_POST['register']))
{
$email_id=$_POST['email'];
$pass=$_POST['password'];
$code=substr(md5(mt_rand()),0,15);
mysql_connect('localhost','root','');
mysql_select_db('sample');

$insert=mysql_query("insert into verify values('','$email','$pass','$code')");
$db_id=mysql_insert_id();

$message = "Your Activation Code is ".$code."";
$to=$email;
$subject="Activation Code For Talkerscode.com";
$from = 'your email';
$body='Your Activation Code is '.$code.' Please Click On This link <a href="verification.php">Verify.php?id='.$db_id.'&code='.$code.'</a>to activate your account.';
$headers = "From:".$from;
mail($to,$subject,$body,$headers);

echo "An Activation Code Is Sent To You Check You Emails";
} 

然后创建验证页面然后

if(isset($_GET['id']) && isset($_GET['code']))
{
$id=$_GET['id'];
$code=$_GET['id'];
mysql_connect('localhost','root','');
mysql_select_db('sample');
$select=mysql_query("select email,password from verify where id='$id' and code='$code'");
if(mysql_num_rows($select)==1)
{
    while($row=mysql_fetch_array($select))
    {
        $email=$row['email'];
        $password=$row['password'];
    }
    $insert_user=mysql_query("insert into verified_user values('','$email','$password')");
    $delete=mysql_query("delete from verify where id='$id' and code='$code'");
 }
}

如果您有任何问题,请参阅完整的教程http://talkerscode.com/webtricks/account-verification-system-through-email-using-php.php