This commit is contained in:
erdeilevente 2023-01-27 11:57:08 +01:00
commit e33afe7c0f
9 changed files with 410 additions and 0 deletions

69
db/test_db.sql Normal file
View File

@ -0,0 +1,69 @@
-- phpMyAdmin SQL Dump
-- version 4.9.0.1
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Dec 14, 2020 at 12:51 PM
-- Server version: 10.4.6-MariaDB
-- PHP Version: 7.3.9
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `test_db`
--
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE `users` (
`id` int(11) NOT NULL,
`user_name` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`id`, `user_name`, `password`, `name`) VALUES
(3, 'asd', '202cb962ac59075b964b07152d234b70', 'Asd'),
(4, 'Asd', '202cb962ac59075b964b07152d234b70', 'asd');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

13
db_conn.php Normal file
View File

@ -0,0 +1,13 @@
<?php
$sname= "localhost";
$unmae= "root";
$password = "";
$db_name = "test_db";
$conn = mysqli_connect($sname, $unmae, $password, $db_name);
if (!$conn) {
echo "Connection failed!";
}
?>

24
home.php Normal file
View File

@ -0,0 +1,24 @@
<?php
session_start();
if (isset($_SESSION['id']) && isset($_SESSION['user_name'])) {
?>
<!DOCTYPE html>
<html lang="hu">
<head>
<meta charset="utf-8">
<title>HOME</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>Hello, <?php echo $_SESSION['name']; ?></h1>
<a href="logout.php">Logout</a>
</body>
</html>
<?php
}else{
header("Location: index.php");
exit();
}
?>

24
index.php Normal file
View File

@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="hu">
<head>
<meta charset="utf-8">
<title>Belépés</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<form action="login.php" method="post">
<h2>Bejelentkezés</h2>
<?php if (isset($_GET['error'])) { ?>
<p class="error"><?php echo $_GET['error']; ?></p>
<?php } ?>
<label>Felhasználónév</label>
<input type="text" name="uname" placeholder="Felhasználónév*"><br>
<label>Jelszó</label>
<input type="password" name="password" placeholder="Jelszó*"><br>
<button type="submit">Belépés</button>
<a href="signup.php" class="ca">Regisztrálj egy fiókot!</a>
</form>
</body>
</html>

54
login.php Normal file
View File

@ -0,0 +1,54 @@
<?php
session_start();
include "db_conn.php";
if (isset($_POST['uname']) && isset($_POST['password'])) {
function validate($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$uname = validate($_POST['uname']);
$pass = validate($_POST['password']);
if (empty($uname)) {
header("Location: index.php?error=Felhasználónév szükséges!");
exit();
}else if(empty($pass)){
header("Location: index.php?error=Jelszó szükséges!");
exit();
}else{
// hashing the password
$pass = md5($pass);
$sql = "SELECT * FROM users WHERE user_name='$uname' AND password='$pass'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) === 1) {
$row = mysqli_fetch_assoc($result);
if ($row['user_name'] === $uname && $row['password'] === $pass) {
$_SESSION['user_name'] = $row['user_name'];
$_SESSION['name'] = $row['name'];
$_SESSION['id'] = $row['id'];
header("Location: home.php");
exit();
}else{
header("Location: index.php?error=Hibás felhasználónév vagy jelszó!");
exit();
}
}else{
header("Location: index.php?error=Hibás felhasználónév vagy jelszó!");
exit();
}
}
}else{
header("Location: index.php");
exit();
}
?>

8
logout.php Normal file
View File

@ -0,0 +1,8 @@
<?php
session_start();
session_unset();
session_destroy();
header("Location: index.php");
?>

74
signup-check.php Normal file
View File

