Advertisement
Guest User

startPage

a guest
Mar 7th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.39 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <!--
  3.  
  4. -->
  5. <html>
  6. <head>
  7.     <meta charset="UTF-8">
  8.     <meta name="viewport" content="width=device-width, initial-scale=1">
  9.     <link rel="stylesheet" type="text/css" href="footer2.css">
  10.     <title> Webbutveckling 2</title>
  11.  
  12. </head>
  13. <body>
  14.  
  15. <h1> Uppgift 2a - Skapa en användare och logga sedan in! </h1>
  16.  
  17. <?php
  18.  
  19. //nedan inkluderar/bifogar jag sidan "footer.php".
  20. include 'footer2.php';
  21.  
  22. echo "<br>";
  23. session_start();
  24.  
  25. function createUser(){
  26.  
  27.     $user_username = $_POST['userName'];
  28.     $user_password = $_POST['passWord'];
  29.     $password_hashed = password_hash($user_password, PASSWORD_DEFAULT);
  30.  
  31.     $user_usernameLower = strtolower($user_username);
  32.  
  33.     $accountInfo = $user_usernameLower . ":" . $password_hashed . "\n";
  34.     $filepath = fopen('useraccounts.txt', 'a+');
  35.     $file = "useraccounts.txt";
  36.  
  37.     if (isset($_POST['submit_button'])) {
  38.  
  39.         if (file_exists($file)) {
  40.             $content = file_get_contents($file);
  41.             $content = explode("\n", $content);
  42.             $users = array();
  43.  
  44.             foreach ($content as $value) {
  45.                 $user = explode(':', $value);
  46.                 $users[$user[0]] = $user[1];
  47.             }
  48.  
  49.             if (isset($users[$_POST['userName']])) {
  50.                 echo "that username is already taken";
  51.             } elseif (empty($user_username) || empty($user_password)) {
  52.                 echo "Please, Fill in both password and username";
  53.             } else {
  54.                 fwrite($filepath, $accountInfo);
  55.                 echo "Account created! ";
  56.             }
  57.             fclose($filepath);
  58.         }
  59.     }
  60. }
  61.  
  62. createUser();
  63. ?>
  64.  
  65. <br>
  66.  
  67. <form action="" method="post">
  68.     <label> Skapa användare nedan </label><br>
  69.     <label>Username: </label><input type="text" name="userName" placeholder="Username">
  70.     <br>
  71.     <label>Password: </label> <input type="password" name="passWord" placeholder="Password">
  72.     <br><br>
  73.     <input type="submit" name="submit_button" value="createUser">
  74. </form>
  75. <br>
  76.  
  77. <br>
  78. <form action="index.php" method="post">
  79.     <label> Logga in nedan</label><br>
  80.     <label>Username: </label><input type="text" name="userName" placeholder="Username">
  81.     <br>
  82.     <label>Password: </label> <input type="password" name="password" placeholder="Password">
  83.     <br><br>
  84.     <input type="submit" name="submit_button1" value="Login">
  85. </form>
  86.  
  87.  
  88. </body>
  89. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement