0

i have to make a log in form that displays a msg if the user uses the username 'letmein'

if not, a msg will also be printed.

But the message to be printed aren't showing.

Here's my code:

<html>
<head>

<?php

if (isset($_POST['submit'])) {

$username = $_POST['username'];

if ($username = 'letmein') {
print ("ACCESS GRANTED. WELCOME!!");
}
else {
print ("INVALID USERNAME. ACCESS DENIED");
	}
}

?>

</head>
<body bgcolor="#FFFFFF">

<center>
<form method="POST" action="login.php">

	<input type="text" name="username">
	<br><br>
	<input type="submit" value="Login">
	
</form>
</center>

</body>
</html>
3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by dietdew12z
0

correct this line to (==) if ($username == 'letmein') { = means assignment operator.
== means comparision operator.

Edited by Shanti C: n/a

0

Remember that PHP code gets evaluated and the echo/print statements are sent to the HTML file that gets delivered to your browser.

If you view the page you have there(for a successful login) it would read:

<html>
<head>
ACCESS GRANTED. WELCOME!!
</head>
<body bgcolor="#FFFFFF">

<center>
<form method="POST" action="login.php">

	<input type="text" name="username">
	<br><br>
	<input type="submit" value="Login">
	
</form>
</center>

</body>
</html>

So the print statement is getting sent to your header.

Move your PHP code to in between the <body> tags and it should work better.

html>
<head>

</head>
<body bgcolor="#FFFFFF">
<?php

if (isset($_POST['submit'])) {

$username = $_POST['username'];

if ($username == 'letmein') {
print ("ACCESS GRANTED. WELCOME!!");
}
else {
print ("INVALID USERNAME. ACCESS DENIED");
	}
}

?>


<center>
<form method="POST" action="login.php">

	<input type="text" name="username">
	<br><br>
	<input type="submit" value="Login">
	
</form>
</center>

</body>
</html>

Oh, and what Shanti said. :) Sorry, I missed that.

David

Edited by dietdew12z: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.