Daten in Datenbank schreiben,

Daten aus Datenbank auslesen,
Anfang SessionManagement
This commit is contained in:
Johannes Theiner 2021-04-27 12:11:43 +02:00
parent cfc154c407
commit e727f0e97e
8 changed files with 171 additions and 31 deletions

View File

@ -1,5 +1,7 @@
{
"require": {
"phpmailer/phpmailer": "^6.4"
"phpmailer/phpmailer": "^6.4",
"rakit/validation": "v1.4.0",
"ext-pdo": "*"
}
}

48
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "d01b4a542231b112db557e6b7e5a5121",
"content-hash": "59230ec995edc426fe883cde9a32b541",
"packages": [
{
"name": "phpmailer/phpmailer",
@ -81,6 +81,52 @@
}
],
"time": "2021-03-31T20:06:42+00:00"
},
{
"name": "rakit/validation",
"version": "v1.4.0",
"source": {
"type": "git",
"url": "https://github.com/rakit/validation.git",
"reference": "ff003a35cdf5030a5f2482299f4c93f344a35b29"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/rakit/validation/zipball/ff003a35cdf5030a5f2482299f4c93f344a35b29",
"reference": "ff003a35cdf5030a5f2482299f4c93f344a35b29",
"shasum": ""
},
"require": {
"ext-mbstring": "*",
"php": ">=7.0"
},
"require-dev": {
"php-coveralls/php-coveralls": "^2.2",
"phpunit/phpunit": "^6.5",
"squizlabs/php_codesniffer": "^3"
},
"type": "library",
"autoload": {
"psr-4": {
"Rakit\\Validation\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Muhammad Syifa",
"email": "emsifa@gmail.com"
}
],
"description": "PHP Laravel like standalone validation library",
"support": {
"issues": "https://github.com/rakit/validation/issues",
"source": "https://github.com/rakit/validation/tree/v1.4.0"
},
"time": "2020-08-27T05:07:01+00:00"
}
],
"packages-dev": [],

View File

@ -1,5 +1,9 @@
;<?php die(); ?>
url = http://localhost
user = sample
password = password
[database]
db_host = localhost
db_database = alumni
@ -12,6 +16,7 @@ mail_port = 25
mail_user =
mail_address =
mail_password =
mail_name = Alumni Max Emden
[style]
title = Alumni Max Emden

View File

@ -2,4 +2,6 @@ create table entries(id bigint auto_increment primary key, name varchar(255), ma
year int(4), birthday date, verify bool, vocation varchar(255)
);
create table verify(id bigint primary key, uuid BINARY(16) ,foreign key verify(id) REFERENCES entries(id));
create table verify(id bigint primary key, uuid VARCHAR(36),foreign key verify(id) REFERENCES entries(id));
CREATE TRIGGER before_insert_verify BEFORE INSERT ON verify FOR EACH ROW SET new.uuid = uuid();

View File

