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>

Recommended Answers

All 2 Replies

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.