2014-10-01 55 views
0

我有一個受保護的頁面,需要您登錄才能訪問頁面內容。頁面保護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

回答

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

謝謝。這工作。 – AlwaysLearning 2014-10-01 04:07:43

+0

歡迎您@AlwaysLearning,您可能已經知道,但只是一個頭 - 您需要驗證用戶輸入,使您的憑據更安全,通常的東西,你走過之前推動生產或生活。 – 2014-10-01 04:16:54

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

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 

    } 

    ?> 
+0

我目前只是在與該項目的內容相同的頁面上檢查用戶名/密碼。謝謝 – AlwaysLearning 2014-10-01 03:51:12

+0

@AlwaysLearning所以你要我根據你的需求更新答案,如果你想我不能這樣做,謝謝讓我知道 – 2014-10-01 03:53:39

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