繁体   English   中英

PHP代码中用于电子邮件验证的错误

[英]error in PHP code for email validation

我正在尝试为我的数据库创建一个电子邮件验证表单,但是存在许多问题。 当我尝试运行下面的代码时,出现错误,未选择数据库。

我也收到未定义的变量错误。 我希望将用户名放入用户名字段下的数据库中,但显然$ name是未定义的变量。 错误,第xx行未定义变量mysql_query("INSERT INTO registrations (username, password, email, hash) VALUES( '". mysql_real_escape_string($name) ."',

我正在使用WAMP服务器。 数据库的名称是sitememberdetails,需要输入信息的表的名称是注册。 我对此很陌生-谁能告诉我如何定义变量以及如何选择db(即使它似乎已经被选择了?)

     <?php

             $host = "localhost";
             $username = "";
             $password = "";
             $databasename = "sitememberdetails";
             $email="xxxxxx@xxxxxxxx.xxx";


              $connection = mysql_connect($host,$username,$password) or die        
             ("Error: ".mysql_error());

               mysql_select_db($databasename);("sitememberdetails") or  

               die(mysql_error());   



                    if(isset($_POST['name']) && !empty($_POST['name']) AND  

                    isset($_POST['email']) && !empty($_POST['email'])){  
                     $name = mysql_real_escape_string($_POST['name']);  
                     $email = mysql_real_escape_string($_POST['email']); }  



                      if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a- 

                        z0-9-]+)*(\.[a-z]{2,3})$/i", $email)){

$msg = 'The email you have entered is invalid, please try again.';  
                }else{  

                    $msg = 'Your account has been made, <br /> please verify it     

              by clicking the activation link that has been send to  

                 your email.';  
                       }  


                        $hash = md5( rand(0,1000) ); 


                         $password = rand(1000,5000); 



                        mysql_query("INSERT INTO registrations (username, password,  

                          email, hash) VALUES( 
                          '". mysql_real_escape_string($name) ."', 
                          '". mysql_real_escape_string(md5($password)) ."', 
                          '". mysql_real_escape_string($email) ."', 
                          '". mysql_real_escape_string($hash) ."') ") or  

                          die(mysql_error());



                             $to      = $email; // Send email to our user  
                      $subject = 'Signup | Verification'; // Give the email a subject  
                      $message = ' 
                       Thanks for signing up! 
                      Your account has been created, you can login with the following  

                       credentials after you have activated your account by pressing  

                       the url below. 

                 Username: '.$name.' 
                 Password: '.$password.' 

                      Please click this link to activate your account: 
                      http://www.yourwebsite.com/verify.php?email='.$email.'& 

                      hash='.$hash.' 
                       ';  
                     $headers = 'From:noreply@yourwebsite.com' . "\r\n"; // Set from  

                       headers  
                       mail($to, $subject, $message, $headers); // Send our email  


                        ?>

尝试更改此代码

mysql_select_db($databasename);("sitememberdetails") or  

           die(mysql_error());

对此

mysql_select_db($databasename) or  die(mysql_error());

停产;

if (database_connection) {
unset($undefined_variable_error)
} else {
echo $undefined_variable_error;
}
// Because mysql_real_escape_string needs an open mysql connection

查看此修改后的代码:

<?php
         $host = "localhost";
         $username = "";
         $password = "";
         $databasename = "sitememberdetails";
         $email="xxxxxx@xxxxxxxx.xxx";


        $connection = mysql_connect($host,$username,$password) or die ("Error: ".mysql_error());
        mysql_select_db($databasename) or die(mysql_error());   


               $name = "";
           if(isset($_POST['name']) && !empty($_POST['name']) AND  
           isset($_POST['email']) && !empty($_POST['email'])){  
                 $name = mysql_real_escape_string($_POST['name']);  
                 $email = mysql_real_escape_string($_POST['email']); }  



                if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)){
                    $msg = 'The email you have entered is invalid, please try again.';  }
            else { 
                $msg = 'Your account has been made, <br /> please verify it     
                            by clicking the activation link that has been send to your email.';  
                  }  


            $hash = md5( rand(0,1000) ); 
            $password = rand(1000,5000); 

            mysql_query("INSERT INTO registrations (username, password,email, hash) VALUES( 
                      '". mysql_real_escape_string($name) ."', 
                      '". mysql_real_escape_string(md5($password)) ."', 
                      '". mysql_real_escape_string($email) ."', 
                      '". mysql_real_escape_string($hash) ."') ") or die(mysql_error());

            $to = $email; // Send email to our user  
                $subject = 'Signup | Verification'; // Give the email a subject  
            $message = ' Thanks for signing up! 
                Your account has been created, you can login with the following  
                credentials after you have activated your account by pressing  
                the url below. 
                    Username: '.$name.' 
                    Password: '.$password.' 
                Please click this link to activate your account: 
                http://www.yourwebsite.com/verify.php?email='.$email.'& 
                hash='.$hash.' 
               ';  

            $headers = 'From:noreply@yourwebsite.com' . "\r\n"; // Set from  

                mail($to, $subject, $message, $headers); // Send our email  

?>

并且我建议您使用PDO代替mysql_函数

似乎$ name值没有被发布到表单中。 如果名称变量已设置且不为空,则您正在使用mysql_escaping,但是如果根本未设置名称变量,会发生什么呢? 没有对此的检查,因此它一直持续到到达INSERT语句并导致错误为止。

此处查看示例1 以选择数据库。 您在($ databasename)之后有一个分号; 这没有任何意义。

这是一些修改后的代码,使用PDO代替mysql_ *。 让我知道这是否可行,我们可以在那里解决任何问题。

<?php

            $host = 'localhost';
            $dbname = 'sitememberdetails';
            $user = '';
            $pass = '';
            try
            {
                $DB = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); 
            }
            catch(PDOException $e)
            {  
                echo $e->getMessage();  
            }

            if(isset($_POST['name']) && !empty($_POST['name']) AND isset($_POST['email']) && !empty($_POST['email']))
            {  
                $name = $_POST['name'];  
                $email = $_POST['email'];
            }
            else
            {
                $name = 'No Name';  
                $email = 'No Email';
            }

            if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email))
            {
                $msg = 'The email you have entered is invalid, please try again.';  
            }else{  
                $msg = 'Your account has been made, <br /> please verify it by clicking the activation link that has been send to your email.';  
            }  

            $hash = md5( rand(0,1000) ); 
            $password = rand(1000,5000); 

            $query = "INSERT INTO registrations (username, password, email, hash) VALUES('?', '?', '?', '?')";
            $sth = $DB->prepare($query);
            //By using ?'s and prepare/execute, PDO will prevent SQL Injection for you!
            $sth->execute(array($name, md5($password), $email, $hash));

            $to      = $email; // Send email to our user  
            $subject = 'Signup | Verification'; // Give the email a subject  
            $message = 'Thanks for signing up! Your account has been created, 
                        you can login with the following credentials after you 
                        have activated your account by pressing the url below. 
                        Username: '.$name.' 
                        Password: '.$password.' 

                        Please click this link to activate your account: 
                        http://www.yourwebsite.com/verify.php?email='.$email.'& 

                        hash='.$hash;  
            $headers = 'From:noreply@yourwebsite.com' . "\r\n"; // Set from header  
            mail($to, $subject, $message, $headers); // Send our email  
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM