2017-06-14 69 views
-2

嘿,我的登錄腳本有問題。當我在if語句中嘗試訪問變量時,就像他們無法訪問它就像他們是私人的。我曾嘗試使用PHP $ GLOBALS使其成爲全球性的,但我沒有成功。這裏是一些示例代碼。PHP中的不可訪問變量

<?php 
    $error = array(); 
    $username = $_POST['username']; 
    $password = $_POST['password']; 
    if(empty($username)){ 
     $error[] = 'Username can not be empty'; //This will show error 
    }elseif(empty($password)){ 
     $error[] = 'Password can not be empty'; //This will show error 
    }elseif(empty($error)){ 
     $stmt = $pdo->prepare("SOME QUERY"); 
     $stmt -> execute(array(':username' => $username, ':password' => $password)); 
     $count = $stmt -> fetchColumn(0); 
     if($count == 1){ 
     $stmt = $pdo->prepare("SOME QUERY"); 
     $stmt -> execute(array(':username' => $username, ':password' => $password)); 
     $results = $stmt->fetch(PDO::FETCH_ASSOC); 
       if($results['status'] == 1){ 
        $error[] = 'you are banned'; //This will not show error 
       } 
     } 
    }else{ 
     foreach($error as $alert){ 
     $errors = '<p><b>example</b>' . $alert . '</p>'; 
     } 
    } 
?> 
<html> 
    <head> 
     <title>Example</title> 
    </head> 
    <body> 
     <?php echo (!empty($errors) ? $errors : ''); ?> 
    </body> 
</html> 
+0

這是示例代碼笑它不是我會用。 –

+0

你太有趣了,但感謝您的反饋! –

回答

0

你有$error有些地方,有些地方你有$errors。如果你有所有變量引用拼寫相同,那麼它應該正常工作。

0

試試這個代碼,測試

<?PHP 


if(isset($_POST["submit"])){ 

if(empty($_POST['username'])){ 

$error[] = 'Username Cannot Be Empty'; 

}elseif(empty($_POST['password'])){ 

$error[] = 'Password Cannot Be Empty'; 

}else{ 


     $username = $_POST['username']; 
     $password = $_POST['password']; 

     $stmt = $db->prepare('SELECT * FROM user WHERE username = :username'); 
     $stmt->bindParam(':username', $_POST['username'], PDO::PARAM_STR, 16); 
     $stmt->execute(array(':username' => $_POST['username'])); 
     $row = $stmt->fetch(PDO::FETCH_ASSOC); 

     if(!empty($row['username'])){ 

     if($password==$row['password']){ 


     if($row['status']==1){ 

       $error[] = 'you are banned'; 

     }else{ 

     header("Location: index.php"); //valid login will redirect to another page 

     } 

     }elseif($password!=$row['password']){ 

     $error[] = 'Invalid Username/Password';  

     } 

     }else{ 

     $error[] = 'Invalid Username';  

     } 


} 
} 

?> 

    <?PHP 
        //check for any errors 
        if(isset($error)){ 
         foreach($error as $error){ 
          echo '<p >'.$error.'</p>'; 
         } 
        } 

        ?> 
+0

謝謝你的工作。 –

+0

@DerekBrazil你的歡迎,請upvote答案來幫助我:) –

+0

我記入你在我的來源也不能這樣做,直到我的代表是> 15 –