Register and Login With​ Using PHP 7

Register and Login With​ Using PHP 7


https://drive.google.com/file/d/1nbPi3ibXY47kAYQqIBTDzWHPb8jOQUcs/view?usp=sharing

1. Database Schema (SQL)

CREATE TABLE `user_login` ( `id` INT(6) AUTO_INCREMENT PRIMARY KEY, `username` VARCHAR(40) NOT NULL, `email` VARCHAR(50) NOT NULL UNIQUE, `phone_number` VARCHAR(15) NOT NULL, `password` VARCHAR(255) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2. Database Connection (db.php)

<?php $host = 'localhost'; // Change this if using a live server $dbname = 'db_login'; // Your database name $username = 'root'; // Your database username $password = ''; // Your database password try { $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { die("Database connection failed: " . $e->getMessage()); } ?>

3. Error Handling (errors.php)

<?php if (!empty($errors)): ?> <div class="error"> <?php foreach ($errors as $error): ?> <p><?= htmlspecialchars($error) ?></p> <?php endforeach ?> </div> <?php endif ?>

4. Login (login.php)

<?php session_start(); require 'db.php'; $errors = []; if ($_SERVER["REQUEST_METHOD"] == "POST") { $email = trim($_POST['email']); $password = $_POST['password']; if (empty($email) || empty($password)) { $errors[] = "Email and password are required."; } else { $stmt = $pdo->prepare("SELECT id, username, password FROM user_login WHERE email = ?"); $stmt->execute([$email]); $user = $stmt->fetch(); if ($user && password_verify($password, $user['password'])) { $_SESSION['user_id'] = $user['id']; $_SESSION['username'] = $user['username']; header("Location: dashboard.php"); exit; } else { $errors[] = "Invalid email or password."; } } } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Login</title> </head> <body> <?php include 'errors.php'; ?> <form method="POST"> <input type="email" name="email" placeholder="Email" required> <input type="password" name="password" placeholder="Password" required> <button type="submit">Login</button> </form> </body> </html>

5. Logout (logout.php)

<?php session_start(); session_destroy(); header("Location: login.php"); exit; ?>

6. Registration (register.php)

<?php require 'db.php'; $errors = []; if ($_SERVER["REQUEST_METHOD"] == "POST") { $username = trim($_POST['username']); $email = trim($_POST['email']); $phone_number = trim($_POST['phone_number']); $password_1 = $_POST['password_1']; $password_2 = $_POST['password_2']; if ($password_1 !== $password_2) { $errors[] = "Passwords do not match."; } if (empty($errors)) { $hashed_password = password_hash($password_1, PASSWORD_DEFAULT); $stmt = $pdo->prepare("INSERT INTO user_login (username, email, phone_number, password) VALUES (?, ?, ?, ?)"); $stmt->execute([$username, $email, $phone_number, $hashed_password]); header("Location: login.php"); exit; } } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Register</title> </head> <body> <?php include 'errors.php'; ?> <form method="POST"> <input type="text" name="username" placeholder="Username" required> <input type="email" name="email" placeholder="Email" required> <input type="text" name="phone_number" placeholder="Phone Number" required> <input type="password" name="password_1" placeholder="Password" required> <input type="password" name="password_2" placeholder="Confirm Password" required> <button type="submit">Register</button> </form> </body> </html>

7. Dashboard (dashboard.php)

<?php session_start(); if (!isset($_SESSION['user_id'])) { header("Location: login.php"); exit; } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Dashboard</title> </head> <body> <h1>Welcome, <?= htmlspecialchars($_SESSION['username']) ?></h1> <a href="logout.php">Logout</a> </body> </html>

8. File Organization

Organize your files as follows:

/public_html ├── db.php ├── login.php ├── logout.php ├── register.php ├── dashboard.php └── errors.php

9. Testing the Website

  1. Upload all files to your web server (public_html or htdocs folder).

  2. Ensure the database connection db.php is correctly configured for your server's settings.

  3. Open the website in your browser.

    • For login: http://yourdomain.com/login.php

    • For registration: http://yourdomain.com/register.php

    • After logging in, you will be redirected to the dashboard.

  4. Test creating new users and logging in with those credentials.

Final Recommendations

  • Enable SSL (HTTPS) for secure communication.

  • Set up error logging to avoid displaying sensitive information on the live site.

  • Backup your database regularly.

  • Enable session security (e.g., regenerate session IDs with session_regenerate_id(true)).

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close