Jump to content

reading from file problem


maciek4

Recommended Posts

I have a simple login form id and password and I want to read 2 lines from the file password.txt

my code:

<?php
$lines=file('password.txt');
if (isset ($_POST['id']) && isset ($_POST['pass']))
{
if ($_POST['id'] != $lines[0] && $_POST['pass'] != $lines[1])
{

echo'form<>' ; //form goes here
echo'try again';
}
else
{

$_SESSION['id'] = "$lines[0]";
$_SESSION['pass'] = "$lines[1]";
header("Location: edit.php");
exit();
}
}
else
{
echo'form<>'; //form goes here
}
?>

The file password.txt has 2 lines and as a result no matter if I type in the user id and password correctly or
not, I am getting try again message. What did I do wrong?

If I substitute $_POST['id'] != $lines[0] by $_POST['id'] !="admin" it works fine, but I wanted to read from the
file. Thanks

Link to comment
Share on other sites

When you read information from a file the resulting data includes the line terminating character(s). You need to compare against the TRIMed value:
[code]<?php
if (isset ($_POST['id']) && isset ($_POST['pass']))
   if ($_POST['id'] != trim($lines[0]) && $_POST['pass'] != trim($lines[1]))
   {
    echo'form<>'; //form goes here
    echo'try again';
?>[/code]

Ken
Link to comment
Share on other sites

[!--quoteo(post=359273:date=Mar 28 2006, 04:00 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Mar 28 2006, 04:00 PM) [snapback]359273[/snapback][/div][div class=\'quotemain\'][!--quotec--]
When you read information from a file the resulting data includes the line terminating character(s). You need to compare against the TRIMed value:
[code]<?php
if (isset ($_POST['id']) && isset ($_POST['pass']))
   if ($_POST['id'] != trim($lines[0]) && $_POST['pass'] != trim($lines[1]))
   {
    echo'form<>'; //form goes here
    echo'try again';
?>[/code]

Ken
[/quote]

It worked fine now. Thank you. This is my 2nd day learning php so I didn't know about this. trim($lines[0]) solved the problem.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.