2011-06-26 41 views
2

我想創建一個PHP日誌形式。我只想做一些調整,但當我修補它,它停止工作...php日誌形式與消息和日期和時間

如果你不能從代碼中看到,我想創建一個(模擬)日誌形式要求輸入用戶名和密碼。

  • 我想要任何空白文本框在文本框右側顯示紅色消息。 (我有紅色的錯誤信息,但我不能讓它在框的左側)

  • 我想要一個粘性的形式,保持任何領域,如果它的填充(再次,我想我有這個設置但千萬不要以爲它的所有工作

  • 我想一個人誰輸入用戶名的方式):用戶和密碼:ABC123看到一個可喜的消息。如果您不使用該用戶名/密碼組合,我想要一條消息說明他們沒有被授權。 (這是我真的不知道該怎麼做)

  • 我想這一切的終極版(也覺得我有工作,但不是100%確定)

任何幫助將是大大apprecaited!

這裏是我的代碼:

<?php 

define('TITLE', 'LOG IN'); 

// CSS 
print '<style type="text/css" media="screen"> 
.error { color: red; } 
    </style>'; 

    // Checking 
    if (isset($_POST['submitted'])) { 

$problem = FALSE; 

// Each value 

if (empty($_POST['email'])) { 
    $problem = TRUE; 
    print '<p class="error">Please enter the username!</p>'; 
} 

if (empty($_POST['password1'])) { 
    $problem = TRUE; 
    print '<p class="error">Please enter the password!</p>'; 
} 


if (!$problem) { //No problem 

    // Printing the log in message 
    print '<p>Thank you for logging in!</p>'; 

    $_POST = array(); 

} else { 

    print '<p class="error">No entry!</p>'; 

     } 

    } 

    ?> 
    <form action="login.php" method="post"> 

    <p>"Username": <input type="text" name="username" size="20" value="<?php if (isset($_POST['username'])) { print htmlspecialchars($_POST['username']); } ?>" /></p> 
    <p>Password: <input type="password" name="password1" size="20" /></p> 
    <p><input type="submit" name="submit" value="Log in" /></p> 
    <input type="hidden" name="submitted" value="true" /> 
    </form> 
+0

這只是一個學習練習,對吧? –

+0

我在教自己的PHP和我目前正在嘗試瞭解更多關於窗體。我已經建立了一些,但我想嘗試一些新技術。 – Chris

+0

我希望我不會傷害你的感情,但是這裏有很多問題。讓我看看我是否可以用一個簡單的腳本來做你想做的事情,作爲一個演示。 –

回答

3

好吧,這裏是一個簡單的登錄,它並不意味着對於真實情況。請閱讀代碼中包含的評論,以瞭解我對每個評論的看法。做很多原因,登錄是非常棘手的,所以這個例子並不是要展示真實世界的工作代碼庫,而是一個非常簡單的用戶名/密碼檢查。

與更復雜的使用相關的安全問題可能超出了這個答案,但下面的代碼是我將解釋你上面發佈的內容的方式,而不需要詳細說明(可能使其難以理解最簡單的步驟發生)。

讓我知道如果您有任何問題。在行動中看到的形式,檢查:

http://jfcoder.com/test/simplelogin.php

還有,我用PHP的定界符,而不是引用的字符串爲簡單起見。要了解更多關於此有時適用的表單,請參閱

http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

<html> 
<head> 
<style type="text/css"> 
.error { 
    color: red; 
} 
</style> 
</head> 
<body> 
<?php 

// Note, in most cases you will set a SESSION variable 
// of $_SESSION['loggedin'], which would require you to 
// use session_start() before you access any session 
// variables. 

// Note, this defaults to false. 
$loggedin = false; 

// If I get an error, I will put it in this variable. 
$error = ''; 

// If the username is provided, run the code. Otherwise, 
// act as if the login form was not submitted. This makes 
// a hidden `submitted` value superfluous, and guarantees 
// your users at least provide a username. 
if ($_POST['username']) { 
    // NOTE!!! In mose cases, you're querying a database 
    // for a username/password match. In PHP, this often 
    // means a MySQL query. DO NOT USE THE BELOW IF YOU 
    // ARE DOING SO!!! This will allow what's called a 
    // SQL injection. You MUST wash your data with something 
    // like mysql_real_escape_string() for the $_POST 
    // values (NEVER trust submitted data, always validate 
    // and escape as necessary), or use the PHP PDO library. 
    // In this example, though, I use a switch to check the 
    // values for exact matches, which means I do not need 
    // to escape (and mysql_real_escape_string() requires 
    // a database connection to use). 
    $username = $_POST['username']; 
    $password = $_POST['password']; 

    // Here, I check if the username and password match. 
    // This is, of course, hardcoded, but to match your 
    // attempt, I chose to keep the form, although you 
    // rarely see this in use in the real world.  
    switch ($username) { 
     // My one case. For each additional user, you 
     // would need to add a new entry with password 
     // check. And I set my error text according to 
     // the result of the code. 
     case 'user': 
      if ($password === 'abc123') { 
       $loggedin = true; 
      } else { 
       $error = 'Username/Password did not match.'; 
      } 
      break; 
     default: 
      // Note, I don't give a descriptive error 
      // here. If someone reports this error, I 
      // know what may have gone wrong, but the 
      // user is not told the username does not 
      // exist. 
      $error = 'Unknown error. Try again.'; 
    } 
} 

