add m4 part 1

This commit is contained in:
vadym Novoselskyi 2024-09-09 14:19:57 +02:00
parent d442c2facc
commit 866c6b3731
21 changed files with 474 additions and 83 deletions

View File

@ -1 +1 @@
22
48

View File

@ -1 +1 @@
1
4

View File

@ -1 +1 @@
4
6

View File

@ -1 +1,2 @@
<hr><p>Från: wreqtrthsr</p><p>reywtyret</p><hr><p>Från: eargnkdsl</p><p>earsthy</p><hr><p>Från: aeawwtsrthd</p><p>agdhsf</p><hr><p>Från: argsdn</p><p>hsdtrj</p><hr><p>Från: ewttr</p><p>rerew</p><hr><p>Från: nklhio</p><p>hiohiohio</p><hr><p>Från: jo</p><p>jijpnin</p><hr><p>Från: </p><p>j</p><hr><p>Från: joqoapre</p><p>dvbilgfb</p>
<hr><p>Från: wreqtrthsr</p><p>reywtyret</p><hr><p>Från: eargnkdsl</p><p>earsthy</p><hr><p>Från: aeawwtsrthd</p><p>agdhsf</p><hr><p>Från: argsdn</p><p>hsdtrj</p><hr><p>Från: ewttr</p><p>rerew</p><hr><p>Från: nklhio</p><p>hiohiohio</p><hr><p>Från: jo</p><p>jijpnin</p><hr><p>Från: </p><p>j</p><hr><p>Från: joqoapre</p><p>dvbilgfb</p><hr><p>Från: 1234</p><p>testtest
</p><hr><p>Från: <br>Vadym </br></p><p>dfsgdhsg</p>

View File

@ -1 +1 @@
4
8

1
www/M3-06-messages.dat Normal file
View File

@ -0,0 +1 @@
<hr><p>Från: 555</p><p>afsdgfh</p>

Binary file not shown.

BIN
www/M4-01-users.dat Normal file

Binary file not shown.

View File

@ -2,18 +2,14 @@
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
else {
$_SESSION = array(); // Tömmer sessionsarrayen
session_destroy();
}
$hit = 0;
if(file_exists("../../../M3-02-hit.dat")) {
$hit = file_get_contents("../../../M3-02-hit.dat");
}
$hit++; // Ökar antalet besökare med 1
file_put_contents("../../../M3-02-hit.dat", $hit);
echo $hit;
$hit = 0;
if(file_exists("../../../M3-02-hit.dat")) {
$hit = file_get_contents("../../../M3-02-hit.dat");
}
$hit++; // Ökar antalet besökare med 1
file_put_contents("../../../M3-02-hit.dat", $hit);
echo $hit;
?>
<!doctype html>
<html lang="sv">

View File

@ -1,46 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>M3 06</title>
</head>
<body>
<?php
include("userManipulation.php");
if (isset($_POST["username"])) $username = $_POST["username"];
if (isset($_POST["password"])) $password = $_POST["password"];
$username = cleanData($username);
$user = new User($username, $password);
if (isset($_POST["login"])) {
if (isPresent($user)) {
echo "User: " . $user->getUsername();
echo "<br>Pass: " . $user->getPassword();
} else {
header("Location: index.php");
exit();
}
}
else if(isset($_POST["signup"])) {
addUser($user);
header("Location: index.php");
exit();
}
function cleanData($data)
{
$data = strip_tags($data);
$data = htmlspecialchars($data);
$data = trim($data);
$data = stripslashes($data);
return $data;
}
?>
</body>
</html>

View File

@ -27,7 +27,12 @@ function login()
header("Location: login.php");
exit();
}
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
$_SESSION['logged_in'] = true;
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
}
function signup() {
if (isset($_POST["username"]) && $_POST["username"] != "") $username = $_POST["username"];
@ -40,11 +45,14 @@ function signup() {
header("Location: login.php");
exit();
}
$username = cleanData($username);
$user = new User($username, $password);
addUser($user);
incUserCount();
$_SESSION['logged_in'] = true;
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
}
function logout()

View File

@ -1,16 +1,11 @@
<?php
if (!isset($_SESSION['logged_in'])) {
echo "<h1>No login </h1>";
}
else {
?>
<h1>Klotterplanket</h1>
<form action="saveMsg.php" method="post">
<label>Namn</label><br>
<input type="text" name="name"><br />
<?php
echo '<label>Namn</label><br>';
echo '<input type="text" value="' . $_SESSION['username'] .'" disabled name="name"> <br/>';
?>
<label>Meddelande</label><br>
<textarea name="message" cols="45" rows="5"></textarea><br />
<textarea name="message" cols="45" rows="5"></textarea> <br/>
<input type="submit" value="Skicka">
</form>
@ -18,5 +13,4 @@ else {
if(file_exists("../../../M3-06-messages.dat")) {
echo file_get_contents("../../../M3-06-messages.dat");
}
}
?>
?>

View File

@ -1,8 +1,19 @@
<?php
$name = "<hr><p>Från: " . $_POST['name'] . "</p>";
$msg = "<p>" . $_POST['message'] . "</p>";
file_put_contents("../../../M3-06-messages.dat",$name.$msg,FILE_APPEND);
header("location: index.php?page=klotter"); //Omdirigerar till klotterplanket
?>
<?php
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
$name = "<hr><p>Från: " . $_SESSION['username'] . "</p>";
$msg = "<p>" . cleanData($_POST['message']) . "</p>";
file_put_contents("../../../M3-06-messages.dat", $name . $msg, FILE_APPEND);
header("location: index.php?page=klotter"); //Omdirigerar till klotterplanket
function cleanData($data)
{
$data = strip_tags($data);
$data = htmlspecialchars($data);
$data = trim($data);
$data = stripslashes($data);
return $data;
}

28
www/public/M4/01/User.php Normal file
View File

@ -0,0 +1,28 @@
<?php
class User
{
private $username;
private $password;
public function __construct($username, $password)
{
$this->username = $username;
$this->password = $password;
}
public function getUsername() {
return $this->username;
}
public function getPassword() {
return $this->password;
}
public function setUsername($username) {
$this->username = $username;
}
public function setPassword($password) {
$this->password = $password;
}
}

View File

@ -0,0 +1,94 @@
<?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");
}
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();
}
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
$_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();
}
$username = cleanData($username);
$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
session_destroy();
#header("Location: login.php");
}
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>M4 | 01</title>
<link href="css/styleSheet.css" rel="stylesheet" type="text/css">
</head>
<body>
<main>
<?php
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true) {
echo "<h1>Logged in!</h1>";
}
else {
echo "<h1>Not logged in!</h1>";
}
?>
<a href="login.php">Login</a>
</main>
</body>
</html>

