PHP MySQL Login

please click here for more wordpress cource

Sure, I can help you with that!

Here are the basic steps to create a PHP MySQL login system:

  1. Create a database in MySQL to store user information. You can use a tool like phpMyAdmin to create the database and tables.
  2. Create a PHP script to handle user authentication. This script will verify the user’s credentials against the database and create a session for the user if the login is successful.
  3. Create a login form in HTML that will allow users to enter their username and password.
  4. When the user submits the form, the data will be sent to the PHP script for authentication.
  5. If the user’s credentials are correct, the PHP script will create a session for the user and redirect them to the main page of your site. If the login is unsuccessful, the user will be shown an error message.

Here’s some sample code to get you started:

  1. Create the database:
CREATE DATABASE mydatabase;
USE mydatabase;

CREATE TABLE users (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(30) NOT NULL,
    password VARCHAR(30) NOT NULL,
    email VARCHAR(50),
    reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
  1. Create the PHP script:
<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $conn = mysqli_connect('localhost', 'username', 'password', 'mydatabase');
    $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
    $result = mysqli_query($conn, $query);
    if (mysqli_num_rows($result) == 1) {
        $_SESSION['username'] = $username;
        header('Location: index.php');
    } else {
        $error = "Invalid username or password";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
</head>
<body>
    <h2>Login</h2>
    <form method="POST" action="">
        <label>Username:</label><br>
        <input type="text" name="username"><br>
        <label>Password:</label><br>
        <input type="password" name="password"><br>
        <input type="submit" value="Login">
    </form>
    <?php if (isset($error)) { echo $error; } ?>
</body>
</html>
  1. Create the login form in HTML:
<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
</head>
<body>
    <h2>Login</h2>
    <form method="POST" action="login.php">
        <label>Username:</label><br>
        <input type="text" name="username"><br>
        <label>Password:</label><br>
        <input type="password" name="password"><br>
        <input type="submit" value="Login">
    </form>
</body>
</html>

Note that in the PHP script, you will need to replace “username” and “password” with the actual username and password for your MySQL database. Also, you should hash the password before storing it in the database for security reasons.

You may also like...

Popular Posts

Leave a Reply

Your email address will not be published. Required fields are marked *