WebServ/www/public/M4/02/index.php

107 lines
2.6 KiB
PHP
Raw Permalink Normal View History

2024-09-09 12:19:57 +00:00
<?php
include("userManipulation.php");
if (isset($_POST['login'])) login();
else if (isset($_POST['signup'])) signup();
else if (isset($_POST['logout'])) logout();
else {
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
}
function login()
{
if (isset($_POST["username"])) $username = $_POST["username"];
if (isset($_POST["password"])) $password = $_POST["password"];
2024-09-13 12:56:16 +00:00
$username = filter_input(INPUT_POST, $username, FILTER_SANITIZE_SPECIAL_CHARS | FILTER_SANITIZE_STRIPPED);
2024-09-09 12:19:57 +00:00
$user = new User($username, $password);
if (!isPresent($user)) {
header("Location: login.php");
exit();
}
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
2024-09-13 12:56:16 +00:00
session_regenerate_id(true);
2024-09-09 12:19:57 +00:00
$_SESSION['logged_in'] = true;
}
function signup() {
if (isset($_POST["username"]) && $_POST["username"] != "") $username = $_POST["username"];
else {
header("Location: login.php");
exit();
}
if (isset($_POST["password"]) && $_POST["password"] != "") $password = $_POST["password"];
else {
header("Location: login.php");
exit();
}
2024-09-13 12:56:16 +00:00
$username = filter_input(INPUT_POST, $username, FILTER_SANITIZE_SPECIAL_CHARS | FILTER_SANITIZE_STRIPPED);
2024-09-09 12:19:57 +00:00
$password = password_hash($password, PASSWORD_DEFAULT);
$user = new User($username, $password);
addUser($user);
$_SESSION['logged_in'] = true;
}
function logout()
{
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
$_POST = array();
$_SESSION = array(); // Tömmer sessionsarrayen
2024-09-13 12:56:16 +00:00
session_regenerate_id(true);
2024-09-09 12:19:57 +00:00
}
?>
<!doctype html>
<html lang="sv">
<head>
<meta charset="UTF-8">
2024-09-13 12:56:16 +00:00
<title>M4 | 02</title>
2024-09-09 12:19:57 +00:00
<link href="css/styleSheet.css" rel="stylesheet" type="text/css">
</head>
<body>
<main>
2024-09-13 12:56:16 +00:00
<form action="" method="post">
<fieldset>
<legend>Form</legend>
<input type="hidden" name="message">
<label>Your name: </label>
<input type="text" name="name"> <br> <br>
<label>Your message: </label>
<input type="text" name="text"> <br> <br>
<input type="submit" value="Submit">
</fieldset>
</form>
2024-09-09 12:19:57 +00:00
<?php
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true) {
echo "<h1>Logged in!</h1>";
}
else {
echo "<h1>Not logged in!</h1>";
}
2024-09-13 12:56:16 +00:00
if(isset($_POST['message'])) {
$safeName = filter_input(INPUT_POST, 'name',FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$safeMassage = filter_input(INPUT_POST, 'text',FILTER_SANITIZE_FULL_SPECIAL_CHARS);
echo "<h3>Name - " . $safeName . "</h3>";
echo "<p>Massage - " . $safeMassage . "</p>";
}
else {
echo "<h3>No message!</h3>";
}
2024-09-09 12:19:57 +00:00
?>
<a href="login.php">Login</a>
</main>
</body>
</html>