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/typr/

14 filer

create_tables.php
dbconnection.php
getUrl.php
index.php
notes.txt
plan.php
profile.php
redovisning.html
resources/
session_check.php
styles/
sv_200.txt
sv_829.txt
test.php

index.php

110 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

function dump($dump) {
 echo 
"<pre>";
 
var_dump($dump);
 echo 
"</pre>";
}
function 
clog($output$with_script_tags true) {
 
$js_code 'console.log(' json_encode($outputJSON_HEX_TAG) . ');';
 if (
$with_script_tags) {
  
$js_code '<script>' $js_code '</script>';
 }
 echo 
$js_code;
}
function 
cerr($output$with_script_tags true) {
 
$js_code 'console.error(' json_encode($outputJSON_HEX_TAG) . ');';
 if (
$with_script_tags) {
  
$js_code '<script>' $js_code '</script>';
 }
 echo 
$js_code;
}


session_start();
include 
"session_check.php";

$notice "";
if (isset(
$_POST)) {
 if (isset(
$_POST['username']) && isset($_POST['password']) && isset($_POST['submit'])) {
  
// include "dbconnection.php";

  
$username $_POST['username'];
  
$password $_POST['password'];

  
$notice login_user($username$password);
 }
}


function 
login_user($username$password) {
 include 
"dbconnection.php";
 
$sql "SELECT user_id, username, password, usertype FROM typr_users WHERE username=?";
 
$users = [];

 try {
  
$stmt $dbconn->prepare($sql);
  
$stmt->execute([$username]);
  
$users $stmt->fetchAll(PDO::FETCH_ASSOC);

  if (
count($users) === 1) {
   if (
password_verify($password$users[0]['password'])) {
    try {
     
$sql "UPDATE typr_users SET online_status=?, last_login_date=now() WHERE username=?";
     
$stmt $dbconn->prepare($sql);
     
$stmt->execute(["online"$username]);

     
$_SESSION['typr'] = ['loggedIn' => true'usertype' => $users[0]['usertype'], 'userId' => $users[0]['user_id']];

     
// header("Location:index.php");

    
} catch (PDOException $e) { cerr(isset($e->errorInfo[2]) ? $e->errorInfo[2] : (method_exists($e"getMessage") ? $e->getMessage() : $e)); }
   } else {
    return 
"Incorrect login details";
   }

   
// redirect - not actually though. Page changes but no explicit redirect!
   // session login
   // set online status
  
} else {
   return 
"Incorrect login details";
  }
 } catch (
PDOException $e) { cerr(isset($e->errorInfo[2]) ? $e->errorInfo[2] : (method_exists($e"getMessage") ? $e->getMessage() : $e)); }
}




// if logged in
 // redirect to start page

?>

<!DOCTYPE html>
<html lang="sv">
<head>
 <title>typr</title>
 <meta charset="utf-8">
 <link rel="stylesheet" type="text/css" href="styles/main.css">
</head>
<body>
 <div id="wrapper">
  <h1>typr</h1>
  <main>
   <h2>Log in</h2>
   <div id="login-container">
    <form method="post" action="">
     <input type="text" name="username" placeholder="Username"><br>
     <input type="password" name="password" placeholder="Password"><br>
     <input type="submit" name="submit" value="Log in"><br>
     <a href="">Don't have an account? Create one here.</a>
    </form>
   </div>
   <p id="login-notice"><?= $notice ?></p>
  </main>
 </div>
</body>
</html>