1
0
Fork 0

Init commit

This commit is contained in:
Ethanell 2020-09-12 14:31:17 +02:00
parent 669681890f
commit fbdc4d9e31
5 changed files with 128 additions and 0 deletions

43
createUser.php Normal file
View file

@ -0,0 +1,43 @@
<?php
require("header.php");
if ($_POST["username"] AND $_POST["word"] AND $_POST["nb"]) {
$username = htmlspecialchars($_POST["username"]);
$word = htmlspecialchars($_POST["word"]);
$nb = (int) $_POST["nb"];
$bdd = getPDO();
if ($bdd INSTANCEOF PDO)
try {
$req = $bdd->prepare("SELECT * FROM user WHERE username = ?");
$req->execute(array($username));
if ($req->fetch()) { ?>
<p><strong>Username already exist !</strong></p>
<?php } else {
$req->closeCursor();
$req = $bdd->prepare("INSERT INTO user VALUES (?, ?, ?)");
$req->execute(array($username, $word, $nb));
$_SESSION["p1905458"] = $username;
header("Location: helloworld4.php");
}
$req->closeCursor();
} catch (Exception $e) {
databaseError($e);
}
elseif ($bdd INSTANCEOF Exception)
databaseError($bdd);
}
?>
<form method="POST">
<label for="username">Username: </label>
<input id="username" name="username" required> <br />
<label for="word">Word: </label>
<input id="word" name="word" required> <br />
<label for="nb">Number: </label>
<input id="nb" type="number" name="nb" min="0" max="100" required> <br />
<input type="submit">
</form>
<?php require("footer.php") ?>

15
db.php Normal file
View file

@ -0,0 +1,15 @@
<?php
CONST DB_HOST = "127.0.0.1";
const DB_DATABASE = "TP2";
const DB_USER = "root";
const DB_PASSWORD = "root";
function getPDO() {
try {
$bdd = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_DATABASE . ";charset = utf8", DB_USER, DB_PASSWORD);
$bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $bdd;
} catch (Exception $e) {
return $e;
}
}

2
footer.php Normal file
View file

@ -0,0 +1,2 @@
</body>
</html>

20
header.php Normal file
View file

@ -0,0 +1,20 @@
<?php
CONST DEBUG = true;
require("db.php");
function databaseError(Exception $e) {
if (DEBUG)
die("Error: " . $e->getMessage());
echo "<p><strong>An error occurred with the database, please contact an administrator</strong></p>";
}
session_name("p1905458");
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello World 4</title>
</head>
<body>

48
helloworld4.php Normal file
View file

@ -0,0 +1,48 @@
<?php
$username = "";
if (isset($_POST["username"]))
$username = htmlspecialchars($_POST["username"]);
elseif ($_SESSION["p1905458"]) {
$username = $_SESSION["p1905458"];
if ( ini_get ( " session . use_cookies " ) ) {
$params = session_get_cookie_params();
setcookie(session_name(), "", -1, $params["path"], $params["domain"], $params["secure"], $params["httponly"]);
}
}
require("header.php");
?>
<form method="POST">
<label for="username">Username: </label>
<input id="username" name="username" value="<?= $username ?>" required>
<input type="submit">
</form>
<ul>
<?php
if ($username) {
$bdd = getPDO();
if ($bdd INSTANCEOF PDO)
try {
$req = $bdd->prepare("SELECT * FROM user WHERE username = ?");
$req->execute(array($username));
if (!$user = $req->fetch()) { ?>
<p><strong>No user found !</strong></p>
<a href="createUser.php"><button>Create a new user</button></a>
<?php } else {
for ($i = 0; $i < $user["nb"]; $i++) { ?>
<li>Hello <?= $user["word"] ?> !</li>
<?php }
}
$req->closeCursor();
} catch (Exception $e) {
databaseError($e);
}
elseif ($bdd INSTANCEOF Exception)
databaseError($bdd);
}
?>
</ul>
<?php require("footer.php") ?>