Init commit

This commit is contained in:
Archie Fox
2024-06-08 14:22:07 +03:00
commit a9853c39cc
9 changed files with 201 additions and 0 deletions

0
.gitignore vendored Normal file
View File

17
classes/MyDB.php Normal file
View File

@@ -0,0 +1,17 @@
<?php
namespace classes;
use SQLite3;
class MyDB extends SQLite3
{
public function __construct()
{
$this->open('mysqlitedb.db');
}
}
$db = new MyDB();
$db->exec('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE, pass TEXT)');

45
index.php Normal file
View File

@@ -0,0 +1,45 @@
<?php
require_once __DIR__ . '/classes/MyDB.php';
$title = 'SQLite';
$res = '';
$name = $_POST['name'];
$pass = $_POST['pass'];
if ($name === '' || $pass === '') {
$res = 'All strings required!';
unset($_POST);
}
if ($_POST['submit'] === 'OK') {
global $db;
$result = $db->query("SELECT * FROM users WHERE name = '$name'");
$auth = $result->fetchArray();
if ($auth['name'] !== $name || password_verify($pass, $auth['pass']) !== true) {
$res = 'Login or password is wrong!';
}
session_start();
$_SESSION['name'] = $auth['name'];
$_SESSION['id'] = $auth['id'];
header("Location: secret.php");
exit();
}
?>
<?php require_once 'templates/header.php'; ?>
<body>
<div class="auth">
<h1>Authorization</h1>
<form action="/" method="post">
<p><input type="text" name="name" id="name" placeholder="Login"></p>
<p><input type="password" name="pass" id="pass" placeholder="Password"></p>
<p class="error"><?= $res; ?></p>
<p><input type="submit" value="OK" name="submit"></p>
</form>
<p><a href="registration.php">Registration</a></p>
</div>
<?php include_once 'templates/footer.php'; ?>

BIN
mysqlitedb.db Normal file

Binary file not shown.

43
registration.php Normal file
View File

@@ -0,0 +1,43 @@
<?php
require_once __DIR__ . "/classes/MyDB.php";
$title = 'Registration';
$res = '';
if ($_POST['name'] === '' || $_POST['pass'] === '') {
$res = 'All strings required!';
unset($_POST);
}
if ($_POST['submit'] === 'OK') {
$name = $_POST['name'];
$pass = password_hash($_POST['pass'], PASSWORD_DEFAULT);
global $db;
$result = $db->query("SELECT * FROM users WHERE name = '$name'");
$arr = $result->fetchArray();
if ($arr !== false) {
$res = "This name is already used!";
} else {
$db->exec("INSERT INTO users (name, pass) VALUES ('$name', '$pass')");
header("Location: index.php");
}
}
?>
<?php require_once 'templates/header.php'; ?>
<div class="auth">
<h1>Registration</h1>
<form action="registration.php" method="post">
<p><input type="text" name="name" id="name" placeholder="Name"></p>
<p><input type="password" name="pass" id="pass" placeholder="Password"></p>
<p class="error"><?= $res; ?></p>
<p><input type="submit" value="OK" name="submit"></p>
</form>
<p><a href="index.php">Authorization</a></p>
</div>
<?php include_once 'templates/footer.php'; ?>

28
secret.php Normal file
View File

@@ -0,0 +1,28 @@
<?php
session_start();
$title = 'Secret page';
if (!$_SESSION['id'] || !$_SESSION['name']) {
header("Location: index.php");
exit();
}
echo 'Your ID: ' . $_SESSION['id'] . "</br>";
echo 'Your Name: ' . $_SESSION['name'] . "</br>";
if ($_GET['do'] === 'logout') {
session_destroy();
header("Location: index.php");
exit();
}
?>
<?php require_once 'templates/header.php'; ?>
<div>
<a href="?do=logout">Logout</a>
</div>
<?php include_once 'templates/footer.php'; ?>

58
style.css Normal file
View File

@@ -0,0 +1,58 @@
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: #454444;
color: white;
}
.auth {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 20%;
margin-left: auto;
margin-right: auto;
width: 250px;
padding: 30px;
border: white solid 1px;
border-radius: 10px;
}
.auth h1 {
font-size: 22px;
}
input {
padding: 5px;
margin: 5px;
border-radius: 5px;
}
input[type=button], input[type=submit], input[type=reset] {
background-color: #04AA6D;
border: none;
color: white;
padding: 8px 16px;
text-decoration: none;
margin: 4px 2px;
cursor: pointer;
}
form {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.auth a {
color: white;
}
.error {
color: red;
}

2
templates/footer.php Normal file
View File

@@ -0,0 +1,2 @@
</body>
</html>

8
templates/header.php Normal file
View File

@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title><?= $title; ?></title>
</head>