View File

@ -0,0 +1,44 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
</head>
<body>
<form action="index.php" method="post">
<fieldset>
<legend>Login</legend>
<input type="hidden" name="login">
<label>Your username: </label>
<input type="text" name="username"> <br> <br>
<label>Your password: </label>
<input type="password" name="password"> <br> <br>
<input type="submit" value="Submit">
</fieldset>
</form>
<form action="index.php" method="post">
<fieldset>
<legend>Signup</legend>
<input type="hidden" name="signup">
<label>Your username: </label>
<input type="text" name="username"> <br> <br>
<label>Your password: </label>
<input type="password" name="password"> <br> <br>
<input type="submit" value="Submit">
</fieldset>
</form>
<br>
<br>
<form action="index.php" method="post">
<fieldset>
<legend>Logout</legend>
<input type="hidden" name="logout">
<input type="submit" value="Logout">
</fieldset>
</form>
</body>
</html>

View File

@ -0,0 +1,47 @@
<!DOCTYPE html>
<html lang="se">
<head>
<meta charset="UTF-8">
<title>Person write</title>
</head>
<body>
<?php
include("User.php");
function addUser($user)
{
$file = "../../../M4-01-users.dat";
if(file_exists($file)) {
$users = unserialize(file_get_contents($file));
$users[] = $user;
file_put_contents($file, serialize($users));
}
else {
$users = array();
$users[] = $user;
file_put_contents($file, serialize($users));
}
}
function isPresent($userToFind)
{
$file = "../../../M4-01-users.dat";
if (file_exists($file)) {
$userArray = unserialize(file_get_contents($file));
}
else return false;
foreach($userArray as $user) {
if($userToFind->getUsername() == $user->getUsername() && password_verify($userToFind->getPassword(), $user->getPassword())) return true;
}
return false;
}
?>
</body>
</html>

28
www/public/M4/02/User.php Normal file
View File

@ -0,0 +1,28 @@
<?php
class User
{
private $username;
private $password;
public function __construct($username, $password)
{
$this->username = $username;
$this->password = $password;
}
public function getUsername() {
return $this->username;
}
public function getPassword() {
return $this->password;
}
public function setUsername($username) {
$this->username = $username;
}
public function setPassword($password) {
$this->password = $password;
}
}

View File

@ -0,0 +1,94 @@
<?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");
}
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();
}
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
$_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();
}
$username = cleanData($username);
$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
session_destroy();
#header("Location: login.php");
}
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>M4 | 01</title>
<link href="css/styleSheet.css" rel="stylesheet" type="text/css">
</head>
<body>
<main>
<?php
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true) {
echo "<h1>Logged in!</h1>";
}
else {
echo "<h1>Not logged in!</h1>";
}
?>
<a href="login.php">Login</a>
</main>
</body>
</html>

View File

@ -0,0 +1,44 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
</head>
<body>
<form action="index.php" method="post">
<fieldset>
<legend>Login</legend>
<input type="hidden" name="login">
<label>Your username: </label>
<input type="text" name="username"> <br> <br>
<label>Your password: </label>
<input type="password" name="password"> <br> <br>
<input type="submit" value="Submit">
</fieldset>
</form>
<form action="index.php" method="post">
<fieldset>
<legend>Signup</legend>
<input type="hidden" name="signup">
<label>Your username: </label>
<input type="text" name="username"> <br> <br>
<label>Your password: </label>
<input type="password" name="password"> <br> <br>
<input type="submit" value="Submit">
</fieldset>
</form>
<br>
<br>
<form action="index.php" method="post">
<fieldset>
<legend>Logout</legend>
<input type="hidden" name="logout">
<input type="submit" value="Logout">
</fieldset>
</form>
</body>
</html>

View File

@ -0,0 +1,47 @@
<!DOCTYPE html>
<html lang="se">
<head>
<meta charset="UTF-8">
<title>Person write</title>
</head>
<body>
<?php
include("User.php");
function addUser($user)
{
$file = "../../../M4-02-users.dat";
if(file_exists($file)) {
$users = unserialize(file_get_contents($file));
$users[] = $user;
file_put_contents($file, serialize($users));
}
else {
$users = array();
$users[] = $user;
file_put_contents($file, serialize($users));
}
}
function isPresent($userToFind)
{
$file = "../../../M4-02-users.dat";
if (file_exists($file)) {
$userArray = unserialize(file_get_contents($file));
}
else return false;
foreach($userArray as $user) {
if($userToFind->getUsername() == $user->getUsername() && password_verify($userToFind->getPassword(), $user->getPassword())) return true;
}
return false;
}
?>
</body>
</html>