Php 生成随机数&;电子邮件激活

Php 生成随机数&;电子邮件激活,php,mysql,email,Php,Mysql,Email,我目前正在尝试使用php,并创建了一个小的HTML文档,该文档使用表单解析电子邮件和姓名等信息 我还创建了一个单独的php文件,用于分配解析数据(电子邮件、名称),并将此信息作为登录信息保存到数据库表中 我还创建了一个随机数,它应该在每次填写表单时生成 然后我想向用户发送一封电子邮件,然后用它激活他们在数据库中的帐户。为此,我在db表中创建了两个额外字段,一个用于存储生成代码,另一个作为布尔值0(false)或1(true) 我发电子邮件有困难。电子邮件本身正在发送,但如果您查看代码中的$bod

我目前正在尝试使用php,并创建了一个小的HTML文档,该文档使用表单解析电子邮件和姓名等信息

我还创建了一个单独的php文件,用于分配解析数据(电子邮件、名称),并将此信息作为登录信息保存到数据库表中

我还创建了一个随机数,它应该在每次填写表单时生成

然后我想向用户发送一封电子邮件,然后用它激活他们在数据库中的帐户。为此,我在db表中创建了两个额外字段,一个用于存储生成代码,另一个作为布尔值0(false)或1(true)

我发电子邮件有困难。电子邮件本身正在发送,但如果您查看代码中的$body变量,我会给出一个链接:

$acode

我正在尝试将激活代码附加到.php之后,但不确定为什么不是

我将发布我的php代码,因为它可能更容易浏览,并且对我所做的过程有一个想法:

$acode = rand(1111111111,9999999999); 

    $to = $email; 
    $subject = 'Please activate your account';
    $headers = 'From: welcome@oreon.com'; 
    $body = 'Hello ' . $first_name . ', \n\n Please click the link below to activate your account. \n\n http://localhost:8888/activation.php?acode=$acode \n\n Thanks.';

    // Create a new connection 
    $conn = new mysqli($servername, $username, $password); 

    if($conn->connect_error) { 
        die ('Connection Failed'); 
    } else { 
        echo ('Connection Established <br>'); 

        if(!mysqli_select_db($conn,'Oreon')) { 
            die('Database could not be reached'); 
        } else { 
            echo ('Database Reached'); 
        } // close brackets db selected 

        // Prepare SQL statements 
        $core_customer_insert = "INSERT INTO core_customer_information(firstname, lastname, email, password, activation_code, activated) VALUES ('$first_name','$last_name','$email','$user_password','$acode','0')"; 

        // Prepare SQL statements 
        $core_company_insert = "INSERT INTO core_company_information(name, reg_address, postcode, comp_reg_no, comp_utr_no, comp_vat_no) VALUES ('$company_name','$address_line','$postcode','$company_reg_no','$company_utr_no','$company_vat_no')";

        if($conn->query($core_customer_insert) === TRUE) { 
            echo ('Data successfully added'); 
            // Send activation email
            if(!mail($to,$subject,$body,$headers)) { 
                echo '<br>The activation email could not be sent at this time.'; 
            }
        } else { 
            die ('Data not added ' . $conn->error); 
        }

        if($conn->query($core_company_insert) === TRUE) { 
            echo ('Data successfully added'); 
            echo ('<br> ' . $activation_code_generator); 
        } else { 
            die ('Data not added ' . $conn->error); 
        }
    }
} // close brackets for connected 
$acode=rand(1111111111199999999);
$to=$email;
$subject='请激活您的帐户';
$headers='来自:welcome@oreon.com'; 
$body='Hello'$第一个名字\n\n请单击下面的链接激活您的帐户\n\nhttp://localhost:8888/activation.php?acode=$acode\n\n谢谢。“;
//创建新连接
$conn=newmysqli($servername、$username、$password);
如果($conn->connect_error){
die(“连接失败”);
}否则{
echo(‘已建立连接’
); 如果(!mysqli_select_db($conn,'Oreon'){ die('无法访问数据库'); }否则{ echo(‘已到达数据库’); }//关闭选定的数据库 //准备SQL语句 $core_customer_insert=“插入到core_客户信息(名字、姓氏、电子邮件、密码、激活代码、激活)值(“$first_name”、“$last_name”、“$email”、“$user_密码”、“$acode”、“0”); //准备SQL语句 $core_company_insert=“插入核心_company_信息(名称、注册地址、邮政编码、公司注册号、公司utr号、公司增值税号)值(“$company_名称”、“地址行”、“邮政编码”、“公司注册号”、“公司utr号”、“公司增值税号”); 如果($conn->query($core\u customer\u insert)==TRUE){ echo(“已成功添加数据”); //发送激活电子邮件 如果(!mail($to,$subject,$body,$headers)){ echo“
此时无法发送激活电子邮件。”; } }否则{ die('未添加数据'$conn->错误); } 如果($conn->query($core\u company\u insert)==TRUE){ echo(“已成功添加数据”); 回声(“
”.$activation\u code\u生成器); }否则{ die('未添加数据'$conn->错误); } } }//关闭已连接设备的支架

