页面保护PHP页面上的回显错误

问题描述:

我有一个受保护的页面,需要您登录才能访问页面内容。页面保护PHP页面上的回显错误

我在哪里可以把那回声“错误的用户名或密码错误”

如果用户不输入准确的用户名/密码的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 
    } 
?> 

**我曾尝试进入后它 - 否则{在

页面

底部**我曾尝试进入后它 - 如果($ _ POST。 .. $ password){

没有人工作。我附上了一张图片来向你展示我的意思。

感谢

enter image description here

+0

首先,你需要'AJAX'!由于您需要再次调用脚本来检查值和“echo”。 – loveNoHate 2014-10-01 03:20:10

+0

亲爱的你的第一个问题只是使用空如果(空)比回声消息 – 2014-10-01 03:21:03

+1

@DOCASAREL这是一个非常愚蠢的建议..为什么ajax会有所帮助? – Ben 2014-10-01 03:23:20

它也可以通过简单的设置一些验证标志,并利用这些来解决对你的看法。使用你自己的代码结构模板,以下可能会诀窍:

<?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; ?> 
+0

谢谢。这工作。 – AlwaysLearning 2014-10-01 04:07:43

+0

欢迎您@AlwaysLearning,您可能已经知道,但只是一个头 - 您需要验证用户输入,使您的凭据更安全,通常的东西,你走过之前推动生产或生活。 – 2014-10-01 04:16:54

<?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; 
} 
+0

这根本不起作用。我会更多地考虑它。 – AlwaysLearning 2014-10-01 03:50:18

+0

是的逻辑是有缺陷的,一大早在睡觉前;)现在更新。事情是,你不再那么做了,坏坏的坏事。如果用户,pwhash匹配或者发送了一个jwt(如果匹配的话),你可以发出一个加密的cookie。我想向您介绍模板。你有一个模板字符串,替换占位符,一些模板评估模板中的php代码(我认为从安全角度来看这不是很好),最后输出编辑好的模板字符串。 – 2014-10-01 13:22:26

+0

你也有2个处理程序(或PHP脚本)。一个用于检查登录,另一个用于检查cookie或jwt并返回受保护的资源。所以这个逻辑实际上只是为了学习/理解的目的,你不会在现实世界中做这样的事情(如果你这样做,对你不好的坏耻辱)。 – 2014-10-01 13:29:03

试试这个,代码阅读评论

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

    } 

    ?> 
+0

我目前只是在与该项目的内容相同的页面上检查用户名/密码。谢谢 – AlwaysLearning 2014-10-01 03:51:12

+0

@AlwaysLearning所以你要我根据你的需求更新答案,如果你想我不能这样做,谢谢让我知道 – 2014-10-01 03:53:39

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

谢谢@Rasclatt。让我来研究一下这个。我不想在这一点上使用数据库,只是在页面上的用户名。它将在页面上进行检查。 – AlwaysLearning 2014-10-01 03:49:39

+0

所以你正在硬编码的用户名和密码,只是检查反对呢? – Rasclatt 2014-10-01 03:50:45

+0

是的。我目前没有使用数据库。只是硬代码用户/通 – AlwaysLearning 2014-10-01 03:52:21