add 1/2 M6

This commit is contained in:
vadym Novoselskyi 2024-09-19 13:47:33 +02:00
parent f2565ab422
commit 9031c8596b
8 changed files with 291 additions and 0 deletions

View File

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>M6 | 01</title>
</head>
<body>
<?php
include_once('../inc/egytalk_connect.php');
/* Kör frågan mot databasen world och tabellen country */
$stmt = $db->prepare("SELECT Name, Population FROM country WHERE Name LIKE 'Z%' ORDER BY Population DESC");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach( $result as $row ){
echo "<strong>Country: </strong>".$row['Name'];
echo " <strong>Population: </strong>".$row['Population'];
echo "<br /><hr />";
}
?>
</body>
</html>

View File

@ -0,0 +1,35 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>M6 | 01</title>
</head>
<body>
<form method="post">
<label>City: </label>
<input type="text" name="city">
<input type="submit" value="Submit"> <br><br>
</form>
<?php
include_once('../inc/egytalk_connect.php');
if(isset($_POST['city']) && $_POST['city'] != '') $city = filter_input(INPUT_POST, 'city', FILTER_SANITIZE_SPECIAL_CHARS);
else $city = 'Malmö';
/* Kör frågan mot databasen world och tabellen country */
$stmt = $db->prepare("SELECT Name, Population FROM city WHERE Name LIKE :city ORDER BY Name");
$stmt->bindValue(":city", "$city%", PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach( $result as $row ){
echo "<strong>City: </strong>".$row['Name'];
echo " <strong>Population: </strong>".$row['Population'];
echo "<br /><hr />";
}
?>
</body>
</html>

View File

@ -0,0 +1,48 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>M6 | 01</title>
</head>
<body>
<form method="post">
<label>Name: </label>
<input type="text" name="firstName"> <br><br>
<label>Surname: </label>
<input type="text" name="surName"> <br><br>
<label>Username: </label>
<input type="text" name="username"> <br><br>
<label>Password: </label>
<input type="password" name="password"> <br><br>
<input type="submit" value="Submit"> <br><br>
</form>
<?php
if(isset($_POST['firstName'],$_POST['surName'],$_POST['username'],$_POST['password'])){
include_once('../inc/egytalk_connect.php');
$firstName = filter_input(INPUT_POST, 'firstName', FILTER_SANITIZE_SPECIAL_CHARS);
$surName = filter_input(INPUT_POST, 'surName', FILTER_SANITIZE_SPECIAL_CHARS);
$username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_SPECIAL_CHARS);
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$stmt= $db->prepare("INSERT INTO user(uid, firstname, surname, username, password) VALUES(UUID(), :fn, :sn,:user,:pwd)");
$stmt->bindValue(":fn", $firstName);
$stmt->bindValue(":sn", $surName);
$stmt->bindValue(":user", $username);
$stmt->bindValue(":pwd", $password);
try{
$stmt->execute();
echo "Good";
}catch(Exception $e){
echo "Not good";
}
}
?>
</body>
</html>

View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>M6 | 04</title>
</head>
<body>
<?php
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true) echo "<h1>Good</h1>";
else echo "<h1> No good </h1>";
?>
<a href="login.php">Log In</a>
</body>
</html>

View File

@ -0,0 +1,62 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>M6 | 01</title>
</head>
<body>
<form method="post" action="userManipulation.php">
<input type="hidden" name="login">
<label>Username: </label>
<input type="text" name="username"> <br><br>
<label>Password: </label>
<input type="password" name="password"> <br><br>
<input type="submit" value="Log In"> <br><br><br><br>
</form>
<form method="post" action="userManipulation.php">
<input type="hidden" name="signup">
<label>Name: </label>
<input type="text" name="firstName"> <br><br>
<label>Surname: </label>
<input type="text" name="surName"> <br><br>
<label>Username: </label>
<input type="text" name="username"> <br><br>
<label>Password: </label>
<input type="password" name="password"> <br><br>
<input type="submit" value="Sign Up"> <br><br><br><br>
</form>
<form method="post" action="userManipulation.php">
<input type="hidden" name="logout">
<input type="submit" value="Log Out"> <br><br>
</form>
<?php
if(isset($_POST['firstName'],$_POST['surName'],$_POST['username'],$_POST['password'])){
include_once('../inc/egytalk_connect.php');
$firstName = filter_input(INPUT_POST, 'firstName', FILTER_SANITIZE_SPECIAL_CHARS);
$surName = filter_input(INPUT_POST, 'surName', FILTER_SANITIZE_SPECIAL_CHARS);
$username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_SPECIAL_CHARS);
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$stmt= $db->prepare("INSERT INTO user(uid, firstname, surname, username, password) VALUES(UUID(), :fn, :sn,:user,:pwd)");
$stmt->bindValue(":fn", $firstName);
$stmt->bindValue(":sn", $surName);
$stmt->bindValue(":user", $username);
$stmt->bindValue(":pwd", $password);
try{
$stmt->execute();
echo "Good";
}catch(Exception $e){
echo "Not good";
}
}
?>
</body>
</html>