提前感谢任何能帮助我的人

首先,正如对方所说,你只需要联系他们,但我想进一步帮助你

好的,这里是你的代码的问题,像建设性的批评一样思考它:

  • 您是而不是保护您的查询,它们很容易被注入
  • 存在两个激活值可能冲突的风险,因为您只是得到随机值,而不是唯一的随机值
  • 您可以避免#2,但您必须确保在激活帐户后删除激活码
  • 您正在使用过程方法,并且存在一个MySQLi对象
  • 您也不包括电子邮件数据类型标题
  • 我还有一些顾虑,但考虑到我和你几年前的处境一样,我就不谈美学了。我已经决定重写您的代码并对其进行一点注释,这样您就可以真正从中学习

    <?php
    
    # First we generate the randomly unique activation code, there is an incredibly small chance of collision but that would only happen once in millenia or if you don't delete any activation values for centuries
    
    $acode = md5(uniqid());
    
    # If you are doing this for organization, I recommend arrays
    
    $mailData = array(
            "to"        =>  $email, # User's email
            "subject"   =>  "Please activate your account", # E-mail subject, should be same as body title
            "headers"   =>  "From:  <you@atemail.com>\r\n
                            To: ".$first_name." <".$email.">\r\n
                            MIME-Version: 1.0\r\n
                            Content-type: text/html; charset=iso-8859-1\r\n", # From: <your or sender's email>, To: User's name <user's email>, MIME-Version: Mime version, don't worry too much about this one. COntent-type: Charset and type of content that the email will have
            "body"      => "<html>
                            <head>
                                <title>Please activate your account</title>
                            </head>
                            <body>
                                Hello ".$first_name . ", \n\n Please click the link below to activate your account. \n\n http://localhost:8888/activation.php?acode=".$acode." \n\n Thanks.
                            </body>
                            </html>" # Actual content of the email
        );
    
    // Create a new connection 
    $conn = new mysqli($servername, $username, $password); 
    
    if ( $conn->connect_error )
    {
        # I'm assuming this is a testing environment so I'll keep my comments about the way you are reporting progress off
        die ('Connection Failed'); 
    }
    else
    {
        echo ('Connection Established <br>'); 
    
        # No need for this, you are already checking for connection errors on the creaton of the MySQLi object
    
        /*
        if ( !mysqli_select_db($conn, 'Oreon') )
            die('Database could not be reached'); 
        else
            echo ('Database Reached');
        */
    
        // Prepare SQL statements
        # Please protect your queries
    
        $core_customer_insert = "INSERT INTO core_customer_information(firstname, lastname, email, password, activation_code, activated) VALUES (
        '".$conn->real_escape_string($first_name)."',
        '".$conn->real_escape_string($last_name)."',
        '".$conn->real_escape_string($email)."',
        '".$conn->real_escape_string($user_password)."',
        '".$conn->real_escape_string($acode)."',
        '0')"; 
    
        // Prepare SQL statements 
        $core_company_insert = "INSERT INTO core_company_information(name, reg_address, postcode, comp_reg_no, comp_utr_no, comp_vat_no) VALUES (
        '".$conn->real_escape_string($company_name)."',
        '".$conn->real_escape_string($address_line)."',
        '".$conn->real_escape_string($postcode)."',
        '".$conn->real_escape_string($company_reg_no)."',
        '".$conn->real_escape_string($company_utr_no)."',
        '".$conn->real_escape_string($company_vat_no)."')";
    
        if ( $conn->query($core_customer_insert) === TRUE )
        { 
            echo ('Data successfully added'); 
    
            // Send activation email
    
            if( !mail($mailData['to'], $mailData['subject'], $mailData['body'], $mailData['headers']) )
                echo '<br>The activation email could not be sent at this time.'; 
    
        }
        else
            die ('Data not added ' . $conn->error);
    }
    

    如果希望在字符串中解析变量,请使用双引号。警告:当使用
    mysqli
    时,您应该使用参数化查询并将用户数据添加到查询中。不要使用字符串插值或串联来完成此操作,因为这样会创建严重的错误。切勿将
    $\u GET
    $\u POST
    数据直接放入查询中。使用uniqid()快速提问,每次创建并存储值0。有什么想法吗?奇怪,激活码是VARCHAR吗?如果不是,服务器将尝试解析文本的整数值,因此它存储0的原因是,如果它是INT列,则将其更改为VARCHAR抱歉,类型是INT xD我的道歉。