0

该脚本在 2 天前工作。但是现在在每次注册时,它都会给我错误,即未定义用户名密码未定义和未定义电子邮件。

似乎 $username、$password 和 $email 变量未设置。

这是来自我的error_log:

PHP 警告:mysql_real_escape_string() [function.mysql-real-escape-string]:第 52 行 /registerScr.php 中用户 'root'@'localhost'(使用密码:NO)的访问被拒绝 PHP 警告:mysql_real_escape_string() [ function.mysql-real-escape-string]:无法在第 52 行的 /registerScr.php 中建立到服务器的链接

在第 52 行,我有 mysql_real_escape_string 函数。我在stackoverflow上搜索解决方案,但没有一个解决我的问题。

代码是:

<? 


/* Include Files *********************/
session_start(); 
include("db.php"); //Database connection
include("lScr.php"); // check if I am connected
/*************************************/
?>
<!DOCTYPE html>
<html>
 ................

<?                  
function protect($string){
        $string = mysql_real_escape_string($string);
        $string = strip_tags($string);
        $string = addslashes($string);
        return $string;
        }


    echo " <br>           
    <form class=\"form-horizontal\" action=\"\" method=\"post\">
    <fieldset>
    <div class=\"control-group\">
    <label class=\"control-label\" for=\"input01\">Username </label>
    <div class=\"controls\">
    <input type=\"text\" class=\"input-xlarge\" id=\"input01\" name=\"username\" maxlength=\"30\" placeholder=\"Your username\">    
    </div>
    </div>
    <div class=\"control-group\">
    <label class=\"control-label\" for=\"input01\">Password </label>
    <div class=\"controls\">
    <input type=\"password\" class=\"input-xlarge\" id=\"input01\" name=\"password\" maxlength=\"30\" placeholder=\"Your password\">
    </div>
    </div>
    <div class=\"control-group\">
    <label class=\"control-label\" for=\"input01\">Confirm Password </label>
    <div class=\"controls\">
    <input type=\"password\" class=\"input-xlarge\" id=\"input01\" name=\"passconf\" maxlength=\"30\" placeholder=\"Your password,again!\">
    </div>
    </div>
    <div class=\"control-group\">
    <label class=\"control-label\" for=\"input01\">E-mail </label>
    <div class=\"controls\">
    <input type=\"text\" class=\"input-xlarge\" id=\"input01\" name=\"email\" placeholder=\"Your e-mail\">
    </div>
    </div>
    <div class=\"control-group\">
    <label class=\"control-label\" for=\"input01\">Enter Captcha </label>
    <div class=\"controls\">";
          require_once('recaptchalib.php');
          $publickey = "examplekey******"; // you got this from the signup page
          echo recaptcha_get_html($publickey);
    echo"
    </div>
    </div>
    <br/><div class=\"control-group\">
    <div class=\"controls\">
    <input type=\"submit\" class=\"btn btn-primary\" name=\"submit\" value=\"Register\">
    </div>
    </div>
    </fieldset>
    </form>";

?>  
                            </div>

                            <div class="span4">
<? if (isset($_POST['submit'])) {
        $username = protect($_POST['username']);
        $password = protect($_POST['password']);
        $confirm = protect($_POST['passconf']);
        $email = protect($_POST['email']);
        $errors = array();
        if(!$username){
        $errors[] = "Username is not defined!";
        }
        if(!$password){
        $errors[] = "Password is not defined!";
        }
        if($password){
        if(!$confirm){
        $errors[] = "Confirmation password is not defined!";
        }
        }
        if(!$email){
        $errors[] = "E-mail is not defined!";
        }
        if($username){
        $aValid = array('-', '_');
        if(!ctype_alnum(str_replace($aValid, '', $username))){
        $errors[] = "Username can only contain numbers, letters and symbols like \"-\" or \"_\"!";
        }
        $range = range(1,32);
        if(!in_array(strlen($username),$range)){
        $errors[] = "Username must be between 1 and 32 characters!";
        }
        }
        if($password && $confirm){
        if($password != $confirm){
        $errors[] = "Passwords do not match!";
        }
        }
        if($email){
        $checkemail = "/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i";
        if(!preg_match($checkemail, $email)){
        $errors[] = "E-mail is not valid, must be name@server.tld!";
        }
        }
        if($username){
        $sql = "SELECT * FROM `users` WHERE `username`='".$username."'";
        $res = mysql_query($sql) or die(mysql_error());
        if(mysql_num_rows($res) > 0){
        $errors[] = "The username you supplied is already in use!";
        }
        }
        if($email){
        $sql2 = "SELECT * FROM `users` WHERE `email`='".$email."'";
        $res2 = mysql_query($sql2) or die(mysql_error());
        if(mysql_num_rows($res2) > 0){
        $errors[] = "The e-mail address you supplied is already in use of another user!";
        }
        }
  require_once('recaptchalib.php');
  $privatekey = "examplekey*****";
  $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

  if (!$resp->is_valid) {
    // What happens when the CAPTCHA was entered incorrectly
     $errors[] = "Wrong Captcha. Please re-write Captcha!";
  }
        if(count($errors) > 0){
        foreach($errors AS $error){
        echo "<div class=\"alert alert-block alert-error fade in\"> 
       <h4 class=\"alert-heading\"><li>$error</li></h4></div>";
        }
        }else {
        $sql4 = "INSERT INTO `users`
        (`username`,`password`,`email`)
        VALUES ('".$username."','".$password."','".$email."')";
        $res4 = mysql_query($sql4) or die(mysql_error());
        echo "Registed";
        }
        }
        ?>

db.php 代码:

                        $conn = mysql_connect($dbhost, $dbuser, $dbpassword);
                        if (!$conn)
                        {
                        die('Could not connect: ' . mysql_error());
                        }
                            mysql_select_db("db_example", $conn);
4

2 回答 2

0

在未连接到数据库之前不要使用此功能。没有数据库连接。

于 2013-10-12T18:36:17.017 回答
0

您从日志中转载的错误消息

PHP 警告:mysql_real_escape_string() [function.mysql-real-escape-string]: 用户'root'@'localhost' 的访问被拒绝(使用密码:否)我

是一个很好的线索。

由于某些非直观的原因,您必须先建立数据库连接,然后才能使用该mysql_real_escape_string()功能。

要解决您的问题,您必须在调用mysql_connect() 之前检查您正在调用的包含文件中的protect()内容以及连接是否正常(您拥有正确的密码、用户名和主机名)。

于 2013-10-12T18:42:18.703 回答