WebServ/www/public/M3/06/index.php

161 lines
3.5 KiB
PHP
Raw Permalink Normal View History

2024-09-06 13:22:36 +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();
}
#header("Location: login.php");
}
2024-09-13 12:56:16 +00:00
if (file_exists("../../../userCount/M3-06-hit.dat")) {
$hit = file_get_contents("../../../userCount/M3-06-hit.dat");
2024-09-06 13:22:36 +00:00
echo $hit;
}
function login()
{
if (isset($_POST["username"])) $username = $_POST["username"];
if (isset($_POST["password"])) $password = $_POST["password"];
$username = cleanData($username);
$user = new User($username, $password);
if (!isPresent($user)) {
header("Location: login.php");
exit();
}
2024-09-09 12:19:57 +00:00
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
2024-09-06 13:22:36 +00:00
$_SESSION['logged_in'] = true;
2024-09-09 12:19:57 +00:00
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
2024-09-06 13:22:36 +00:00
}
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-09 12:19:57 +00:00
2024-09-06 13:22:36 +00:00
$username = cleanData($username);
$user = new User($username, $password);
addUser($user);
incUserCount();
$_SESSION['logged_in'] = true;
2024-09-09 12:19:57 +00:00
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
2024-09-06 13:22:36 +00:00
}
function logout()
{
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
$_POST = array();
$_SESSION = array(); // Tömmer sessionsarrayen
session_destroy();
#header("Location: login.php");
}
function incUserCount() {
if (session_status() == PHP_SESSION_NONE) {
session_start();
$hit = 0;
2024-09-13 12:56:16 +00:00
if (file_exists("../../../userCount/M3-06-hit.dat")) {
$hit = file_get_contents("../../../userCount/M3-06-hit.dat");
2024-09-06 13:22:36 +00:00
}
$hit++; // Ökar antalet besökare med 1
2024-09-13 12:56:16 +00:00
file_put_contents("../../../userCount/M3-06-hit.dat", $hit);
2024-09-06 13:22:36 +00:00
}
}
function cleanData($data)
{
$data = strip_tags($data);
$data = htmlspecialchars($data);
$data = trim($data);
$data = stripslashes($data);
return $data;
}
?>
<!doctype html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<title>Länka in med PHP</title>
<link href="css/styleSheet.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="wrapper">
<header>
<?php include("inc/header.php"); ?>
</header>
<!-- header -->
<section id="leftColumn">
<nav>
<?php include("inc/meny.php"); ?>
</nav>
<aside>
<?php include("inc/aside.php"); ?>
</aside>
</section>
<!-- End leftColumn -->
<?php
?>
<main>
<section>
<!-- Lägg in innehållet här -->
<?php
$page = "start";
if (isset($_GET['page']))
$page = $_GET['page'];
switch ($page) {
case 'blogg':
include('pages/blogg.php');
break;
case 'bilder':
include('pages/bilder.php');
break;
case 'kontakt':
include('pages/kontakt.php');
break;
case 'klotter':
include('pages/klotter.php');
break;
default:
include('pages/start.php');
}
?>
</section>
</main>
<!-- End main -->
<footer>
<?php include('inc/footer.php'); ?>
</footer>
<!-- End footer -->
</div>
<!-- End wrapper -->
</body>
</html>