Need help login with notepad based

hi all,

i have to try to check whether my login information is correct or not, it’s just contain username and password only. here is my config.txt content :

user1, user2, user3

and here is my html index file :

and here is my code to check login info.

everything is correct to check if the username was exists in text file but when I type wrong username (another user beside in notepad) it’s just show me blank page cause I didn’t know what code should i type in there :frowning:

what should i do ?

Instead of using for and while loop to match username you should just use in_array() instead.


if (!in_array($username, $users))
	echo "$username is NOT in our records";

When u type wrong username, $username == $users[$i] will return false, hence ur while loop will not be executed. Thus u got blank page.

An alternative without using array,

    echo "You are : " . $username;
    echo "$username not in record.";

You cannot use your preg_match() example for this match as it will match any characters in user as well as number 1, 2, and 3 for the username.

sorry, i didn’t read TS codes in detail.

If you use this

$file = fopen('config.txt', 'r');
$content = fgets($file);
$users = array_map('trim', explode(',', $content));

people can have spaces in their username if they want :slight_smile:

Also, instead of fopen…fgets I’d use file_get_contents, since you want the whole file anyway.


$file = fopen('config.txt', 'r');
$content = str_replace(' ', '', fgets($file));
$users = explode(',', $content);

$content = str_replace(',', '|', $content);

echo '<br /><br />';

$username = $_POST['username'];
$password = $_POST['password'];

if (preg_match("/^($content)$/", $username) === 1) {
    echo "You are : " . $username;
} else {
    echo "$username not in record.";

@leelong; You need to [fphp]preg_quote[/fphp] that, otherwise it won’t work with certain characters in a username (e.g. a forward slash). Also, why do you prefer this over in_array? In general in_array is a faster than using a regular expressions.

Normally we don’t allow those special characters in username, so not quite an issue.

Looking at the first post, this thread is about learning PHP rather than “I get paid” for this project so help me, after all who on earth will use notepad based login system?

Of course, in_array will be the choice. IMHO, providing other approach will be beneficial for learning purpose, nothing else.