Jump to content

[SOLVED] Compare user input to flat file data


wobblyfrogs

Recommended Posts

Help...Am a complete newbie to programming so my code is prolly quite long. Am trying to verify a users details entered into a login form against details stored in an existing flat file.

No matter what I input, I get "not registered", even if it does match.

What have I done wrong? I have spent 3 LOOng afternoons trying to nut this out..now am going nuts....

 

<?php

 

if ($_POST['submit']=="submit")

{

$errormessage = "";

if (empty($_POST['userName']) ||empty($_POST['password']))

{                 

  $errormessage = "<p>Please enter a user name and password</p>";

}

else

{

$userName=trim($_POST['userName']);

$password=trim($_POST['password']);

 

// check user name and password

$nameFlag=false;

$passwordFlag=false;

 

  $fp = fopen($_SERVER['DOCUMENT_ROOT']."/toys/users.txt","r" );

 

  while (!feof($fp))

{

  $line=fgets($fp,100);

 

($userName== $line? $nameFlag=true : $nameFlag=false);

 

($password==$line? $passwordFlag=true : $passwordFlag=false);

 

}

 

fclose($fp);

 

 

if($nameFlag && $passwordFlag==true)

{

$foundFlag= true;

}

else

{

$foundFlag=false;

}

($foundFlag==true?$welcome= "Welcome ".$userName." you are now logged in":$sorry= "Sorry  you are not registered");

 

}

 

 

}

 

 

?>

// my html form below

<form method="post" action="<?=$_SERVER['PHP_SELF']?>" >

          <fieldset class="outline">

          <h3>Registered customers</h3>

          <label>User Name:</label>

          <input type="text" class="login" value="" name="userName" id="userName" />

          <label>Password:</label>

          <input type="text" class="login" value="" name="password" id="password"/>

          <br />

          <br />

          <input type="submit" value="submit" name="submit" id="submit"/>

          <br />

          <?=$errormessage?>

          <br />

          </fieldset>

        </form>

Hi wobblyfrogs,

 

The problem is with this line:

 

if($nameFlag && $passwordFlag==true)

 

The syntax is incorrect, when evaluating more than one variable in a single if statement (a multiple conditional statement) you must write it in the following way:

 

if($nameFlag==true && $passwordFlag==true)

 

Hope this helps.

Is my logic even correct?

 

Erm... no. I don't believe so.

 

$line=fgets($fp,100);
($userName== $line? $nameFlag=true : $nameFlag=false);
($password==$line? $passwordFlag=true : $passwordFlag=false);   

 

You read in a line from the file, you then compare $usename to that line, followed by $password, the only way this could ever return true would be if the userName and password are the same, which doesn't sound right to me. Even if that was the case there would be no need to compare them both as if they were the same and one matched, theres no point checking the other. What exactly does your txt file look like? Is it something like...

 

username1, password1
username2, password2
...
username1100, password1100

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.