Jump to content

password/username conditonals practice help


nitrozero

Recommended Posts

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 :D

 

<?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

 

?>

 

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]

Archived

This topic is now archived and is closed to further replies.

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