【问题标题】:error in PHP code for email validation用于电子邮件验证的 PHP 代码错误
【发布时间】:2012-10-19 22:57:56
【问题描述】:

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

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

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

     <?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  


                        ?>

【问题讨论】:

  • 请考虑使用 mysqliPDO 代替旧的 mysql 驱动程序。 mysql 驱动程序现在不受支持并且存在一些安全问题。
  • 你应该避免使用 mysql_* 函数,因为它们已经被贬值了。
  • 使用filter_var($email, FILTER_VALIDATE_EMAIL) 代替正则表达式。

标签: php mysql database email validation


【解决方案1】:

查看修改后的代码:

<?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_ 函数

【讨论】:

  • thnks Isa/dcirello - 我对此非常陌生,但有一个截止日期,所以我只想先尝试让代码正常工作,一旦它启动并运行,我会尝试更新已弃用的标签。代码工作正常,除了我不断收到错误“用户'@'localhost'拒绝访问数据库'sitememberdetails'”。你知道为什么会这样吗?我正在使用 Wampserver。
  • 在 phpMyAdmin 面板中检查您在连接中使用的用户的用户和权限。祝你好运。
【解决方案2】:

这是一些修改后的代码,使用 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  
?>

【讨论】:

  • 干杯山姆 - 我在网上都遇到了两个错误:“$sth = $DB->prepare($query);”。错误是“未定义的变量:C:\wamp\www\Root\siginuppage.php 中的 DB”和“致命错误:在 C:\wamp\www\Root\ 中的非对象上调用成员函数 prepare() siginuppage.php ".
  • @Danielokeeffe 我希望这是关于数据库连接的问题。尝试把 $DB = "1";在 catch 语句中的 echo 下方,看看它是否“删除”了第一个错误,如果是,则表示您的初始数据库连接失败。
【解决方案3】:

似乎 $name 值没有发布到表单中。如果 name 变量已设置且不为空,您将对其进行 mysql_escaping,但如果 name 变量根本未设置会发生什么?没有对此进行检查,因此它会继续执行,直到它到达 INSERT 语句并导致错误。

查看示例 #1 here 以选择数据库。 ($databasename); 后面有一个分号;这没有意义。

【讨论】:

    【解决方案4】:

    尝试更改此代码

    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
    

    【讨论】:

    • 感谢 Delta 解决了这个问题。除了我现在有另一个1!修复此错误意味着现在选择了一个数据库,但现在它显示“用户'@'localhost'对数据库'sitememberdetails'的访问被拒绝”!数据库名称是正确的,如果用户名和密码留空,我是否应该被允许访问?我没有在 phpmyadmin 中更改它们。
    • 我认为 wamp 服务器默认的 mysql 用户名是 root。但我不确定,因为我没有使用它。这是这个answers.yahoo.com/question/index?qid=20090716053359AAx59vM的谷歌搜索结果@
    • 我保证的最后一个问题 - 将用户名指定为 root,现在它显示“警告:邮件():无法在“localhost”端口 25 连接到邮件服务器,请验证您的“SMTP”和“在 php.ini 中设置“smtp_port”或在第 90 行的 C:\wamp\www\Root\siginuppage.php 中使用 ini_set()”。这是“邮件($to,$subject,$message,$headers);”。就像解决的问题越多,出现的问题就越多!这是否与该网站目前尚未上线有关?
    • 您必须在您的机器上设置一个 smtp 服务器或在真实服务器上尝试此代码。几乎所有托管公司都在其 Web 服务器上设置了 smtp 服务器。
    猜你喜欢
    • 1970-01-01
    • 2021-02-16
    • 1970-01-01
    • 2018-05-05
    • 1970-01-01
    • 2014-12-19
    • 2011-09-25
    • 2017-03-19
    • 1970-01-01
    相关资源
    最近更新 更多