Webbserverprogrammering 1

Källkod

Följande filer och mappar finns under mappen webbserverprogrammering.
Mappar visas till vänster och filer till höger. Klicka på en fil eller mapp för att öppna nedan eller visa dess innehåll.

webbserverprogrammering/projects/quiz/

22 filer

admin.php
create_account.php
create_quiz.php
css/
dbconnection.php
hacktest.php
highscore.php
index.php
js/
login.php
mysql_create_table_options.php
mysql_create_table_questions.php
mysql_create_table_quizzes.php
mysql_create_table_results.php
mysql_create_table_submits.php
mysql_create_table_users.php
planering.txt
profile.php
quizzes.php
resources/
result.php
session_variable_array_check.php

create_account.php

80 lines ASCII Windows (CRLF)
<?php
error_reporting
(-1); // Report all type of errors
ini_set('display_errors'1); // Display all errors 
ini_set('output_buffering'0); // Do not buffer outputs, write directly

session_start();
include 
"session_variable_array_check.php";

if (
$_SESSION['quiz']['loggedIn']) {
 
header("Location:index.php");
}


// POST RECEIVE
$redMessage "";
include(
"dbconnection.php");
if (isset(
$_POST['displayname']) && isset($_POST['username']) && isset($_POST['password']) && isset($_POST['submitted'])) {
 
$username $_POST['username'];

 
$sql "SELECT * FROM quiz_users WHERE username=?";
 
$stmt $dbconn->prepare($sql);
 
$stmt->execute([$username]);
 
$result $stmt->fetchAll(PDO::FETCH_ASSOC);

 if (
count($result) < 1) {

  
$displayname $_POST['displayname'];
  
$password $_POST['password'];
  
$password password_hash($passwordPASSWORD_DEFAULT);

  
$sql "INSERT INTO quiz_users (displayname, username, password, user_level) VALUES (?, ?, ?, ?)";

  
$stmt $dbconn->prepare($sql);
  
$stmt->execute([$displayname$username$password"regular"]);

  
// user now registered

  
$userId = (int) $dbconn->lastInsertId();

  
// log in
  
$_SESSION['quiz'] = ['loggedIn' => true'admin' => false'displayname' => $displayname'userId' => $userId'username' => $username];
  
header("Location:index.php");

 } else {
  
$redMessage "Username is taken! Try another one.";
 }
 
/*
 check if user already exists
 when logged in, redirect/success!!
 */

}



?>

<!DOCTYPE html>
<html lang="sv">
<head>
 <title>Create account</title>
 <link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
 <a href="index.php" id="logo">
  CuriousQuizzes
 </a>
 <main class="centered">
  <h1>Create account</h1>
  <form method="post" action="">
   <input type="text" name="displayname" placeholder="Display name"><br>
   <input type="text" name="username" placeholder="Username"><br>
   <input type="password" name="password" placeholder="Password"><br>
   <input type="submit" name="submitted" value="Create account"><br>
  </form>
  <p class="redMessage"><?= $redMessage ?></p>
  <a class="smallMessage" href="login.php">Already have an account? Log in here!</a>
 </main>
</body>
</html>