Session implementiert

This commit is contained in:
wiecktobi 2018-06-13 19:46:32 +02:00
parent 3c53460d20
commit 7e84260cb7
4 changed files with 56 additions and 42 deletions

View File

@ -1,3 +1,4 @@
<?php session_start(); ?>
<!doctype html>
<html lang="en">
<head>

View File

@ -25,6 +25,9 @@ document.getElementById('btnCreate').addEventListener("click" , async (event) =>
method: 'post',
body: formData
});
const jsonData = await response.json();
console.log(jsonData);
} catch (e) {
console.log("Abschicken der Daten fehlerhaft!");
}
@ -36,7 +39,7 @@ document.getElementById('btnLogin').addEventListener("click" , async (event) =>
// if (document.getElementById('btnLogin') === "Login") {
const formData = new FormData(document.getElementById('login-form'));
try {
// try {
const response = await fetch('php/signup.php', {
method: 'post',
body: formData
@ -47,23 +50,26 @@ document.getElementById('btnLogin').addEventListener("click" , async (event) =>
document.getElementById('login-status').innerHTML = jsonData.error.fontcolor("red");
} else {
console.log(jsonData);
if (jsonData == 'true') {
document.getElementById('toggle-login').innerHTML = "Logout";
document.getElementById('btnLogin').innerHTML = "Logout";
document.getElementById('exampleModalLabel').innerHTML = "Logout";
document.getElementById('login-status').innerHTML = "Erfolgreich eingeloggt";
}
else {
document.getElementById('toggle-login').innerHTML = "Login";
document.getElementById('btnLogin').innerHTML = "Login";
document.getElementById('exampleModalLabel').innerHTML = "Login";
document.getElementById('login-status').innerHTML = "Erfolgreich ausgeloggt";
}
document.getElementById('login-status').innerHTML = "Eingeloggt";
location.replace('index2.htm');
// if (jsonData == 'true') {
// document.getElementById('toggle-login').innerHTML = "Logout";
// document.getElementById('btnLogin').innerHTML = "Logout";
// document.getElementById('exampleModalLabel').innerHTML = "Logout";
// document.getElementById('login-status').innerHTML = "Erfolgreich eingeloggt";
// }
// else {
// document.getElementById('toggle-login').innerHTML = "Login";
// document.getElementById('btnLogin').innerHTML = "Login";
// document.getElementById('exampleModalLabel').innerHTML = "Login";
// document.getElementById('login-status').innerHTML = "Erfolgreich ausgeloggt";
// }
// location.reload();
}
} catch (e) {
console.log("Login Fehler");
}
// }
// catch (e) {
// console.log("Login Fehler");
// }
// }
})

View File

@ -6,11 +6,21 @@
$pswd = ($_POST['registerPassword']);
$email = ($_POST['registerMail']);
// if ($abfrage == "") {
$search_user = $database->prepare("SELECT ID FROM user WHERE user='$name '");
$search_user->execute();
$search_result = $search_user->get_result();
if ($search_result->num_rows == 0) {
$pswd = md5($pswd);
$stmt = $database->prepare("INSERT INTO user(user, email, password) VALUES (?,?,?)");
$stmt->bind_param("sss", $name, $email, $pswd);
$stmt->execute();
$stmt->close();
// }
echo json_encode("Dein Account wurde erfolgreich erstellt!");
} else {
echo json_encode("Der Benutzername ist leider schon vergeben!");
}
?>

View File

@ -1,36 +1,33 @@
<?php
//<!--Login-->
session_start();
require('../dbConnect.php');
$name = ($_POST['loginName']);
$pswd = ($_POST['loginPassword']);
// $pswd = md5($pswd);
try {
$stmt = $database->prepare("SELECT password FROM user WHERE '$name'=user");
$stmt = $database->prepare("SELECT id FROM user WHERE '$name'=user AND password='$pswd'");
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
} catch (PDOException $e) {
$e->getMessage();
}
if ($pswd == $row['password']) {
$stmt = $database->prepare("SELECT loggedin FROM user WHERE '$name'=user");
$stmt->execute();
$result = $stmt->get_result()->fetch_assoc();
if ($result['loggedin'] == 'false') {
$stmt = $database->prepare("UPDATE `benutzer`.`user` SET `loggedin`='true' WHERE '$name'=user");
$stmt->execute();
$a = "true";
if ($result->num_rows == 1) {
$object = $result->fetch_object();
$_SESSION['user'] = $object->id;
// header('Location: /index2.htm');
echo json_encode('Eingeloggt!');
// $stmt = $database->prepare("SELECT loggedin FROM user WHERE '$name'=user");
// $stmt->execute();
// $result = $stmt->get_result()->fetch_assoc();
// if ($result['loggedin'] == 'false') {
// $stmt = $database->prepare("UPDATE `benutzer`.`user` SET `loggedin`='true' WHERE '$name'=user");
// $stmt->execute();
// $a = "true";
} else {
$stmt = $database->prepare("UPDATE `benutzer`.`user` SET `loggedin`='false' WHERE '$name'=user");
$stmt->execute();
$a = "false";
$a = array('error' => 'Einlogen fehlgeschlagen!');
echo json_encode($a);
}
echo json_encode($a);
} else {
$a = array('error' => 'Einlogen fehlgeschlagen!' . $pswd . " " . $row['password']);
echo json_encode($a);
}
?>