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

verify_page.php

89 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>Account verification</title>
  <link href="../css/snake_oil.css" rel="stylesheet">
  <link href="../css/log-sign_in.css" rel="stylesheet"> 
</head>
<body>
  <?php
    
include "title_card.php";

    if (!empty(
$_POST['email']) && !empty($_POST['password'])) {

      
$email_input $_POST['email'];
      
$password $_POST['password'];
      
$username $_SESSION['username'] ?? null;

      if (
$username === null) {
        echo 
"No active session";
        exit;
      }

      
// Hämta både id och password
      
$stmt $dbconn->prepare("SELECT id, password FROM users WHERE username = ?");
      
$stmt->execute([$username]);
      
$res $stmt->fetch(PDO::FETCH_ASSOC);

      if (
$res && password_verify($password$res["password"])) {

        
$user_id $res["id"];

        
// Spara email i databasen
        
$update $dbconn->prepare("UPDATE users SET email = ? WHERE id = ?");
        
$update->execute([$email_input$user_id]);

        
// Spara i session
        
$_SESSION["user_id"] = $user_id;
        
$_SESSION["email"] = $email_input;

        
$subject "Verification";
        
$text "Click link";

        if (
$_SERVER['SERVER_NAME'] != "localhost") {
          
mail($email_input$subject$text);
          echo 
"Mail sent";
        } else {
          echo 
"Localhost: <a href='user_verified.php'>Verify</a>";
        }

      } else {
        echo 
"Wrong password";
      }
    }
  
?>
  
  <!-- Skapa Konto Ruta -->
  <main>
    <form method="post" action="" id="form"> 
      <table> 
        <tr>
          <td><span style="display:flex; justify-content: right;">E-mail*:</span></td>
          <td><input type="text" name="email" size=40 maxlength=100 placeholder="firstname.lastname@mail.com"></td>
        </tr> 
        <tr>
          <td><span style="display:flex; justify-content: right;">Rewrite password*:</span></td>
          <td><input type="password" name="password" size=40 maxlength=30 placeholder="Password"></td>
        </tr> 
        <tr>
          <td><span style="display:flex; justify-content: right;">* = Obligatory:</span></td>
          <td><button type="submit">Send verification code</button></td>
        </tr>
      </table>
    </form>
  </main>
  <!--Tillbaka-->
  <div id="exit_div">
    <h4>Back</h4>
    <a href="log_out.php">Startsida</a>
  </div>
  </body>
</html>