// I will only show the welcome message if the user has 
// successfully logged in. 
if ($loggedin === true) { 
    echo <<<HTML 
<h1>Welcome!</h1> 
<p>Thank you for logging in $username</p> 
HTML; 
} else { 
    // If an error text is set, display that error. 
    if ($error != '') { 
     $error = "<h4>Login error</h4><p class='error'>$error</p>"; 
    } 
    // Here's my form, only shown if the user has not 
    // successfully logged in (note, this is only a one- 
    // time check when the POST data is submitted; I 
    // would need to use sessions to "remember" the requestor 
    // had logged in across page accesses. 
    echo <<<FORM 
<h1>Login Form</h1> 
<form action="simplelogin.php" method="POST"> 
$error 
<p><label>Username: <input type="text" name="username"/></label></p> 
<p><label>Password: <input type="password" name="password"/></label></p> 
<p><input type="submit" value="Login"/> <input type="reset"/></p> 
</form> 
FORM; 
} 

?> 
</body> 
</html> 
+0

哇。 @jared - 非常感謝您這樣做!我不知道這是否會讓我成爲一個糟糕的程序員,但是一旦我看到完整的工作代碼,我就只能理解代碼/程序。也許我會變得越好,我的代碼越長,我越學越多。再次,非常感謝!你太棒了! – Chris

+0

@Chris--沒問題,你試着去了解和檢查其他代碼的次數越多,你就越能得到它。我花了一些時間纔將代碼打包並使其工作併合理安全。 :)如果你認爲這是你的問題的答案,請點擊答案旁邊的複選標記,也許可以提高它的效果。 :) –

+0

點擊複選標記並嘗試投票,但我在這裏是新的,所以我想這將需要一段時間我才能!雖然謝謝! – Chris

1

這是我的完整代碼。我幾乎重寫了整個事情,所以我很抱歉,如果編碼風格差異太大:

<?php 
// Output our CSS code 
echo '<style type="text/css" media="screen"> 
      .error 
      { 
       color: red; 
      } 
     </style>'; 

// Define our variable  
$problem = false; 

// Check if the form has been submitted 
if (isset($_POST['submitted'])) 
{ 
    // If either user or password are empty, we have a problem 
    if (empty($_POST['username']) || empty($_POST['password'])) 
    { 
     $problem = TRUE; 
    } 

    // If there is no problem, username is user, and password is abc123, we're good 
    if (!$problem && $_POST['username']=='user' && $_POST['password']=='abc123') { 

     // Print our login message 
     echo 'Thank you for logging in!<br />'; 
    } 
    // Ok, there's either a problem or the username or password is wrong, so no entry for them 
    else 
    { 
     echo '<p class="error">No entry!</p>'; 
    } 

} 

    ?> 
    <form action="login.php" method="post"> 
    Username: <input type="text" name="username" size="20" value="<?php 
     if (isset($_POST['submitted']) && !empty($_POST['username'])) 
     { 
      echo $_POST['username']; 
     } ?>" /> 
    <?php 
     if (isset($_POST['submitted']) && empty($_POST['username'])) 
     { 
      echo '<span class="error">Please enter a username!</span>'; 
     } 
    ?> 
    <br />Password: <input type="password" name="password" size="20" value="<?php 
     if (isset($_POST['submitted']) && !empty($_POST['password'])) 
     { 
      echo $_POST['password']; 
     } ?>" /> 
    <?php 
     if (isset($_POST['submitted']) && empty($_POST['password'])) 
     { 
      echo '<span class="error">Please enter the password!</span>'; 
     } 
    ?> 
    <br /><input type="submit" value="Log in" /> 
    <br /><input type="hidden" name="submitted" value="true" /> 
</form> 
+0

謝謝你的代碼以及...我真的想學習(想象那!),所以它是非常有用的,看到不同的代碼! – Chris

+0

好的,不是有害生物,但我只有2個問題。在查看你的代碼(並且運行它幾次)之後,我想知道在提交之後是否有輸入字段可以消失? (這似乎將來會對我構建的其他形式非常重要)。我的另一個問題是,如果輸入字段消失了,是否有辦法讓'輸入密碼/用戶名'單獨出現,而頂部不顯示'no entry'消息?我知道做這2個額外的元素將需要if循環改變,我只是不確定哪些循環要改變。任何人有任何建議? – Chris

+0

Late-ish回覆:使輸入字段檢查是否設置了$ _POST ['submitted]'(如果它之前已經提交,它將會)。如果不是以HTML形式輸出表單,只有在它不是真(意味着它尚未提交)時纔會回顯它。 – John