DEV Community

Cover image for LOGIN SYSTEM IN PHP AND MYSQL: User Authentication system.
Marwa El mekkaoui
Marwa El mekkaoui

Posted on

LOGIN SYSTEM IN PHP AND MYSQL: User Authentication system.

hackathon - OFPPT

User authentication in web developemen is used to authorized and restrict users to certain pages in a web appplication.

here are the github repository
github

REGISTERATION SYSTEM

DATABASE TABLE IN MYSQL

The database used is MySQL, so you'll need a MySQL database to run create the users table.
Run the hackathon.sql file in MySQL database to create users table.

CONFIGURATION FILE

The PHP script to connect to the database is in config.php directory.
Replace credentials to in config.php to match your server credentials.

<?php

$host = 'localhost';
$db   = 'Hackathon';
$user = 'root';
$pass = '';
$charset = 'UTF8';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";

// set the PDO error mode to exception and the default fetch mode to associative arrays
$options = [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
];

try {
    $conn = new PDO($dsn, $user, $pass, $options);

} catch (PDOException $e) {
    echo 'Connextion failed' . $e->getMessage();
}
Enter fullscreen mode Exit fullscreen mode

REGISTERATION FORM AND SCRIPT

The register.php creates a web form that allows users to register themselves.
The script generates error if form input is empty and username is has been taking already by another user.

Image description

LOGIN SYSTEM

LOGIN FORM AND SCRIPT

login.php is the login script.
When a user submit a form with the input of username and password, these inputs will be verified against the credentials data stored in the database, if there is a match then the user will be authorized and granted access to site or page.

Image description

Image description

DASHBOARD PAGE

User is redirected to dashboard.php if login is successful.

Image description

LOGIN OUT

logout.php log out the user and destroy all his sessions.

SECURITY

  • always hash the password before stoke it in the database.
    we use functions password_hash() for hashing
    and password_verify() for check for the hashed password.

  • we use the function filter_input() its similar to htmlspecialchar(), to validate variables from insecure sources, such as user input.

  • use sessions to store and persist user-specific data across multipile pages.

Top comments (1)

Collapse
 
yasserro profile image
Yassir

تبارك الله عليكم
Image description