页面保护的PHP页面的echo错误

时间:2014-10-01 03:17:05

标签: php

我有一个受保护的页面,要求您登录才能访问页面内容。

我在哪里可以发出一个其他声明,即echos“用户名错误或密码错误”

如果用户没有输入确切的用户/密码?

PHP页面

<?php
    // Define your username and password 
    $username = "user"; 
    $password = "password"; 
    if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) { 
?> 
    <h1>Login</h1> 
    <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
        <label>User</label>
        <input type="text" title="Enter your Username" name="txtUsername" />
        <label>Password</label>
        <input type="password" title="Enter your password" name="txtPassword" />
        <input type="submit" name="Submit" value="Login" />
    </form> 
<?php
    } 
    else {
?> 
    <p>This is the protected page. Your private content goes here.</p> 
<?php
    }
?>  

**我之后尝试输入 - 否则{在页面底部

**我之后尝试输入 - if($ _ POST ... $ password){

两个人都没有工作。我附上了一张图片,告诉你我的意思。

由于

enter image description here

4 个答案:

答案 0 :(得分:1)

<?php 
    class   ValidateUser
        {
            public static   function Check($user,$pass)
                {
                    $settings[] =   ($user == $_POST['txtUsername'])? 1:0;
                    $settings[] =   ($pass == $_POST['txtPassword'])? 1:0;

                    return (array_sum($settings) == 2)? true:false;
                }
        }

    // if the username and passowrd match up
    if(isset($_POST['txtUsername'])) {
            $uservalid  = ValidateUser::Check('hardcodeuser','hardcodepass');
        }

    // If user/pass not valid
    if($uservalid !== true || !isset($uservalid)) { ?> 
        <h1>Login</h1> 
        <?php if(isset($uservalid) && $uservalid !== true) echo 'Invalid Login'; ?>
        <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
            <label>User</label>
            <input type="text" title="Enter your Username" name="txtUsername" />
            <label>Password</label><input type="password" title="Enter your password" name="txtPassword" />
            <input type="submit" name="Submit" value="Login" />
        </form> 

<?php } 
    else { ?> 
    <p>This is the protected page. Your private content goes here.</p>
    <?php 
        } ?> 

答案 1 :(得分:1)

也可以通过简单地设置一些验证标志并在视图中使用这些标志来解决。使用您自己的代码结构模板,以下可能会起到作用:

<?php 

// Define your username and password 
$username = "user"; 
$password = "password"; 

$hasError = true;
$hasSubmitted = false;

if (isset($_POST['Submit'])) {
    $hasSubmitted = true;

    if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) { 
        $hasError = true;
    } else {
        $hasError = false;
    }
}

if ($hasError):
?>
<h1>Login</h1> 

<?php if ($hasSubmitted): ?>
<p>*You entered a wrong username or password</p>
<?php endif; ?>
<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
<label>User</label><input type="text" title="Enter your Username" name="txtUsername" />
<label>Password</label><input type="password" title="Enter your password" name="txtPassword" />
<input type="submit" name="Submit" value="Login" />
</form> 

<?php else: ?>

<p>This is the protected page. Your private content goes here.</p> 

<?php endif; ?>

答案 2 :(得分:0)

<?php
// Define your username and password
$username = "user";
$password = "password";

$loginPageTPL = <<< EOF
<!doctype html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>Login</h1> 

<form name="form" method="post" action="{% PHP_SELF %}"> 
{% ERROR_MESSAGES %}
<label for="username">User</label>
<input id="username" type="text" placeholder="Enter your Username" name="txtUsername" />
<label for="password">Password</label>
<input id="password" type="password" placeholder="Enter your password" name="txtPassword" />
<input type="submit" name="Submit" value="Login" />
</form> 
</body>
</html>
EOF;

$loginPageTPL = str_replace('{% PHP_SELF %}', $_SERVER['PHP_SELF'], $loginPageTPL);

if ((isset($_POST['txtUsername'])) && (isset($_POST['txtPassword']))) {
    if (($_POST['txtUsername'] == $username) && ($_POST['txtPassword'] == $password)) {
        echo "your private content here";
        return;
    } else {
        $loginPageTPL = str_replace('{% ERROR_MESSAGES %}', '<div style="color: red">* you entered a wrong username and password</div>', $loginPageTPL);
        echo $loginPageTPL;
    }
} else {
    $loginPageTPL = str_replace('{% ERROR_MESSAGES %}', '', $loginPageTPL);
    echo $loginPageTPL;
}

答案 3 :(得分:0)

尝试此操作并阅读代码中的注释

    <?php 
    if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) { 

    ?> 

    <h1>Login</h1> 

    <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
    <label>User</label><input type="text" title="Enter your Username" name="txtUsername" />
    <label>Password</label><input type="password" title="Enter your password" name="txtPassword" />
    <input type="submit" name="Submit" value="Login" />
    </form> 

    <?php 

//adde these line to check weather its empty or not    
if(!empty($_POST['txtUsername']) && empty($_POST['txtPassword']))
        {
        //this is complicated part you need check username and password. and they have to 
//match. 
// i cant help you here, cause i dont know from where you want to check username and password
//but i am giving you if statement.
//simple way you get the username from form, than check wether the username password matches, in the db or not, and if does not matches, we show the error message
//run the query
//check the result
//result return succes then ok
//else show the error
        }
        else 
            {
        echo 'You need to enter your username and password';
    }

    } 
    else { 

    ?> 

    <p>This is the protected page. Your private content goes here.</p> 

    <?php 

    }

    ?>