【问题标题】:echo Error on for Page Protected PHP page受页面保护的 PHP 页面的回显错误
【发布时间】:2014-10-01 03:17:05
【问题描述】:

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

我在哪里可以放置一个回显“错误的用户名或错误的密码”的 else 语句

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

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
    }
?>  

** 我试过在 -- else { 在页面底部之后输入它

** 我试过在 -- if($_POST...$password) {

两者都不起作用。我附上了一张图片来告诉你我的意思。

谢谢

【问题讨论】:

  • 首先你需要AJAX!由于您需要再次调用脚本来检查值和echo
  • 亲爱的你的第一个问题,只需使用 if(empty) 之类的空而不是回显消息
  • @DOCASAREL 这真是一个愚蠢的建议。为什么 ajax 会有所帮助?
  • 您不一定需要 ajax,但您几乎没有任何逻辑包含在此页面上来执行任何操作。没有数据库检查或任何东西......
  • 我希望你不会直播

标签: php


【解决方案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; ?>

【讨论】:

  • 欢迎您@AlwaysLearning,您可能已经知道但只是提醒一下 - 您需要验证用户输入,使您的凭据更安全,在推送到生产或上线之前您通常会经历的事情.
【解决方案2】:
<?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 
        } ?> 

【讨论】:

  • 感谢@Rasclatt。让我处理这个。我现在不想使用数据库,只是页面上的用户名。将在页面上进行检查。
  • 所以您是在硬编码用户名和密码,然后只进行检查?
  • 是的。我目前没有使用数据库。只是硬编码用户/通行证
  • 试试这个验证器类。在ValidateUser::Check('hardcodeuser','hardcodepass'),您需要在此处更改您的用户名和密码。希望你没有任何重要的事情要做,因为这里的东西不是很安全......
  • 感谢您的帮助!不,这不是每个用户网站的直播,这是我正在做的一项测试,以自学 php。这些东西已经存在免费代码。但我正在努力实现它,而不是复制和粘贴它。
【解决方案3】:

试试这个并阅读代码中的注释

    <?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 

    }

    ?> 

【讨论】:

  • 我目前只是在与该项目的内容相同的页面上检查用户名/密码。谢谢
  • @AlwaysLearning 所以你想让我根据你的要求更新答案,如果你想我不能这样做,谢谢让我知道
【解决方案4】:
<?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;
}

【讨论】:

  • 是的,逻辑有缺陷,早上睡觉前;)现在更新。问题是,你不再那样做了,坏坏坏。如果用户、pwhash 匹配,您可以发送一个加密的 cookie,或者如果匹配,则发送一个 jwt。我想向您介绍模板。你有一个模板字符串,替换占位符,一些模板 eval 模板中的 php 代码(从安全角度我认为这不是很好),最后输出编辑的模板字符串。
  • 您还有 2 个处理程序(或 php 脚本)。一个用于检查登录,另一个用于检查 cookie 或 jwt 并返回受保护的资源。所以这个逻辑真的只是为了学习/理解的目的,你不会在现实世界中做这样的事情(如果你这样做了,对你来说太糟糕了太丢脸了)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-21
  • 2010-09-17
  • 1970-01-01
  • 2021-06-10
  • 2012-11-08
  • 1970-01-01
相关资源
最近更新 更多