如何创建随机密钥 php mysql?
我需要在 MySQL 中创建随机密钥存储 如果我使用:
<?php echo md5(rand(10000,99999)); ?>
我可以将其存放在哪里
<?php
require 'config.inc.php';
foreach($_POST as $k=>$v)
{
$_POST[$k] = trim($v);
}
if(!isset($_POST['produgg_username']) or !isset($_POST['produgg_password']) or !isset($_POST['produgg_email']))
{
print "Please use all fields";
}elseif(empty($_POST['produgg_username'])){
print "Please choose a username";
}elseif(empty($_POST['produgg_password'])){
print "Please choose a password";
}elseif(empty($_POST['produgg_email'])){
print "Please enter an email address";
}elseif(!filter_var($_POST['produgg_email'], FILTER_VALIDATE_EMAIL)) {
print "Please enter a valid email address";
}elseif(!preg_match("/^[a-z0-9]+$/i", $_POST['produgg_username'])) {
print "Please use only characters and numbers for username";
}elseif($usersClass->checkUserExists($_POST['produgg_username'])) {
print "Username Taken, please choose another";
}else{
if($usersClass->register($_POST['produgg_username'], md5($_POST['produgg_password']), $_POST['produgg_email']))
{
print "success";
$toemail = $_POST['produgg_email'];
$touser = $_POST['produgg_username'];
// Send activation email
$to = $toemail;
$subject = "Activation";
$headers = "From: [email protected]";
$body = "Howdy $touser!
To activate your please click on the following link - http://www..co.uk/activateuser.php?email=$toemail";
mail($to, $subject, $body, $headers);
}else{
print "Something weird happened and we couldn't setup the account!";
}
}
?>
I need create random key store in MySQL
If I use:
<?php echo md5(rand(10000,99999)); ?>
Where can I store it
<?php
require 'config.inc.php';
foreach($_POST as $k=>$v)
{
$_POST[$k] = trim($v);
}
if(!isset($_POST['produgg_username']) or !isset($_POST['produgg_password']) or !isset($_POST['produgg_email']))
{
print "Please use all fields";
}elseif(empty($_POST['produgg_username'])){
print "Please choose a username";
}elseif(empty($_POST['produgg_password'])){
print "Please choose a password";
}elseif(empty($_POST['produgg_email'])){
print "Please enter an email address";
}elseif(!filter_var($_POST['produgg_email'], FILTER_VALIDATE_EMAIL)) {
print "Please enter a valid email address";
}elseif(!preg_match("/^[a-z0-9]+$/i", $_POST['produgg_username'])) {
print "Please use only characters and numbers for username";
}elseif($usersClass->checkUserExists($_POST['produgg_username'])) {
print "Username Taken, please choose another";
}else{
if($usersClass->register($_POST['produgg_username'], md5($_POST['produgg_password']), $_POST['produgg_email']))
{
print "success";
$toemail = $_POST['produgg_email'];
$touser = $_POST['produgg_username'];
// Send activation email
$to = $toemail;
$subject = "Activation";
$headers = "From: [email protected]";
$body = "Howdy $touser!
To activate your please click on the following link - http://www..co.uk/activateuser.php?email=$toemail";
mail($to, $subject, $body, $headers);
}else{
print "Something weird happened and we couldn't setup the account!";
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您似乎正在使用普通的
md5()
来存储用户密码...不要这样做,这是一个安全风险。 您正在将您的用户和您自己置于危险之中。使用更强的哈希算法或 bcrypt 进行密钥强化。 有关详细信息,请参阅此答案。看来您实际上是在尝试生成用于电子邮件激活的
nonce
。如果有的话,通用唯一标识符(UUID)将完成这项工作。它的碰撞变化非常低,并允许 3 × 1038 唯一值(使用某个值后,您可以根据您的用例将其重复用于其他用户)。
您可以使用我编写的这个函数在 PHP 中生成 UUID。您需要的是 v4 UUID。
First, it seems that you are using plain
md5()
to store user passwords... DO NOT DO THAT, IT IS A SECURITY RISK. You are putting your users and yourself at risk. Use key strengthening with a stronger hash algorithm or bcrypt. See this answer for more information.It seems that you are actually trying to generate a
nonce
for email activation.If anything, a Universally Unique IDentifier (UUID) will do the job. It has a very low change of collision and allows for 3 × 1038 unique values (once a value is used, you can reuse it for another user anyway for your use case).
You can use this function I wrote to generate UUIDs in PHP. What you want for your needs is a v4 UUID.
使用
uniqid()
函数而不是使用 md5。确保将more_entropy
设置为 true。即,
将
'prefix'
更改为适合您的应用程序的内容。Use the
uniqid()
function instead of doing it with the md5. Make sure to setmore_entropy
to true.i.e.
Change
'prefix'
to something appropriate for your application.