嘿我的登录脚本有问题。当我尝试在if语句中访问变量时,它们就像无法被访问一样,就像它们是私有的一样。我已经尝试使用PHP $ GLOBALS 使其全局化,但我没有成功。这是一些示例代码。
<?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)
有些地方有$error
,有些地方有$errors
。如果所有变量引用的拼写相同,那么它应该可以正常工作。
答案 1 :(得分: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>';
}
}
?>