0% found this document useful (0 votes)
31 views4 pages

Ex 11

The document outlines the design of a PHP-based authentication web page using MySQL, including components such as a login form (index.php), database connection (db.php), user authentication (login.php), a welcome page (welcome.php), and logout functionality (logout.php). It provides a database setup script to create a user table and insert a sample user. The login process includes session management and password verification using hashed passwords.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views4 pages

Ex 11

The document outlines the design of a PHP-based authentication web page using MySQL, including components such as a login form (index.php), database connection (db.php), user authentication (login.php), a welcome page (welcome.php), and logout functionality (logout.php). It provides a database setup script to create a user table and insert a sample user. The login process includes session management and password verification using hashed passwords.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Ex 11: Design a authentication web page in PHP with MYSQL to check user name and password

 A login form (index.php)


 A database connection (db.php)
 User authentication (login.php)
 A welcome page (welcome.php)
 Logout functionality (logout.php)
Database Setup
CREATE DATABASE auth_db;
USE auth_db;
CREATE TABLE users
(
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL
);
-- Insert a sample user (password is "password", hashed)
INSERT INTO users (username, password) VALUES
('admin', ‘Test@123');
Database Connection (db.php)
<?php
$host = "localhost";
$user = "root"; // Change if you have a different MySQL username
$pass = ""; // Change if your MySQL has a password
$dbname = "auth_db";
$conn = new mysqli($host, $user, $pass, $dbname);
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
?>
Login Form (index.php)
<?php
session_start();
if (isset($_SESSION["username"]))
{
header("Location: welcome.php");
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<form action="login.php" method="post">
<label>Username:</label>
<input type="text" name="username" required>
<br>
<label>Password:</label>
<input type="password" name="password" required>
<br>
<button type="submit">Login</button>
</form>
</body>
</html>
Login Processing (login.php)
<?php
session_start();
include "db.php";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$password = $_POST["password"];
$stmt = $conn->prepare("SELECT password FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0)
{
$stmt->bind_result($hashed_password);
$stmt->fetch();
if (password_verify($password, $hashed_password))
{
$_SESSION["username"] = $username;
header("Location: welcome.php");
exit();
}
Else
{
echo "Invalid password!";
}
}
else
{
echo "User not found!";
}

$stmt->close();
}
$conn->close();
?>
Welcome Page (welcome.php)
<?php
session_start();
if (!isset($_SESSION["username"]))
{
header("Location: index.php");
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Welcome</title>
</head>
<body>
<h2>Welcome, <?php echo $_SESSION["username"]; ?>!</h2>
<a href="logout.php">Logout</a>
</body>
</html>
Logout (logout.php)
<?php
session_start();
session_destroy();
header("Location: index.php");
exit();
?>

You might also like