[go: up one dir, main page]

0% found this document useful (0 votes)
7 views5 pages

Assignment 04

The document outlines a user registration and login system using PHP and MySQL. It includes SQL commands to create a 'users' table, HTML forms for registration and login, and PHP scripts to handle user registration, login validation, and session management. Additionally, it provides a welcome page for logged-in users and a logout functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Assignment 04

The document outlines a user registration and login system using PHP and MySQL. It includes SQL commands to create a 'users' table, HTML forms for registration and login, and PHP scripts to handle user registration, login validation, and session management. Additionally, it provides a welcome page for logged-in users and a logout functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Create Users Tablesql

USE session_management;

CREATE TABLE users (


id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE
);
register.html
html
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
</head>
<body>
<h2>Register</h2>
<form action="register.php" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br>
<button type="submit">Register</button>
</form>
</body>
</html>
register.php
php
<?php
session_start();

// Database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "session_management";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$user = $_POST['username'];
$email = $_POST['email'];
$pass = password_hash($_POST['password'], PASSWORD_BCRYPT);

$sql = "INSERT INTO users (username, email, password) VALUES ('$user', '$email', '$pass')";

if ($conn->query($sql) === TRUE) {


$_SESSION['message'] = 'Registration successful. Please log in.';
header("Location: login.html");
exit();
} else {
$_SESSION['message'] = 'Registration failed. Please try again later.';
header("Location: register.html");
exit();
}

$conn->close();
}
?>
login.html
html
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<form action="login.php" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br>
<button type="submit">Login</button>
</form>
</body>
</html>
login.php
php
<?php
session_start();
// Database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "session_management";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$user = $_POST['username'];
$pass = $_POST['password'];

$sql = "SELECT * FROM users WHERE username='$user'";


$result = $conn->query($sql);

if ($result->num_rows == 1) {
$row = $result->fetch_assoc();
if (password_verify($pass, $row['password'])) {
$_SESSION['username'] = $user;
header("Location: welcome.php");
exit();
} else {
$_SESSION['message'] = 'Incorrect password. Please try again.';
header("Location: login.html");
exit();
}
} else {
$_SESSION['message'] = 'User not found. Please register.';
header("Location: register.html");
exit();
}
}

$conn->close();
?>
welcome.php
php
<?php
session_start();
if (!isset($_SESSION['username'])) {
$_SESSION['message'] = 'You must log in first.';
header("Location: login.html");
exit();
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h2>Welcome, <?php echo $_SESSION['username']; ?>!</h2>
<p><a href="logout.php">Logout</a></p>
</body>
</html>
logout.php
php
<?php
session_start();
session_unset();
session_destroy();
header("Location: login.html");
exit();
?>

You might also like