Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

index.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. session_start();
  3. use Psr\Http\Message\ResponseInterface as Response;
  4. use Psr\Http\Message\ServerRequestInterface as Request;
  5. use Slim\Factory\AppFactory;
  6. require __DIR__ . '/../vendor/autoload.php';
  7. $app = AppFactory::create();
  8. function addNavbar($response) {
  9. $response->getBody()->write("<html><head></head><body>");
  10. if (isset($_SESSION['username'])) {
  11. $response->getBody()->write("<div><a href='/'>Index</a> | <a href='/blog/create'>Create Blog</a> | <a href='/logout'>Logout</a></div>");
  12. } else {
  13. $response->getBody()->write("<div><a href='/'>Index</a> | <a href='/login'>login</a></div>");
  14. }
  15. return;
  16. }
  17. function addFooter($response) {
  18. $response->getBody()->write("<div>Privacy statement | Cookie Policy | Contact</div>");
  19. $response->getBody()->write("</body></html>");
  20. return;
  21. }
  22. $app->get('/', function (Request $request, Response $response, array $args) {
  23. addNavbar($response);
  24. $response->getBody()->write("<hr/><h1>Onze blog</h1>");
  25. $response->getBody()->write("<ul>");
  26. $response->getBody()->write("<li><a href='/blog/artikel-1'>Blogartikel 1</a></li>");
  27. $response->getBody()->write("<li><a href='/blog/artikel-2'>Blogartikel 2</a></li>");
  28. $response->getBody()->write("<li><a href='/blog/artikel-3'>Blogartikel 3</a></li>");
  29. $response->getBody()->write("<li><a href='/blog/artikel-4'>Blogartikel 4</a></li>");
  30. $response->getBody()->write("</ul>");
  31. $response->getBody()->write("<hr/>");
  32. addFooter($response);
  33. return $response;
  34. });
  35. $app->get('/blog/{slug}', function (Request $request, Response $response, array $args) {
  36. addNavbar($response);
  37. $title = $args['slug'];
  38. $response->getBody()->write("<h1>$title</h1>");
  39. addFooter($response);
  40. return $response;
  41. });
  42. $app->get('/logout', function(Request $request, Response $response, array $args) {
  43. unset($_SESSION['username']);
  44. addNavbar($response);
  45. $response->getBody()->write('logged out');
  46. addFooter($response);
  47. return $response->withHeader('location', '/')->withStatus(302);
  48. });
  49. $app->map(['GET', 'POST'],'/login', function (Request $request, Response $response, array $args) {
  50. if ($request->getMethod() == 'GET') {
  51. addNavbar($response);
  52. $response->getBody()->write('<form action="/login" method="POST">');
  53. $response->getBody()->write('<label for="username">Username</label>');
  54. $response->getBody()->write('<input type="text" name="username"/></br>');
  55. $response->getBody()->write('<label for="password">Password</label>');
  56. $response->getBody()->write('<input type="password" name="password"/></br>');
  57. $response->getBody()->write('<input type="submit">');
  58. addFooter($response);
  59. } else {
  60. $postdata = $request->getParsedBody();
  61. if ($postdata['username'] == 'gebruiker' && $postdata['password'] == 'abcd') {
  62. $_SESSION['username'] = $postdata['username'];
  63. addNavbar($response);
  64. $response->getBody()->write('logged in');
  65. addFooter($response);
  66. return $response->withHeader('location', '/')->withStatus(302);
  67. } else {
  68. $response->getBody()->write('username and/or password incorrect');
  69. }
  70. }
  71. return $response;
  72. });
  73. $app->post('/postcomment', function (Request $request, Response $response, array $args) {
  74. $response->getBody()->write("Postcomment");
  75. return $response;
  76. });
  77. $app->post('/blog/create', function (Request $request, Response $response, array $args) {
  78. $response->getBody()->write("Blog create");
  79. return $response;
  80. });
  81. $app->run();