开发者

PHP string comparison difficulty

I'm attempting to help a friend with a php programming assignment. He needs to read passwords and usernames from a file and compare them to a username ($user) and password ($password) entered from a form to authenticate a user. (simple, no security whatsoever, just academic)

The problem is the comparison of strings. I've gone back and forth between using ==, ===, and strcmp but nothing seems to work correctly. Any Ideas?

This is the text file that's being read:

UserData.txt

test:pass
testa:pass2
testb:pass4
testc:pass6

<?php

$fh = fopen("UserData.txt", "r") or die("Can't open file");

$line = "";
$line_length = 0;
$div = 0;
$accounts = array();

while($line = fgets($fh)) {
    $div = strpos($line, ":"); //positing of ":" dividing username and password
    $line_length = strlen($line); //Total length of username + : + password line entry

    $accounts[substr($line, 0, $div)] = substr($line, $div + 1, $line_length);
}

foreach ($accounts as $key => $value) {
    if(($user === $key) && ($password === $value)) {
        echo "MATCH - user/pass correct<br/>";
        //Just needs to echo the above line if user/pass correct
    }
}

?>

HTML File:

<form name="myform" method="GET" action="login开发者_JAVA百科.php">
Please Login to order
User Name:
<input type="text" name="user" value="" size="10"/>
Password:
<input type="password" name="password" value="" size="10"/>
<input type="submit" name="submit" value="Log In" >

Updated login.php file:

<?php

$lines = file('UserData.txt', FILE_IGNORE_NEW_LINES);
foreach ($lines as $line)
{
  $arr = explode(':', $line);
  if ($arr[0]==$user && $arr[1]==$password)
  {
     echo "MATCH - user/pass correct<br/>";
  } else {
     echo "NO<br />";
  }
}

echo "<br />";
var_dump($user);
echo "<br />";
var_dump($password);
echo "<br />";
echo phpversion();
?>

Output:

NO
NO
NO
NO

string(4) "test" 
string(4) "pass" 
4.4.9


As short as this :

$lines = file('UserData.txt', FILE_IGNORE_NEW_LINES);
foreach ($lines as $line)
{
  $arr = explode(':', $line);
  if ($arr[0]==$user && $arr[1]==$password)
  {
     echo "MATCH - user/pass correct<br/>";
  }
}

If file UserData.txt is huge, increase memory to handle


fgets reads the final newline, so the password for "test" would be "pass\n". Next time you have an issue like this one, use var_dump on the compared strings and carefully examine the output.

Either way, consider this:

$accounts = array();
foreach (file("UserData.txt",FILE_IGNORE_NEW_LINES) as $line) {
  list($user,$pass) = explode(':',$line);
  $accounts[$user] = $pass;
}

if ($accounts[$the_user] === $the_password) { /* Password is correct */ }


Looks like an off-by-one error.

Try:

while($line = fgets($fh)) { // or better, use file()
    list($u, $p) = explode(':', $line, 2);
    $accounts[$u] = $p;
}


First, you'd rather read the entire file into an array:

$file = file('userdata.txt') or die("Can't open file");

After that, you can use explode() to split each line and add the user and password to the accounts array:

foreach($file as $line){
    $up = explode(':', $line);
    // here you can also check line validity
    if(count($up)!=2){
        continue; // skip it
    }
    $accounts[ trim($up[0]) ] = trim($up[1]);
}

Now the easy part:

if(isset($accounts[$user]) && $accounts[$user]===$password){
    echo "Welcome, $user"; // this should be sanitized before use
}
else{
    echo "Wrong username or password";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