【问题标题】:Activating user's accounts by mailing them通过邮寄激活用户帐户
【发布时间】:2011-07-20 17:45:01
【问题描述】:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<body>
<?php

require_once('database_detail.php');
if(isset($_POST['submit']))
{   
$dbc=mysqli_connect(cname,chost,cpwd,cdb);
$username=mysqli_real_escape_string($dbc,trim($_POST['username']));
$password=mysqli_real_escape_string($dbc,trim($_POST['password']));
$confirm=mysqli_real_escape_string($dbc,trim($_POST['confirm']));
$email=mysqli_real_escape_string($dbc,trim($_POST['email']));
$phone=mysqli_real_escape_string($dbc,trim($_POST['phone']));
    if(!empty($username) && !empty($password) && !empty($confirm) && !empty($email) &&        !empty($phone))
    {   
            if($password==$confirm)
            {
                $query="select * from user where      user_username='$username'";
                $data=mysqli_query($dbc,$query);
                if(mysqli_num_rows($data)== 0) 
                {
                    $random=rand(1000,10000);
                    $query="insert into     user(user_username,user_password,user_email,user_phone,date,random)".
                        "values('$username',SHA('$password'),'$email','$phone',now(),'$random')";
                    mysqli_query($dbc,$query);
                    $message="Account created successfully, kindly     visit the following link to activate your account"."\n"."localhost/login?    activation=".$random;
                    $to=$email;
                    $subject="Account Activation";
                        mail($to,$subject,$message,'From:'.'xyz@gmail.com');
                    echo 'Account created successfully. kindly visit     your email addres and activate your account.';
                exit();

                }
                else 
            {
                echo 'same username exists';
                $username="";
                }
            }
            else echo 'Enter the same password in both';
    }
    else echo 'Enter all the fields';
}
?> 

<fieldset>
<legend>signup</legend>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" >
Username:<input type="text" id="username" name="username"  />
Password:<input type="password" name="password" id="password" />
Email<input type="text" name="email" id="email" />
Contact number<input type="text" name="phone" id="phone" />
Confirm Password:<input type="password" name="confirm" id="confirm" />
</fieldset>
<input type="submit" name="submit" value="Sign up" />
</form>
</body>
</html>

因此,这是用于向使用唯一用户名密码注册的用户发送激活邮件。现在我生成一个随机数,我将该特定随机数存储在用户的数据库中,并且数据库中还有一个激活字段,该字段为 0 或 1(未激活或未激活)。现在,当用户登录时,我们检查 activtion 字段,如果正常则继续,否则检查 url 的 $_GET[activation] 字段,如果它与存储在数据库中的随机数匹配,则继续否则返回激活错误。 现在是我们这样做的方式还是有其他方式。另外,如何删除一段时间后未激活的帐户。

【问题讨论】:

    标签: php


    【解决方案1】:

    我不会使用 rand() 创建激活密钥。有可能两个人得到相同的号码。

    所以我总是使用带有用户名和当前时间的 SHA1()。


    对于停用帐户的自动删除:

    您可以创建一个 cronjob 来自动检查注册时间和当前时间之间的差异。

    【讨论】:

    • SHA1() 做什么?还有 cronjob 做了什么,我如何实现它?
    • SHA1() 从字符串创建散列,参见php.net/manual/function.sha1.php。使用 cronjob,您的脚本将在指定时间段之间自动调用。也许您的提供商提供类似的东西,但也有许多其他免费系统。
    • @user603003 我首先通过 SHA 保存了密码“abc”,然后通过 SHA1 保存了密码,但它仍然在我的数据库中保存了相同的内容。即相同的 40 位数值。
    • 实际上,SHA 只是许多编程语言中 SHA1 的另一个(函数)名称。但 SHA 只是 SHA1、SHA2 等的总称。
    【解决方案2】:

    检查:How to Generate secure activation link

    user603003说的对,cron,简单的linux程序,用来执行schedule操作,我个人用它来删除session文件。如何使用定时任务

        Here is the format of a cron job file:
    
    [min] [hour] [day of month] [month] [day of week] [program to be run]
    
    where each field is defined as
    [min]   Minutes that program should be executed on. 0-59. Do not set as * or the program will be run once a minute.
    [hour]  Hour that program should be executed on. 0-23. * for every hour.
    [day of month]  Day of the month that process should be executed on. 1-31. * for every day.
    [month] Month that program whould be executed on. 1-12 * for every month.
    [day of week]   Day of the week. 0-6 where Sunday = 0, Monday = 1, ...., Saturday = 6. * for every day of the week.
    [program]   Program to be executed. Include full path information.
    
    Here are some examples:
    
    0,15,30,45 * * * * /usr/bin/foo
    
    Will run /usr/bin/foo every 15 minutes on every hour, day-of-month, month, and day-of-week. In other words, it will run every 15 minutes for as long as the machine it running. 
    

    【讨论】:

      猜你喜欢
      • 2013-07-24
      • 2020-03-27
      • 2014-08-17
      • 2016-03-11
      • 1970-01-01
      • 1970-01-01
      • 2014-02-01
      • 2020-02-15
      • 1970-01-01
      相关资源
      最近更新 更多