PHP Simple Login Form | PHP Tutorial for Beginners

If you know how to create a website in PHP and you need to allow a user or admin to log in, the first thing that comes to mind is creating a PHP login script or login form in PHP. For login, the user must enter their username and password. To verify a successful login, we check those credentials against a MySQL database table containing all usernames and passwords.

In this tutorial we will show you step-by-step creation of your first PHP login form. Basically we create one simple login form in HTML and connect it with a MySQL database and PHP script.

You can also use JavaScript validation or jQuery validation for your login form.

Step 1 — Create the users table

CREATE TABLE users(
  id       int NOT NULL AUTO_INCREMENT,
  username varchar(16),
  password char(16),
  PRIMARY KEY(id),
  UNIQUE (username)
)

Step 2 — Create config.php

<?php
$database = "mydata";    // database name
$server   = "localhost"; // server
$db_user  = "root";      // MySQL username
$db_pass  = "";          // MySQL password
$table    = "users";     // users table

$link = mysql_connect($server, $db_user, $db_pass);
mysql_select_db($database, $link);
?>

Step 3 — Create signup.php

This file handles new user registration — it inserts a new username and password into the database.

<?php
include("config.php");

if (!empty($_POST['username']) && !empty($_POST['password'])) {
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);

    $query = "INSERT INTO $table (username, password)
              VALUES ('$username', '$password')";

    if (mysql_query($query, $link)) {
        echo "Registration successful!";
    } else {
        echo "Registration failed: " . mysql_error();
    }
}
?>
<html>
<body>
<form action="signup.php" method="POST">
  <label>Username: <input type="text" name="username"></label><br>
  <label>Password: <input type="password" name="password"></label><br>
  <input type="submit" value="Sign Up">
</form>
</body>
</html>

Step 4 — Create login.php

This file checks the submitted username and password against the database and starts a session on success.

<?php
session_start();
include("config.php");

if (!empty($_POST['username']) && !empty($_POST['password'])) {
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);

    $query  = "SELECT * FROM $table WHERE username='$username'
               AND password='$password'";
    $result = mysql_query($query, $link);

    if (mysql_num_rows($result) == 1) {
        $_SESSION['username'] = $username;
        header("Location: dashboard.php");
        exit();
    } else {
        echo "Invalid username or password.";
    }
}
?>
<html>
<body>
<form action="login.php" method="POST">
  <label>Username: <input type="text" name="username"></label><br>
  <label>Password: <input type="password" name="password"></label><br>
  <input type="submit" value="Login">
</form>
</body>
</html>

Step 5 — Create dashboard.php

This is the protected page shown after a successful login. If the user is not logged in, redirect them back to the login page.

<?php
session_start();
if (!isset($_SESSION['username'])) {
    header("Location: login.php");
    exit();
}
echo "Welcome, " . $_SESSION['username'] . "!";
?>

Hope this PHP tutorial is useful for you. Keep following PHP Tutorial for Beginners for more help.

Newer Post →