Jump to content


Photo

reading from file problem


  • Please log in to reply
3 replies to this topic

#1 maciek4

maciek4
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 28 March 2006 - 12:39 PM

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



#2 khendar

khendar
  • Members
  • PipPipPip
  • Advanced Member
  • 78 posts

Posted 28 March 2006 - 01:34 PM

Try print_r($lines) to see whether its reading the file properly.

BTW This is probably a major security hole...you could at least encrypt the values or better yet encrypt them and set them as constants in a PHP file.

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 28 March 2006 - 02:00 PM

When you read information from a file the resulting data includes the line terminating character(s). You need to compare against the TRIMed value:
<?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';
?>

Ken

#4 maciek4

maciek4
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 29 March 2006 - 08:55 AM

[!--quoteo(post=359273:date=Mar 28 2006, 04:00 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Mar 28 2006, 04:00 PM) View Post[/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:
<?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';
?>

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.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users