How To Connect Login And Signup Page In Php

Hey there, fellow developers! Today, I want to share my experience and knowledge about connecting a login and signup page in PHP. As a developer, I’ve come across many projects that require user authentication and registration, so I understand the importance of a seamless and secure connection between these two pages. In this article, I will guide you through the process step by step, providing personal insights and tips along the way.

Step 1: Setting up the Database

Before we dive into the code, it’s crucial to have a database set up to store user information. We’ll need a table to hold user details, such as usernames, emails, and encrypted passwords. For this article, let’s assume you have already created a database called “users” with a table named “user_info”.

Here’s an example of the structure for the “user_info” table:


CREATE TABLE user_info (
id INT(11) AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL
);

Step 2: Creating the Signup Page

First, let’s create the signup page where users can register for an account. This page will contain a form with input fields for username, email, and password. We’ll validate the input data and insert it into the database upon submission.

Here’s a basic example of the signup page (signup.php):


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

<label for="email">Email:</label>
<input type="email" id="email" name="email" required>

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

<input type="submit" value="Sign up">
</form>

Next, let’s create the signup_handler.php file, which will handle the form submission:


<?php
// Connect to the database
$conn = new mysqli("localhost", "username", "password", "users");

// Check if the connection is successful
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Retrieve form data
$username = $_POST['username'];
$email = $_POST['email'];
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);

// Insert data into the user_info table
$sql = "INSERT INTO user_info (username, email, password) VALUES ('$username', '$email', '$password')";
if ($conn->query($sql) === TRUE) {
echo "Sign up successful!";
} else {
echo "Error: " . $sql . "
" . $conn->error;
}

// Close the database connection
$conn->close();
?>

That’s it for the signup page! Users can now register for an account, and their information will be securely stored in the database.

Step 3: Creating the Login Page

Now that we have the signup page set up, let’s move on to creating the login page. This page will allow users to enter their credentials and log into their account.

Here’s an example of the login page (login.php):


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

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

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

Next, let’s create the login_handler.php file, which will handle the form submission and authenticate the user:


<?php
// Start a session
session_start();

// Connect to the database
$conn = new mysqli("localhost", "username", "password", "users");

// Check if the connection is successful
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Retrieve form data
$username = $_POST['username'];
$password = $_POST['password'];

// Retrieve user info from the database
$sql = "SELECT * FROM user_info WHERE username='$username'";
$result = $conn->query($sql);

// Check if the user exists and the password is correct
if ($result->num_rows == 1) {
$row = $result->fetch_assoc();
if (password_verify($password, $row['password'])) {
$_SESSION['username'] = $username;
echo "Login successful!";
} else {
echo "Invalid password!";
}
} else {
echo "User not found!";
}

// Close the database connection
$conn->close();
?>

Great job! Now, users can log in using their credentials, and their session will be authenticated.

Conclusion

In this article, we delved into the process of connecting login and signup pages in PHP. We covered setting up the database, creating the signup page, and handling form submissions. We also discussed creating the login page, authenticating users, and setting up sessions.

Remember, user authentication and registration are essential components of many web applications. By following these steps and customizing the code to suit your specific needs, you can create a secure and user-friendly login and signup system for your projects.

Happy coding!