0

I am trying to use php to verify my html form a file.txt in witch i have a login , password. the problem in my php code that even when the variables are equal i get false when i compare them.The only exception is when i compare the first login and pasword i get true for both.

Here is my code to clarify my problem. so my html form calls this php page code.

<?php
echo " begin <br/>";
    $name=$_GET['username'];
    $password=$_GET['password'];
        echo "$name <br/>";
        echo "$password <br/>";
         $f = fopen("file.txt", "r");
         $res="0";  
            while(!feof($f))
           { echo "begin while <br/> ";
                $p3=fgetc($f );
                 while($p3!="|" && !feof($f) )
                 {
                  $user1=$user1.$p3;    
                  $p3=fgetc($f); 

                 } 
                  $p1=fgetc($f);
                    while($p1!="|" && !feof($f))
                     { $pass=$pass.$p1; 
                       $p1=fgetc($f); 
                     }
                     $p2=fgetc($f);
                    while($p2!="|" && !feof($f))
                      {
                        $q=$q.$p2;  
                        $p2=fgetc($f);  
                      }
                      var_dump($user1 == $name);
                    var_dump($password == $pass);
                    echo "<br/> $user1 <br/>";
                    echo "$pass <br/>";
                    echo "compare <br/>";
                    echo "$name <br/>";
                    echo "$password <br/>";
                 if( $name == $user1 && $password == $pass )
                   {  echo "succeeded <br/>";
                      echo "droit: $q <br/>";
                      echo "$res <br/>";
                        $res="1";
                        echo "$res <br/>";
                        break 2;
                    }

                    unset($p3);
                    unset($p1);
                    unset($p2);
                    unset($user1);
                    unset($pass);
                    unset($q);
             }
    fclose($f);
    if($res == "1")
        { echo "ok <br/>";}
    else 
        { echo " error <br/>";}

  echo " fin <br/>"
    ?>

my file.txt

abc|abc|87|
aaa|ccc|45|
ghi|ghi|67

now if i enter login abc password abc , iget this result in my page

 begin
abc
abc
begin while
bool(true) bool(true)
abc
abc
compare
abc
abc
succeeded
droit: 87
0
1 

now if i enter login aaa and password ccc , i get

begin
aaa
ccc
begin while
bool(false) bool(false)
abc
abc
compare
aaa
ccc
begin while
bool(false) bool(true)
aaa
ccc
compare
aaa
ccc
begin while
bool(false) bool(false)
ghi
ghi
compare
aaa
ccc
error
fin 

and even for the third user line i get an error even though the variable are equal.

I appreciate it, if any one could help me . and thanks in advance

Emb_user
  • 249
  • 1
  • 2
  • 7
  • 1
    The code looks like it was ported from (or, at least, thought on) C. PHP provides several very handy functions to work with files and strings. Read about [`file()`](http://php.net/manual/en/function.file.php) and [`explode()`](http://php.net/manual/en/function.explode.php), they can help you read the file and split the lines in pieces in a couple of lines of code. – axiac Apr 21 '15 at 12:34
  • To be honest - your code is messy and it's hard to understand what is going on. If you want someone to help you, at least make sure your code is organized. – Ofir Baruch Apr 21 '15 at 12:35

2 Answers2

0

Looks like your $user1 is corrupted... are you sure there are no char after 'aaa' ? like 'aaa ' ? try to trim it : trim($name) == trim($user1)... otherwise, try to compare "$name" == "$user1" ?

Random
  • 3,158
  • 1
  • 15
  • 25
0

You get this result because you have newlines on your text file.

As the second user is parsed the first character after the pipe | is a newline and becomes the first character of $user1

You don't see that on your output because you're looking at it in your browser.

You can quick fix that by trimming $user1 as "Random" suggested.

Or instruct your code to skip newlines in the first block.

while(!feof($f))
{
    echo "begin while <br/> ";

    $p3=fgetc($f );
    while( $p3 == "\n" || $p3 == "\r" )
    {
        $p3=fgetc( $f );
    }
    // ....
Paolo
  • 15,233
  • 27
  • 70
  • 91