so I'm trying to create a login form with only html, php and txt files (I can't use a database for this assignment). I'm new to php but i have kinda got i to work. My txt file looks like name|password for example:
Jacob|transport
Lars|griskraft
The problem i have is that it works with the last name|password combo in my txt file but not with another name and password combo before them.
Here is my code:
<?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>";
}
}
?>
Here is the html form:
<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>