Skip to content

Instantly share code, notes, and snippets.

@oelna

oelna/index.php Secret

Created December 18, 2020 15:28
Show Gist options
  • Save oelna/25567e0c9904ad5331b111121c1073de to your computer and use it in GitHub Desktop.
Save oelna/25567e0c9904ad5331b111121c1073de to your computer and use it in GitHub Desktop.
Einfachstes PHP-Gästebuch
<?php
if(file_exists(__DIR__."/gaestebuch.json")) {
$file = file_get_contents(__DIR__."/gaestebuch.json");
$book = json_decode($file, true);
} else {
$book = array();
}
if(isset($_GET["username"]) && isset($_GET["kommentar"])) {
$entry = [
"user" => $_GET["username"],
"comment" => $_GET["kommentar"],
"timestamp" => time()
];
$book[] = $entry;
$savedata = json_encode($book, JSON_PRETTY_PRINT);
file_put_contents(__DIR__."/gaestebuch.json", $savedata);
}
?><!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8" />
<title>meine erste seite mit PHP</title>
</head>
<body>
<h1>Gästebuch</h1>
<form action="index.php">
<label>User<input type="text" name="username" /></label>
<label>Comment<input type="text" name="kommentar" /></label>
<input type="submit" value="Absenden!" />
</form>
<ul class="comments">
<?php
foreach($book as $entry) {
print('<li data-timestamp="'.$entry["timestamp"].'">');
print($entry["user"]." ");
print(date('d.m.y H:i', $entry["timestamp"]));
print(": " . $entry["comment"]);
print("</li>");
}
?>
</ul>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment