Webbserverprogrammering 1

Show sourcecode

The following files exists in this folder. Click to view.

Webserver1/Ovningar/mySQL/

admin.php
fetch_car_things.php
fetch_kompisar.php
fetch_users.php
mysql1.php
mysql2.php
mysql3.php
mysql3_satt_att_sortera.php
mysql3car.php
mysql3garage.php
mysql3owner.php
welcome.php

mysql2.php

98 lines UTF-8 Windows (CRLF)
<?php
session_start
();

include(
'../../incl/dbconnection.php');
/**
 * @var PDO $dbconn
 */

try {
  if (isset(
$_POST['username']) && isset($_POST['password'])) {
    
$username $_POST['username'];
    
$password $_POST['password'];

    
$_SESSION["username"] = $username;
    
$sqlSelect "SELECT * FROM users WHERE username = ?";

    
$sqlInsert "INSERT INTO users (username, password, user_type)
      VALUES(?, ?, ?)"
;

    
$sqlUpdateTime "UPDATE users SET last_login=CURRENT_TIMESTAMP WHERE username = ?";

    
$stmtSelect $dbconn->prepare($sqlSelect);
    
$stmtSelect->execute([$username]);
    
$row $stmtSelect->fetch(PDO::FETCH_ASSOC);

    if (
$row) {
      echo 
$row['password']."\n";
      echo 
$password;
      if (
password_verify($password$row['password'])) {
        
$stmtUpdate $dbconn->prepare($sqlUpdateTime);
        
$stmtUpdate->execute([$username]);
        if (
$row['user_type'] == 'Admin') {
          
header("Location:admin.php");
        } else {
          
header("Location:welcome.php");
        }
      } else {
        echo 
"Felaktigt lösenord!";
      }
    } else {
      
$stmt $dbconn->prepare($sqlInsert);
      
$data = array($usernamepassword_hash($passwordPASSWORD_BCRYPT), "Noob");
      
$stmt->execute($data);
      
header("Location:welcome.php");
    }
  }
} catch (
PDOException $e) {
  echo 
"<br>" $e->getMessage();
}
$dbconn null;
?>

<!DOCTYPE html>
<html lang="sv">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Inloggning</title>
  <style>
    table {
      border: 1px solid black;
    }
  </style>
</head>

<body>
  <form method="post">
    <h1>Inloggningssida</h1>
    <table>
      <tbody>
        <tr>
          <td>
            Användarnamn:
          </td>
          <td>
            <input type="text" name="username" id="username" required>
          </td>
        </tr>
        <tr>
          <td>
            Lösenord:
          </td>
          <td>
            <input type="password" name="password" id="password" required>
          </td>
        </tr>
        <tr>
          <td>
            <input type="submit" value="Logga in / Registrera">
          </td>
        </tr>
      </tbody>
    </table>
  </form>
</body>

</html>