nitrozero Posted July 17, 2010 Share Posted July 17, 2010 Hello, i cannot seem to get this PHP code to work. I have tried to solve it but with no luck. I use xxamp to test it on a localhost and scite to write my PHP. This is supposed to be a form to enter your password and username to enter a secure page. The problem is that the final else statement always occurs from the beginning and even when the proper information is entered the page does not change. I am relatively new to PHP and cannot find the error, please help! thanks <?php //define allowed username/password pair define ('ALLOWED_USER', 'administrator'); define ('ALLOWED_PASS', 'abc123'); //create reference to form program skeletn showing structure of if statements $form =& $_REQUEST; if ( ($form['username'] == ' ' ) || ($form['password'] == ' ' ) ) { //show login form ?> <html> <head><title> CHapter 6 :: Example 6 :: Login Form</title></head> <body> <h2>login</h2> <form action="<?php echo $PHP_SELF ?>" method="POST"> username: <input type="text' name="username"><br /> password: <input type="password" name="password"> <input type="submit: value="login"> </form> </body> </html> <?php //end of login } else { if ( ($form['username'] == ALLOWED_USER) && ($form['password'] == ALLOWED_PASS) ) { //user verified OK; return potected page ?> <html> <head><title> Protected page </title></head> <body> <h2>login successful</h2> This page would contain protected information, such as management facilies for this site, information intended for only a certain person or group of people, etc. </body> </html> <?php //end of protected page } else { //user provided bad login info; return login form with error message. ?> <html> <head><title>Login form-Error</title></head> <body> <h2> Login </h2> <h4> the username and password you entered was not valid. </h4> <form action="<?php echo $PHP_SELF ?>" method="post"> Username: <input type="text" name="username"><br /> password: <input type="password" name="password"> <input type="submit" value="login"> </form> </body> </html> <?php //end ofloginform with error message } //end if } //end if ?> Link to comment https://forums.phpfreaks.com/topic/208053-passwordusername-conditonals-practice-help/ Share on other sites More sharing options...
Pikachu2000 Posted July 17, 2010 Share Posted July 17, 2010 Try changing: if ( ($form['username'] == ' ' ) || ($form['password'] == ' ' ) ) To: if ( empty($form['username']) || empty($form['password']) ) Link to comment https://forums.phpfreaks.com/topic/208053-passwordusername-conditonals-practice-help/#findComment-1087567 Share on other sites More sharing options...
nitrozero Posted July 17, 2010 Author Share Posted July 17, 2010 I tried changing that infomation and it solved part of the problem but made another, it no longer shows the final else statement but now it shows three boxs to enter information but there are only supposed to be two. Link to comment https://forums.phpfreaks.com/topic/208053-passwordusername-conditonals-practice-help/#findComment-1087573 Share on other sites More sharing options...
Pikachu2000 Posted July 17, 2010 Share Posted July 17, 2010 It didn't actually "make" another problem, it just revealed more problems. Double check your quoting and syntax, and change action="<?php echo $_SERVER['PHP_SELF']; ?>" to action="". When a form submits to itself, it's better practice to leave the action empty, since default is to submit to itself. Using $_SERVER['PHP_SELF'] is a security risk. <form action="" method="POST"> username: <input type="text' name="username"><br /> password: <input type="password" name="password"> <input type="submit: value="login"> </form> [/code] Link to comment https://forums.phpfreaks.com/topic/208053-passwordusername-conditonals-practice-help/#findComment-1087583 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.