How To Make Login Form in PHP

PHP (PHP: Hypertext Processor) is a very widely-used scripting language, and the reasons why are obvious:   PHP integrates perfectly with plain HTML documents, is supported by the vast majority of Web hosting providers and has an easy-to-learn syntax, which you can also find in JavaScript, in some cases.

In this tutorial, we'll be experimenting with one of the much appreciated features of PHP: form processing.


We begin by creating a plain HTML document, for this tutorial, we'll be using HTML5.

<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Simple Login Form</title>
</head>
<body>
</body>
</html>
Once that is done, we'll want to add the actual form to the code. Inside the <body> </body> tags, place this:
<form action="#" method="post">
            <input type="text" name="u" />
            <input type="password" name="p" />
            <input type="submit" name="submit" value="Login" />
</form>
Now the fun begins, under the closing </html> tag, place this PHP script. Don't worry, we'll explain the code using comments.

<php
//This declares two variables, 'username' and 'password', the values of which you can change.
$username = "username";
$password = "password";
//Now, since the script will be running on the same document as the form, we want to make sure it doesn't process information that isn't there.
if (!$_POST["u"] or !$_POST["p"])
{
}
else
{
//If there is information provided, we'll make sure that it corresponds to the variables above, and then display the appropriate message.
if ($_POST["u"] == $username and $_POST["p"] == $password)
{
echo "You have been logged in!";
}
else
{
echo "The username or password was incorrect";
}
}
?>
That's it. Simple as that! You now know how to make an HTML for and process it's data using PHP.

Post a Comment

Note: Only a member of this blog may post a comment.