如何实现电子邮件验证与php


How to implement email verification with php?

我使用php和laravel作为框架。我想让用户能够通过电子邮件激活他们的帐户。

我没有这方面的经验。我已经设置了一个表单,要求用户名,电子邮件和密码。

在2013年,这仍然是最好的方式吗?

http://net.tutsplus.com/tutorials/php/how-to-implement-email-verification-for-new-members/?search_index=8

:

  1. 我需要为散列密码创建一个数据库字段。
  2. 在创建用户帐户时,为该字段创建一个随机密码并将其发送给他们。
  3. 提供url中包含密码和用户id的链接,链接到一个页面,该页面将电子邮件中的密码与db字段中的密码进行比较。
  4. 当密码匹配时激活账号(设置active为1)

之类的?

电子邮件验证是一个简单的过程,有两种方式来验证电子邮件,无论是通过发送代码到用户的电子邮件地址或通过发送链接都是一样的,这里是一个示例代码从教程http://talkerscode.com/webtricks/account-verification-system-through-email-using-php.php上TalkersCode

// Table Scheme for Verify Table
CREATE TABLE `verify` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` text NOT NULL,
`password` text NOT NULL,
`code` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1
// Table Scheme for verified_user table
CREATE TABLE `verified_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` text NOT NULL,
`password` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1

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'");
}
}

在您的解释中,您忘记了最重要的部分:随机散列。比较哈希值,而不是密码。导游作了正确的解释。

指南看起来很结实。

我会实现一个更好的随机密码生成器虽然,rand(1000,5000)真的不是很好。您甚至可以设置第一次登录时要求输入密码。

警告:根据PHP手册,EREGI已被弃用!http://php.net/manual/en/function.eregi.php

preg_match将是一个不错的选择。http://www.php.net/manual/en/function.preg-match.php