@ -1,15 +1,21 @@
<?php
$db = getDatabase();
function getConfig() {
return parse_ini_file('config.ini.php');
}
function getDatabase() {
function getDatabase(): PDO {
$config = getConfig();
if($db = mysqli_connect($config['db_host'], $config['db_user'], $config['db_password'], $config['db_database'])){
return $db;
try {
$host = $config['db_host'];
$database = $config['db_database'];
return new PDO("mysql:host=$host;dbname=$database", $config['db_user'], $config['db_password']);
}
catch(PDOException $e) {
echo $e->getMessage();
}
return null;
}
function returnHeader() {

17
login.php Normal file
View File

@ -0,0 +1,17 @@
<?php
require_once 'general.php';
returnHeader();
$config = getConfig();
if (isset($_POST['login']) && !empty($_POST['username'])
&& !empty($_POST['password'])) {
if ($_POST['user'] == $config['user'] && $_POST['password'] == $config['password']) {
$_SESSION['timeout'] = time();
}
}
returnFooter();
?>

View File

@ -1,19 +1,22 @@
<?php
require_once 'general.php';
session_start();
if(!isset($_SESSION['user'])) {
echo "Nicht angemeldet";
http_response_code(401);
exit;
}
returnHeader();
$file = file_get_contents("MOCK_DATA.json");
if ($file === false) {
die();
$statement = getDatabase()->prepare("SELECT * from entries WHERE verify = 1");
if(!$statement->execute()) {
echo "database Error";
}
$entries = json_decode($file, true);
if ($entries === null) {
die();
}
$config = getConfig();
$entries = $statement->fetchAll(PDO::FETCH_ASSOC);
?>
@ -43,13 +46,14 @@ $config = getConfig();
foreach ($entries as $entry) {
$age = date_diff(date_create($entry['birthday']), date_create('now'))->y;
?>
<tr>
<td><?php echo $entry["name"] ?></td>
<td><?php echo $entry["vocation"] ?></td>
<td><?php echo $entry["email"] ?></td>
<td><?php echo $entry["mail"] ?></td>
<td><?php echo $entry["year"] ?></td>
<td><?php echo $entry["age"] ?></td>
<td><?php echo $age ?></td>
</tr>
<?php
}

View File

@ -1,27 +1,76 @@
<?php
/**
if ($_SERVER['REQUEST_METHOD'] != "POST") {
die();
}
* */
require_once 'general.php';
require_once 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use Rakit\Validation\Validator;
$name = $_POST['name'];
$mail = $_POST['mail'];
$year = $_POST['year'];
$birthday = $_POST['birthday'];
$vocation = $_POST['vocation'];
$privacy = $_POST['privacy'];
$validator = new Validator;
//validate here
$validation = $validator->make($_POST,[
'name' => 'required',
'email' => 'required|email',
'year' => 'required|numeric',
'birthday' => 'required|date',
'vocation' => 'required',
'privacy' => 'required'
]);
$validation->setMessages([
'required' => ":attribute muss ausgefüllt werden",
'email' => "Die E-Mail Adresse :email ist nicht gültig",
'numeric' => ":numeric muss eine Zahl sein",
'date' => ":attribute muss ein Datum sein"
]);
$validation->validate();
if($validation->fails()) {
$errors = $validation->errors();
echo "<pre>";
print_r($errors->firstOfAll());
echo "</pre>";
exit;
}
$validData = $validation->getValidData();
$config = getConfig();
$validData['verify'] = 0;
unset($validData['privacy']);
$db = getDatabase();
$statement = $db->prepare("INSERT INTO entries(name, mail, year, birthday, verify, vocation) VALUES (:name, :email, :year, :birthday, :verify, :vocation)");
if(!$statement->execute($validData)) {
echo "Datenbank Fehler";
exit;
}
$id = $db->lastInsertId();
$statement = $db->prepare("INSERT INTO verify(id) VALUES (:id)");
if(!$statement->execute(['id' => $id])) {
echo "Datenbank Fehler";
exit;
}
$statement = $db->prepare("SELECT uuid from verify WHERE id = :id");
if(!$statement->execute(['id' => $id])) {
echo "database Error";
}
$uuid = $statement->fetch(PDO::FETCH_ASSOC)['uuid'];
$mail = new PHPMailer(true);
$mail->isSMTP();
@ -32,15 +81,16 @@ $mail->Username = $config['mail_user'];
$mail->Password = $config['mail_password'];
$mail->From = $config['mail_address'];
$mail->FromName = 'Mailer';
$mail->FromName = $config['mail_name'];
try {
$mail->addAddress('test@joethei.de', 'Joe User');
$mail->addAddress($validData['email'], $validData['name']);
} catch (\PHPMailer\PHPMailer\Exception $e) {
echo $e->getMessage();
}
$mail->Subject = 'Here is the subject';
$mail->AltBody = 'Message without html';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->Subject = 'Bestätigung einer Eintragung';
$mail->AltBody = 'Bitte bestätige deine Eintragung unter folgendem Link: ' . $config['url'] . "/verify.php/?id=" . $uuid;
$mail->Body = 'Bitte bestätige deine Eintragung bitte <a href="' . $config['url'] . "/verify.php/?id=" . $uuid . '">hier</a>';
try {
@ -48,3 +98,11 @@ try {
} catch (\PHPMailer\PHPMailer\Exception $e) {
echo $e;
}
returnHeader();
?>
Vielen Dank, bitte bestätige deine E-Mail Adresse
<?php
returnFooter();