i have a notepad that i made into a database.

i want to know if a username and a password are valid combination.

but if i run it, the 10th entry, for example, gives 10 different outputs until it finds the perfect match.

suggestions would be nice.

<?php

$user=$_POST['user'];
$pass=$_POST['pass'];

$files= file("fileone.txt");
$num = count($files);
for($i=0;$i<$num;$i++)
{

    $arrs=explode(",",$files[$i]);

    if($user==$arrs[0])
    {
    //echo "Welcome " . $user;
    if($pass==$arrs[1])
    {
    echo "Welcome " . $user ."!";
    }
    }
    else
    {
    echo "Incorrect Name and Password Combination.";
    }
}
?>

it is because of your else statement, I assume it prints "Incorrect Name and Password Combination" 9 times and then "Welcome user!"?

You just need to move the else statement:

if($user==$arrs[0])
{
[INDENT]//echo "Welcome " . $user;[/INDENT]
[INDENT]if($pass==$arrs[1])[/INDENT]
[INDENT]{[/INDENT]
[INDENT][INDENT]echo "Welcome " . $user ."!";[/INDENT][/INDENT]
[INDENT]}[/INDENT]
[INDENT]else[/INDENT]
[INDENT]{[/INDENT]
[INDENT][INDENT]echo "Incorrect Name and Password Combination.";[/INDENT][/INDENT]
[INDENT]}[/INDENT]
}

This should fix the issue.

Hope I have been helpful.

The following code uses the SplFileObject [http://www.php.net/manual/en/class.splfileobject.php] from the SPL library. This ensures we never read the entire file into memory, only one line at a time. For small files it is okay to read it into an array, but it is best to get in the habit of working with files line by line unless absolutely necessary to read the whole thing.

Also SplFileObject allows us to parse CSV (Comma Separated Value) files in a single pass. In your case it appears your username and password are separated by a comma so this can be considered a CSV file.

The logic is the same the implementation is different. Also in my example there is a break; statement that stops execution of the foreach loop once a user has been found. This stops us from having to iterate over the n lines that might still exist in the file.

<?php

$user = $_POST['user'];
$pass = $_POST['pass'];

//Use an SplFileObject so we only ever work with 1 line of the file in memory at a time
$file = new SplFileObject( 'fileone.txt' );

//Set flag bitmask for DROP_NEWLINE, SKIP_EMPTY, READ_CSV
$file->setFlags( 15 );

//Because of the READ_CSV flag, $line is already an array of fields field[0], field[1] etc.
foreach( $file as $line ){
    //If we find the user in the line, validate the password
    if( $user == $line[0]){
        if( $pass == $line[1] ){
            echo 'Welcome '.$user.'!';
        } else {
            echo 'Incorrect Name and Password Combination.';
        }
        //This ends execution of the loop so we don't continue processing lines we don't need too
        break;
    }
}

thanks for the help guys. this is really what i wanted to do. stop the code from going over the whole document. get the line with the right username and verify if the password is correct.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.