2013-10-12 55 views
0

這個腳本工作3天前。但現在在每一次註冊時,它都會給我錯誤,說明Usernamen未定義。密碼未定義且電子郵件未定義。註冊錯誤 - 在註冊腳本變量不isset

看來,$的用戶名,密碼$ $和電子郵件變量沒有isset。

這是從我的error_log:

PHP的警告:mysql_real_escape_string()[function.mysql實時逃逸字符串]:拒絕訪問用戶 '根' @ 'localhost' 的(使用密碼:NO)在/registerScr.php在線52 PHP的警告:mysql_real_escape_string()[function.mysql實時逃逸字符串]:到服務器的連接不能在/registerScr.php在線52

在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 [email protected]!"; 
     } 
     } 
     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); 

回答

0

沒有首先連接到數據庫,請不要使用此功能。沒有數據連接。

+0

我已經對數據庫進行連接。看看:include(「db.php」); //數據庫連接 –

+0

檢查數據庫連接是否正確。該錯誤是由於分貝連接 – user2092317

+0

失敗它被連接,如果它不能連接它應該是模頭由於這種機能的研究中db.php中引起 - >模(「無法連接:」。mysql_error()); –

0

你從轉載的錯誤消息日誌

PHP的警告:mysql_real_escape_string()[function.mysql實時逃逸字符串]:拒絕訪問用戶「根」 @「localhost」的(使用密碼:NO)我

是一個很好的線索。

由於某些非直觀的原因,您必須先使用數據庫連接,然後才能使用mysql_real_escape_string()函數。

要解決你的問題,你必須檢查你的包含文件的您呼叫mysql_connect()protect()調用之前和連接工作(你有正確的密碼,用戶名和主機名)。

+0

正如我從你的答案可以理解的,你的意思是在我的保護()之前有一個mysql_connect。我有一個mysql_connect肯定之前的保護功能(只有一個連接 - 它在db.php文件中)。 +我擁有正確的密碼,用戶,主機名。 –

+0

那麼它一定是失敗了。確保'mysql_connect()'沒有返回'null'。並且確保你的數據庫連接沒有調用mysql_close()。 – Annabel