0% found this document useful (0 votes)
30 views

Database

Uploaded by

sabriphillip15
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)
30 views

Database

Uploaded by

sabriphillip15
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/ 8

Database

CREATE TABLE users (

id INT(11) AUTO_INCREMENT PRIMARY KEY,

username VARCHAR(50) NOT NULL,

password VARCHAR(255) NOT NULL

);

Html

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Login Form</title>

<style>

body {

font-family: Arial, sans-serif;

background-color: #f4f4f4;

margin: 0;

padding: 100px;

form {

background: white;

padding: 20px;

border-radius: 5px;

box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);

input[type="text"], input[type="password"] {

width: 100%;
padding: 10px;

margin: 10px 0;

border: 1px solid #ccc;

border-radius: 5px;

input[type="submit"] {

background: #5cb85c;

color: white;

border: none;

padding: 10px;

border-radius: 5px;

cursor: pointer;

input[type="submit"]:hover {

background: #4cae4c;

</style>

</head>

<body>

<h2>Login Form</h2>

<form action="login.php" method="post">

<label for="username">Username:</label>

<input type="text" name="username" required>

<label for="password">Password:</label>

<input type="password" name="password" required>


<input type="submit" value="Login">

</form>

</body>

</html>

Php

<?php

// Database connection

$host = 'localhost'; // Database host

$dbname = 'user_db'; // Database name

$user = 'root'; // Database username

$pass = ''; // Database password

try {

$pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch (PDOException $e) {

die("Could not connect to the database $dbname :" . $e->getMessage());

// Start session

session_start();

// Check if form is submitted

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$username = $_POST['username'];

$password = $_POST['password'];
// Prepare and execute SQL statement

$stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");

$stmt->execute([$username]);

$user = $stmt->fetch();

// Verify password

if ($user && password_verify($password, $user['password'])) {

$_SESSION['username'] = $username;

echo "Login successful! Welcome, $username.";

} else {

echo "Invalid username or password.";

?>

Signup

Database

CREATE TABLE users (

id INT(11) AUTO_INCREMENT PRIMARY KEY,

username VARCHAR(50) NOT NULL UNIQUE,

password VARCHAR(255) NOT NULL

);

Html

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">


<title>Signup Form</title>

<style>

body {

font-family: Arial, sans-serif;

background-color: #f4f4f4;

margin: 0;

padding: 100px;

form {

background: white;

padding: 20px;

border-radius: 5px;

box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);

input[type="text"], input[type="password"] {

width: 100%;

padding: 10px;

margin: 10px 0;

border: 1px solid #ccc;

border-radius: 5px;

input[type="submit"] {

background: #5cb85c;

color: white;

border: none;

padding: 10px;

border-radius: 5px;

cursor: pointer;

}
input[type="submit"]:hover {

background: #4cae4c;

</style>

</head>

<body>

<h2>Signup Form</h2>

<form action="signup.php" method="post">

<label for="username">Username:</label>

<input type="text" name="username" required>

<label for="password">Password:</label>

<input type="password" name="password" required>

<input type="submit" value="Sign Up">

</form>

</body>

</html>

Php

<?php

// Database connection

$host = 'localhost'; // Database host

$dbname = 'user_db'; // Database name

$user = 'root'; // Database username

$pass = ''; // Database password


try {

$pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch (PDOException $e) {

die("Could not connect to the database $dbname :" . $e->getMessage());

// Check if form is submitted

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$username = $_POST['username'];

$password = password_hash($_POST['password'], PASSWORD_DEFAULT); // Hash the password

// Prepare and execute SQL statement to insert user

$stmt = $pdo->prepare("INSERT INTO users (username, password) VALUES (?, ?)");

try {

$stmt->execute([$username, $password]);

echo "User registered successfully!";

} catch (PDOException $e) {

if ($e->getCode() == 23000) {

// Unique constraint violation

echo "Username already taken. Please choose another.";

} else {

echo "Error: " . $e->getMessage();

?>
Session

<?php

// Start the session

session_start();

// Unset all of the session variables

$_SESSION = [];

// Destroy the session

session_destroy();

// Redirect to the login page

header("Location: login.html");

exit();

?>

Logout

<a href="logout.php">Logout</a>

You might also like