How To Connect Login Page To Database In Xampp

Hello there! Today I want to share with you a step-by-step guide on how to connect a login page to a database in XAMPP. As a web developer, I’ve found this process to be essential whenever I’m creating a web application that requires user authentication. So, let’s dive into it!

Setting up XAMPP

Before we start, make sure you have XAMPP installed on your machine. XAMPP is a popular software package that includes Apache, MySQL, and PHP components, making it ideal for local development.

If you haven’t installed XAMPP yet, head over to the official website and download it for your operating system. Once the installation is complete, launch XAMPP and start the Apache and MySQL services.

Creating the Database

Now that our server is up and running, it’s time to create the database that will store our login page data. Open up your favorite web browser and navigate to http://localhost/phpmyadmin. This will take you to the phpMyAdmin dashboard.

Click on the “New” button on the left sidebar to create a new database. Give your database a name, such as “myloginpage”. You can leave the default settings for character set and collation.

Creating the Login Page

Next, we need to create the login page itself. Open up your preferred code editor and create a new HTML file. You can name it login.html for simplicity.

In this file, you’ll need to add the necessary HTML and CSS code to create a basic login form. You can customize it to your liking, but make sure to include fields for username and password, as well as a submit button.

Here’s a simple example to get you started:


<html>
  <head>
    <title>Login Page</title>
    <style>
      body {
        margin: 0;
        padding: 0;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      form {
        display: flex;
        flex-direction: column;
        align-items: center;
      }
      input {
        margin-bottom: 10px;
      }
      button {
        background-color: #4CAF50;
        color: white;
        padding: 10px;
        border: none;
        cursor: pointer;
      }
    </style>
  </head>
  <body>
    <form action="login.php" method="post">
      <input type="text" name="username" placeholder="Username" required>
      <input type="password" name="password" placeholder="Password" required>
      <button type="submit">Login</button>
    </form>
  </body>
</html>

Handling the Login Request

Now that our login form is ready, it’s time to handle the login request and verify the user’s credentials against our database. For this, we’ll need to create a server-side script.

Create a new file in your code editor and save it as login.php. In this file, we’ll write the PHP code to connect to our database and perform the necessary checks.

Here’s an example of how the code could look:


<?php
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "myloginpage";
  
  // Create a new PDO instance
  $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  
  // Retrieve the submitted username and password
  $username = $_POST['username'];
  $password = $_POST['password'];
  
  // Prepare and execute the SQL query
  $stmt = $conn->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
  $stmt->bindParam(':username', $username);
  $stmt->bindParam(':password', $password);
  $stmt->execute();
  
  // Check if a row is found, meaning the credentials are valid
  if ($stmt->rowCount() == 1) {
    echo "Login successful!";
  } else {
    echo "Invalid username or password.";
  }
?>

Conclusion

Congratulations! You’ve learned how to connect a login page to a database in XAMPP. This is a crucial step in building secure web applications that require user authentication. Remember to always sanitize and validate user inputs to prevent any security vulnerabilities.

If you want to further enhance your login page, you can add features like password hashing, email verification, and password reset functionality. The possibilities are endless!

Now that you’re equipped with this knowledge, go ahead and start building your own login page with database integration. Happy coding!