How To Log In Php

Setting up the databasee will learn how to create a simple login system using PHP and MySQL. A login system is an essential component of any web application that requires user authentication. Let’s get started!

1. Setting up the database

First, we need to create a database and a table to store our user information. In this example, we will create a table named users with the following columns:

  • id – an auto-incrementing primary key
  • username – a unique username for the user
  • password – the user’s hashed password

Here’s the SQL query to create the table:

CREATE TABLE users (
        id INT AUTO_INCREMENT PRIMARY KEY,
        username VARCHAR(50) UNIQUE,
        password VARCHAR(255)
    );

2. Creating the login form

Next, let’s create a simple login form where users can enter their username and password. Save this HTML code in a file named login.php:

<form action="process_login.php" method="post">
        <label for="username">Username:</label>
        <input type="text" name="username" id="username" required>

        <label for="password">Password:</label>
        <input type="password" name="password" id="password" required>

        <input type="submit" value="Login">
    </form>

3. Processing the login

Now we need to create the PHP script that will process the login form data. Create a new file named process_login.php and add the following code:

<?php
    // Connect to the database
    $servername = "localhost";
    $username = "your_username";
    $password = "your_password";
    $dbname = "your_dbname";

    $conn = new mysqli($servername, $username, $password, $dbname);

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    // Check if the username and password are valid
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $input_username = $_POST["username"];
        $input_password = $_POST["password"];

        $sql = "SELECT id, password FROM users WHERE username = ?";
        $stmt = $conn->prepare($sql);
        $stmt->bind_param("s", $input_username);
        $stmt->execute();
        $stmt->store_result();
        
        if ($stmt->num_rows > 0) {
            $stmt->bind_result($id, $hashed_password);
            $stmt->fetch();

            if (password_verify($input_password, $hashed_password)) {
                // Start a new session and store the user ID
                session_start();
                $_SESSION["user_id"] = $id;

                echo "Login successful!";
            } else {
                echo "Invalid password.";
            }
        } else {
            echo "Invalid username.";
        }

        $stmt->close();
    }
    $conn->close();
    ?>

In the above code, we first establish a connection to the database. Then, we check if the submitted username exists in the users table. If it does, we use the password_verify() function to check if the submitted password matches the stored hashed password. If the password is correct, we start a new session and store the user’s ID in the $_SESSION variable.

4. Test the login system

Now you can test your login system by navigating to login.php. Enter a valid username and password and see if the system works as expected. Remember, you’ll need to insert at least one user into the users table, with a hashed password generated using password_hash() function.

Conclusion

In this blog post, we have learned how to create a simple login system using PHP and MySQL. This can be easily extended to include other features like user registration, password reset, and user profile management. Happy coding!