kcb Posted June 13, 2007 Share Posted June 13, 2007 I'm very new to PHP. In fact, I wasn't even sure what my search criteria would be. I just started reading the Zend tutorials last week and now I'm trying to make simple programs on my own. Everything has been great until I tried to program a log in script. I thought it'd be simple enough. The problem is, no matter what, it echos "Wrong!" even when the username and password are correct. I've tried everything and nothing seems to work. Also, I'm using PHP 4 on Apache 2.0. HTML form <html> <head> <title></title> </head> <body> <h2>Please login</h2> <br /> <form action="password.php" method="get"> Username: <input type="text" name="username" size="30"><br /><br /> Password: <input type="text" name="password" size="30"><br /><br /> <input type="submit" value="submit"> </form> </body> </html> PHP script <html> <head> <title></title> </head> <body> <?php $filehandle = 'UserandPass.txt' or die('Could not read file!'); $data = file($filehandle) or die('Could not read file!'); if ($_GET['username'] == $data[0] && $_GET['password'] == $data[1]) { $message = "welcome"; } else { $message = "Wrong!"; } echo $message; ?> </body> </html> UserandPass.txt brawley christmas Quote Link to comment https://forums.phpfreaks.com/topic/55425-solved-having-problems-w-a-simple-login-scripts-wheres-newbie-help/ Share on other sites More sharing options...
mr_zhang Posted June 13, 2007 Share Posted June 13, 2007 Try to trim() all variables inside the if() instead of: if ($_GET['username'] == $data[0] && $_GET['password'] == $data[1]) { change to if (trim($_GET['username']) == trim($data[0]) && trim($_GET['password']) == trim($data[1])) { Quote Link to comment https://forums.phpfreaks.com/topic/55425-solved-having-problems-w-a-simple-login-scripts-wheres-newbie-help/#findComment-273939 Share on other sites More sharing options...
Yesideez Posted June 13, 2007 Share Posted June 13, 2007 I'd also use method of POST rather than GET. GET appends the form data onto the URL whereas POST sends a packet of data instead and can't be seen. Quote Link to comment https://forums.phpfreaks.com/topic/55425-solved-having-problems-w-a-simple-login-scripts-wheres-newbie-help/#findComment-273943 Share on other sites More sharing options...
kcb Posted June 13, 2007 Author Share Posted June 13, 2007 Try to trim() all variables inside the if() instead of: if ($_GET['username'] == $data[0] && $_GET['password'] == $data[1]) { change to if (trim($_GET['username']) == trim($data[0]) && trim($_GET['password']) == trim($data[1])) { Thanks a lot, man. That worked. I'm going to google trim(). Either it wasn't mentioned in the Zend tutorial or I haven't quite made it that far yet. Either way, thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/55425-solved-having-problems-w-a-simple-login-scripts-wheres-newbie-help/#findComment-273948 Share on other sites More sharing options...
kcb Posted June 13, 2007 Author Share Posted June 13, 2007 I'd also use method of POST rather than GET. GET appends the form data onto the URL whereas POST sends a packet of data instead and can't be seen. You're absolutely right. I'll definitely change that. Thanks for your input. Quote Link to comment https://forums.phpfreaks.com/topic/55425-solved-having-problems-w-a-simple-login-scripts-wheres-newbie-help/#findComment-273949 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.