Advertisement
LoicEth

Untitled

Mar 26th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. <?php
  2. // This is mandatory to use sessions !
  3. session_start();
  4.  
  5. // Here we check if user is already authenticated, if so we redirect to index.php
  6. if (isset($_SESSION["username"]) and isset($_SESSION["auth_ok"])){
  7. // this function print message in the logs of the web server
  8. error_log("user authenticated");
  9. // This is the way we do a redirect with PHP, by setting a HTTP header and exiting
  10. header("Location: template.php");
  11. die();
  12. }
  13. /** The name of the database */
  14. define('DB_NAME', 'cv');
  15. /** MySQL database username */
  16. define('DB_USER', 'root');
  17. /** MySQL database password */
  18. define('DB_PASSWORD', '');
  19. /** MySQL hostname */
  20. define('DB_HOST', 'localhost');
  21. $conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  22. if ($conn->connect_error) {
  23. error_log("Connection failed: " . $conn->connect_error);
  24. }
  25. $conn->set_charset('utf8');
  26. // Perform SQL query
  27. if (isset($_POST['u'])) {
  28. $query = $conn->query('SELECT * FROM `user` WHERE user.username= "' . $_POST['u'] . '"');
  29. $tata = $query->fetch_array();
  30. // Check if there was an error
  31. if ($tata['password'] == sha1($_POST['p'])) {
  32. var_dump($tata);
  33. $_SESSION['username'] = $_POST['u'];
  34. $_SESSION['auth_ok'] = 1;
  35.  
  36. }
  37. }
  38. ?>
  39. <?php
  40.  
  41. // We check if the form was submitted
  42. if (isset($_POST["u"]) && isset($_POST["p"])) {
  43. error_log("try to authenticate");
  44. // We extract the form data and store them in variables
  45. $email = $_POST["u"];
  46. $pass = $_POST["p"];
  47. // Hum, something is wrong here right ?
  48. if (True) {
  49. error_log("auth OK");
  50. $_SESSION["u"] = $_POST["u"];
  51. $_SESSION["auth_ok"] = 1;
  52. header("Location: login.php");
  53. die();
  54. } else {
  55. error_log("auth not OK");
  56. $err = "erreur d'authentification";
  57. $_SESSION["u"] = '';
  58. $_SESSION["auth_ok"] = 0;
  59. }
  60.  
  61. }
  62. ?>
  63.  
  64. <!DOCTYPE html>
  65. <html lang="en">
  66. <link href="css/login.css" rel="stylesheet">
  67. <head>
  68. <meta charset="UTF-8">
  69. <title>Login</title>
  70. <!DOCTYPE html>
  71. <html>
  72. <div class="login">
  73. <h1>Se connecter</h1>
  74. <form method="post">
  75. <input type="text" name="u" placeholder="Username" required="required" />
  76. <input type="password" name="p" placeholder="Password" required="required" />
  77. <button type="submit" class="btn btn-primary btn-block btn-large">Laissez moi entrer.</button>
  78. </form>
  79. </div>
  80.  
  81. </body>
  82. </html>
  83.  
  84. </head>
  85. <body>
  86.  
  87. </body>
  88. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement