maciek4 Posted March 28, 2006 Share Posted March 28, 2006 I have a simple login form id and password and I want to read 2 lines from the file password.txtmy 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 hereecho'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 ornot, 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 thefile. Thanks Link to comment https://forums.phpfreaks.com/topic/6005-reading-from-file-problem/ Share on other sites More sharing options...
khendar Posted March 28, 2006 Share Posted March 28, 2006 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. Link to comment https://forums.phpfreaks.com/topic/6005-reading-from-file-problem/#findComment-21560 Share on other sites More sharing options...
kenrbnsn Posted March 28, 2006 Share Posted March 28, 2006 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]<?phpif (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 https://forums.phpfreaks.com/topic/6005-reading-from-file-problem/#findComment-21574 Share on other sites More sharing options...
maciek4 Posted March 29, 2006 Author Share Posted March 29, 2006 [!--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]<?phpif (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 https://forums.phpfreaks.com/topic/6005-reading-from-file-problem/#findComment-21875 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.