[英]Pass txt file lines to html file POST form select options, and retrieve option selected to a php file
[英]Login form with only php and html and user and pass id in a .txt file
因此,我嘗試創建僅包含html,php和txt文件的登錄表單(我無法使用數據庫進行此分配)。 我是php的新手,但我有點想工作。 我的txt文件看起來像name | password,例如:
Jacob|transport
Lars|griskraft
我的問題是,它可以與txt文件中的“姓氏|密碼”組合一起使用,但不能與它們前面的其他名稱和密碼組合一起使用。
這是我的代碼:
<?php
if ($_POST) {
$userN = $_POST ['name'];
$passW = $_POST ['password'];
// Puts the whole array in a file every new line is an array
$userlist = file ( 'users.txt', FILE_SKIP_EMPTY_LINES );
// Defines a boolean success to false
$success = false;
foreach ( $userlist as $user ) {
$user_details = explode ( '|', $user);
//if ($user_details [0] == $userN && $user_details [1] == $passW) {
if ((in_array($userN, $user_details) and (in_array($passW, $user_details)))) {
$success = true;
echo $success . " is: ";
break;
}
}
if ($success) {
echo "<br> Hi $userN you have been logged in. <br>";
} else {
echo "<br> You have entered the wrong username or password. Please try again. <br>";
}
}
?>
這是html形式:
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<form action="" method="POST">
Comments:
<textarea rows="10" cols="30" name="commentContent"></textarea>
<br /> Name: <input type="text" name="name"><br /> Password: <input
type="password" name="password" size="15" maxlength="30" /> <br /> <input
type="submit" value="Post!"> <br />
</form>
</body>
</html>
您在.txt文件的第一行中沒有幾個空格,這是有問題的,因此, trim()
它:
foreach ( $userlist as $user ) {
$user_details = explode ( '|', $user);
if (trim($user_details [0]) == $userN && trim($user_details [1]) == $passW) {
//if ((in_array($userN, $user_details) and (in_array($passW, $user_details)))) {
$success = true;
echo $success . " is: ";
break;
}
}
var_dump($user_details)
會告訴您幾乎沒有其他隱藏的字符...
經過測試,現在可以正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.