View File

@ -0,0 +1,81 @@
<?php
if (isset($_POST['login'])) login();
else if (isset($_POST['signup'])) signup();
else if (isset($_POST['logout'])) logout();
else header("Location: login.php");
function login()
{
if (!isset($_POST['username'], $_POST['password'])) header("Location: login.php");
include_once('../inc/egytalk_connect.php');
$username = filter_input(INPUT_POST, 'username', FILTER_UNSAFE_RAW);
$password = $_POST['password'];
$stmt = $db->prepare("SELECT * FROM user WHERE username = :username");
$stmt->bindValue(":username", $username);
$stmt->execute();
if ($stmt->rowCount() != 1) header("Location: login.php");
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if (password_verify($password, $user['password'])) {
$_SESSION = array();
session_start();
$_SESSION['uid'] = $user['uid'];
$_SESSION['username'] = $user['username'];
$_SESSION['name'] = $user['surname'] . " " . $user['firstname'];
$_SESSION['logged_in'] = true;
header("Location: index.php");
}
}
function signup()
{
if (!isset($_POST['firstName'], $_POST['surName'], $_POST['username'], $_POST['password'])) {
header("Location: login.php");
exit();
}
include_once('../inc/egytalk_connect.php');
$firstName = filter_input(INPUT_POST, 'firstName', FILTER_SANITIZE_SPECIAL_CHARS);
$surName = filter_input(INPUT_POST, 'surName', FILTER_SANITIZE_SPECIAL_CHARS);
$username = filter_input(INPUT_POST, 'username', FILTER_UNSAFE_RAW);
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$stmt = $db->prepare("INSERT INTO user(uid, firstname, surname, username, password) VALUES(UUID(), :fn, :sn,:user,:pwd)");
$stmt->bindValue(":fn", $firstName);
$stmt->bindValue(":sn", $surName);
$stmt->bindValue(":user", $username);
$stmt->bindValue(":pwd", $password);
try {
$stmt->execute();
$_SESSION = array();
session_start();
$_SESSION['logged_in'] = true;
header("Location: index.php");
} catch (Exception $e) {
header("Location: login.php");
exit();
}
}
function logout()
{
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
$_POST = array();
$_SESSION = array(); // Tömmer sessionsarrayen
session_regenerate_id(true);
header("Location: index.php");
exit();
}

View File

@ -0,0 +1,11 @@
<?php
// Definierar konstanter med användarinformation.
define ('DB_USER', 'egytalk');
define ('DB_PASSWORD', '12345');
define ('DB_HOST', 'mariadb'); // 'Om docker - 'mariadb', annars 127.0.0.1
define ('DB_NAME', 'egytalk');
// Skapar en anslutning till MySql och databasen world
$dsn = 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=utf8';
$db = new PDO($dsn, DB_USER, DB_PASSWORD);
?>

View File

@ -0,0 +1,11 @@
<?php
// Definierar konstanter med användarinformation.
define ('DB_USER', 'world');
define ('DB_PASSWORD', '12345');
define ('DB_HOST', 'mariadb'); // 'Om docker - 'mariadb', annars 127.0.0.1
define ('DB_NAME', 'world');
// Skapar en anslutning till MySql och databasen world
$dsn = 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=utf8';
$db = new PDO($dsn, DB_USER, DB_PASSWORD);
?>