Webbserverprogrammering 1

Show sourcecode

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

webbserverprogrammering/projekt/snake_oil_seller/php/

about_us.php
add_to_cart.php
admin.php
buy_cart.php
config.php
contact.php
create_products.php
create_tables.php
createtable.php
dbconnection.php
delete_post.php
delete_tables.php
deletepost.php
deletetable.php
entry.php
header.php
insert_posts.php
insertposts.php
leaderboard.php
log_in.php
log_out.php
main.php
my_account.php
question_maker.php
quiz_form.php
quiz_list.php
quiz_maker.php
result.php
select_posts.php
selectposts.php
shop.php
shop_item.php
shopping_cart.php
sign_in.php
title_card.php
update_posts.php
updateposts.php
user_verified.php
verify_page.php

sign_in.php

100 lines UTF-8 Windows (CRLF)
<?php
  
// Initierar sessionen
  
session_start();

  
/** @var PDO $dbconn*/
  
include ("dbconnection.php");
?>
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Sign in</title>
  </head>
  <link href="../css/snake_oil.css" rel="stylesheet">
  <link href="../css/log-sign_in.css" rel="stylesheet"> 
<body>
  <?php
    $message 
null;
    if (isset(
$_POST['username']) && isset($_POST['password']) && !empty($_POST['username']) && !empty($_POST['password'])) {
        
        
// Hämtar info från formuläret
        
$username $_POST['username'];
        
$password $_POST['password'];

        
// Förbereder frågan till databasen
        
$stmt $dbconn->prepare("SELECT username FROM users WHERE username = ?");
        
$stmt->execute([$username]);
        
$user $stmt->fetch(PDO::FETCH_ASSOC);

        
// Kollar om användaren finns i databasen
        
if ($user){
          
// Användaren finns
          
echo "En användare med samma namn finns redan";
        } else {
          
// Användaren finns inte, skickas till quiz listan
          
try {
            
// Hashar lösenordet
            
$hash password_hash($passwordPASSWORD_DEFAULT);
            
            
$sql "INSERT INTO users (username, email, password, is_verified, reg_date) 
              VALUES (?, ?, ?, ?, now())"
;
            
$stmt $dbconn->prepare($sql);
            
$data = array($usernamenull$hash"no");
            
$stmt->execute($data);
                
            
// Nytt konto tillagt
            
$lastId $dbconn->lastInsertId();

            
$_SESSION["username"] = $username;
            
$_SESSION["usertype"] = "user";
            
$_SESSION["email"] = null;
          }
          catch(
PDOException $e)
              {
              echo 
$sql "<br>" $e->getMessage();
          }
          
$dbconn null;
          
          
header("Location: verify_page.php");
        }

    } else {
        
$message .= "";

    }
    echo 
$message;
  
?>
  <!-- Titel -->
  <?php include "title_card.php";?>

    <!-- Skapa Konto ruta --> 
    <main>
      <h1><strong>Create account:</strong></h1>
      <form method="post" action="" id="form"> 
        <table> 
          <tr>
            <td><span style="display:flex; justify-content: right;">Username*:</span></td>
            <td><input type="text" name="username" size=40 maxlength=100 placeholder="Username"></td>
          </tr>
          <tr>
            <td><span style="display:flex; justify-content: right;">Password*:</span></td>
            <td><input type="password" name="password" size=40 maxlength=100 placeholder="Password"></td>
          </tr>
          <tr>
            <td><span style="display:flex; justify-content: right;">* = Obligatory:</span></td>
            <td><button type="submit">Continue</button></td>
          </tr>
        </table>
      </form>
    </main>
    <!--Tillbaka-->
    <div id="exit_div">
      <h4>Back</h4>
      <span>
        <a href="log_out.php">Website</a>
        <a href="log_in.php">Log in</a>
      </span>
    </div>
  </body>
</html>