How to create a simple login and registration form in PHP?

Member

by rollin , in category: PHP , 2 years ago

How to create a simple login and registration form in PHP?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by yadira.tillman , 2 years ago

@rollin You can see below my simple login page example in PHP. My simple html registration form has only 2 fields: username and password. You can try play with this simple login page example: username: john and password: test as a valid user here. You will probably store this data in database and get user data from there by username.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<html lang="en">
<head>
    <title>Simple Login Page</title>
</head>

<?php
// This data usually stored in database.
$expectedUsername = 'john';
$expectedPassword = md5('test');

$username = $_POST['username'];
$password = $_POST['password'];

$result = '';

if (isset($_POST["login"])) {
    if ($username === $expectedUsername && md5($password) === $expectedPassword) {
        $result = "You are successfully logged in";
    } else {
        $result = "Username and/or password incorrect";
    }
}
?>

<body>
<div id="login-container">
    <h1>Login</h1>
    <form action="" method="post">
        <p>
            <label for="username">Username: </label>
            <input type="text" name="username" id="username" required="required" value="<?= $username; ?>"/>
        </p>
        <p>
            <label for="password">Password</label>
            <input type="text" name="password" id="password" required="required"
                   value="<?= $password; ?>"/>
        </p>
        <p><?= $result ?></p>
        <input type="submit" name="login" value="Login"/>
    </form>
</div>
</body>
</html>


Member

by madalyn , a year ago

@rollin 

To create a simple login and registration form in PHP, follow these steps:

  1. Create a database: First, create a database to store user information. You can use a tool like phpMyAdmin to create a database and a table to store user information.
  2. Create the HTML form: Create two forms, one for registration and one for login. The registration form should have fields for the user's name, email, username, and password. The login form should have fields for the user's username and password.
  3. Add PHP code: In the PHP code, you need to write code to handle form submissions and store the data in the database. Here's an example of how to handle a registration form submission:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
if($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get values from form
    $name = $_POST['name'];
    $email = $_POST['email'];
    $username = $_POST['username'];
    $password = $_POST['password'];

    // Insert into database
    $sql = "INSERT INTO users (name, email, username, password) VALUES ('$name', '$email', '$username', '$password')";
    if(mysqli_query($conn, $sql)) {
        // Registration successful
    } else {
        // Registration failed
    }
}


  1. Validate user input: You should also add code to validate user input, such as checking if the email is in a valid format and if the username is already taken.
  2. Create login functionality: To handle the login form, you need to check if the username and password match the data stored in the database. Here's an example of how to do that:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
if($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get values from form
    $username = $_POST['username'];
    $password = $_POST['password'];

    // Check if user exists
    $sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
    $result = mysqli_query($conn, $sql);
    if(mysqli_num_rows($result) == 1) {
        // Login successful
    } else {
        // Login failed
    }
}


  1. Secure the form: Finally, make sure to add security measures to prevent attacks like SQL injection and cross-site scripting (XSS). You can use prepared statements and sanitize user input to prevent these types of attacks.


Note that this is just a basic example of how to create a login and registration form in PHP. Depending on your specific requirements, you may need to add additional functionality and security measures.