luc*_*axi 34 php email-validation
使用PHP,有哪些方法可以生成可以存储在数据库中并用于确认电子邮件的随机确认代码?我不能为我的生活想到一种方法来生成一个可以从用户的个人资料中生成的唯一号码.这样我就可以使用一个函数使数字小到足以包含在URL中(参见此链接).请记住,用户必须单击链接以"确认/激活"他/她的帐户.如果我不能使用数字,我使用字母和数字都没有问题.
话虽如此,我已经尝试将用户名和"salt"一起散列以生成随机代码.我知道必须有更好的方法,所以让我们听听.
Joh*_*nde 54
$random_hash = md5(uniqid(rand(), true));
Run Code Online (Sandbox Code Playgroud)
这将是32个字母数字字符长且独特.如果你想让它更短,只需使用substr():
$random_hash = substr(md5(uniqid(rand(), true)), 16, 16); // 16 characters long
Run Code Online (Sandbox Code Playgroud)
生成随机数据的替代方法包括:
$random_hash = md5(openssl_random_pseudo_bytes(32));
$random_hash = md5(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));
// New in PHP7
$random_hash = bin2hex(random_bytes(32));
Run Code Online (Sandbox Code Playgroud)
1)在数据库中创建激活的字段
2)注册后发送电子邮件
3)创建一个链接以包含在电子邮件中,使用唯一标识符它看起来像这样
欢迎用户名感谢您的注册.
请点击下面的链接激活您的帐户
domain.com/register.php?uid=100&activate=1
Run Code Online (Sandbox Code Playgroud)
4)将Activated Field更新为true
替代文字http://www.jackborn.com/wpress/wp-content/uploads/2008/02/confirmsubs2.gif
$email_encrypt = urlencode($email);
$special_string = 'maybeyourcompanynamereversed?';
$hash = md5($email_encrypt.$special_string);
Here is the link that is sent to the email that was provided:
http://yourdoman.com/confirm.php?hash='.$hash.'
The actual link will look something like this:
http://yourdomain.com/confirm.php?hash=00413297cc003c03d0f1ffe1cc8445f8
Run Code Online (Sandbox Code Playgroud)