@ -0,0 +1,74 @@
<?php
session_start();
include "db_conn.php";
if (isset($_POST['uname']) && isset($_POST['password'])
&& isset($_POST['name']) && isset($_POST['re_password'])) {
function validate($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$uname = validate($_POST['uname']);
$pass = validate($_POST['password']);
$re_pass = validate($_POST['re_password']);
$name = validate($_POST['name']);
$user_data = 'uname='. $uname. '&name='. $name;
if (empty($uname)) {
header("Location: signup.php?error=Felhasználónév szükséges!&$user_data");
exit();
}else if(empty($pass)){
header("Location: signup.php?error=Jelszó szükséges!&$user_data");
exit();
}
else if(empty($re_pass)){
header("Location: signup.php?error=Jelszó ismételt megadása szükséges!&$user_data");
exit();
}
else if(empty($name)){
header("Location: signup.php?error=Név szükséges!&$user_data");
exit();
}
else if($pass !== $re_pass){
header("Location: signup.php?error=A két jelszó nem egyezik!&$user_data");
exit();
}
else{
// hashing the password
$pass = md5($pass);
$sql = "SELECT * FROM users WHERE user_name='$uname' ";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
header("Location: signup.php?error=A felhasználónév már foglalt, próbálj másikat!&$user_data");
exit();
}else {
$sql2 = "INSERT INTO users(user_name, password, name) VALUES('$uname', '$pass', '$name')";
$result2 = mysqli_query($conn, $sql2);
if ($result2) {
header("Location: signup.php?success=A fiókod sikeresen létrehozva!");
exit();
}else {
header("Location: signup.php?error=Ismeretlen hiba lépett fel!&$user_data");
exit();
}
}
}
}else{
header("Location: signup.php");
exit();
}
?>

58
signup.php Normal file
View File

@ -0,0 +1,58 @@
<!DOCTYPE html>
<html lang="hu">
<head>
<meta charset="utf-8">
<title>Regisztráció</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<form action="signup-check.php" method="post">
<h2>Regisztráció</h2>
<?php if (isset($_GET['error'])) { ?>
<p class="error"><?php echo $_GET['error']; ?></p>
<?php } ?>
<?php if (isset($_GET['success'])) { ?>
<p class="success"><?php echo $_GET['success']; ?></p>
<?php } ?>
<label>Név</label>
<?php if (isset($_GET['name'])) { ?>
<input type="text"
name="name"
placeholder="Teljes név*"
value="<?php echo $_GET['name']; ?>"><br>
<?php }else{ ?>
<input type="text"
name="name"
placeholder="Név"><br>
<?php }?>
<label>Felhasználónév</label>
<?php if (isset($_GET['uname'])) { ?>
<input type="text"
name="uname"
placeholder="Felhasználónév*"
value="<?php echo $_GET['uname']; ?>"><br>
<?php }else{ ?>
<input type="text"
name="uname"
placeholder="Felhasználónév*"><br>
<?php }?>
<label>Jelszó</label>
<input type="password"
name="password"
placeholder="Jelszó*"><br>
<label>Jelszó ismét</label>
<input type="password"
name="re_password"
placeholder="Jelszó ismét*"><br>
<button type="submit">Regisztráció</button>
<a href="index.php" class="ca">Van már fiókja?</a>
</form>
</body>
</html>

86
style.css Normal file
View File

@ -0,0 +1,86 @@
body {
background: #1690A7;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
flex-direction: column;
}
*{
font-family: sans-serif;
box-sizing: border-box;
}
form {
width: 500px;
border: 2px solid #ccc;
padding: 30px;
background: #fff;
border-radius: 15px;
}
h2 {
text-align: center;
margin-bottom: 40px;
}
input {
display: block;
border: 2px solid #ccc;
width: 95%;
padding: 10px;
margin: 10px auto;
border-radius: 5px;
}
label {
color: #888;
font-size: 18px;
padding: 10px;
}
button {
float: right;
background: #555;
padding: 10px 15px;
color: #fff;
border-radius: 5px;
margin-right: 10px;
border: none;
}
button:hover{
opacity: .7;
}
.error {
background: #F2DEDE;
color: #A94442;
padding: 10px;
width: 95%;
border-radius: 5px;
margin: 20px auto;
}
.success {
background: #D4EDDA;
color: #40754C;
padding: 10px;
width: 95%;
border-radius: 5px;
margin: 20px auto;
}
h1 {
text-align: center;
color: #fff;
}
.ca {
font-size: 14px;
display: inline-block;
padding: 10px;
text-decoration: none;
color: #444;
}
.ca:hover {
text-decoration: underline;
}