Webbserverprogrammering 1

Show sourcecode

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

webbserverprogrammering/submissions/projekt-matkort-handler/

.github/
add_logs.php
admin/
api/
card_balance.php
classes/
config/
food_logs.php
forgot_password.php
includes/
index.php
insert_restaurants.php
install.php
login.php
logout.php
public/
register.php
reset_password.php
verify.php

reset_password.php

81 lines UTF-8 Windows (CRLF)
<?php
session_start
();
include_once 
'./config/database.php';
include_once 
'./classes/User.php';

$error '';
$success '';
$token $_GET['token'] ?? $_POST['token'] ?? '';

// Check if token exists and is not expired
if (empty($token)) {
    
$error "Ingen säkerhetsnyckel (token) angavs. Länken kan vara ogiltig.";
}

if (
$_SERVER['REQUEST_METHOD'] === 'POST' && !empty($token)) {
  
$password $_POST['password'] ?? '';
  
$password_confirm $_POST['password_confirm'] ?? '';
  
  if (empty(
$password) || empty($password_confirm)) {
    
$error "Vänligen fyll i båda lösenordsfälten.";
  } elseif (
$password !== $password_confirm) {
    
$error "Lösenorden matchar inte.";
  } elseif (
strlen($password) < 6) {
    
$error "Lösenordet måste vara minst 6 tecken långt.";
  } else {
    try {
      if (
User::resetPassword($pdo$token$password)) {
          
$success "Ditt lösenord har återställts. Du kan nu logga in med ditt nya lösenord.";
      } else {
          
$error "Länken är ogiltig, har upphört att gälla, eller så gick något snett.";
      }
    } catch (
Exception $e) {
      
$error "Något gick fel.";
    }
  }
}

$page_title 'Återställ Lösenord';
require_once 
'./includes/header.php';
?>

<div class="container">
  <div class="login-box">
    <h1>Skapa nytt lösenord</h1>

    <?php if ($error): ?>
      <div class="error-message">
        <?php echo htmlspecialchars($error); ?>
      </div>
    <?php endif; ?>
    
    <?php if ($success): ?>
    <div class="success-message">
      <?php echo htmlspecialchars($success); ?>
    </div>
    <p class="register-link" style="margin-top: 15px;">
      <a href="./login.php">Gå till inloggning</a>
    </p>
    <?php else: ?>
        <?php if (!empty($token) && ($error !== "Länken är ogiltig, har upphört att gälla, eller så gick något snett." && $error !== "Ingen säkerhetsnyckel (token) angavs. Länken kan vara ogiltig.")): ?>
        <form method="POST" action="reset_password.php">
          <input type="hidden" name="token" value="<?php echo htmlspecialchars($token); ?>">
          
          <div class="form-group">
            <label for="password">Nytt lösenord:</label>
            <input type="password" id="password" name="password" required>
          </div>
          
          <div class="form-group">
            <label for="password_confirm">Bekräfta nytt lösenord:</label>
            <input type="password" id="password_confirm" name="password_confirm" required>
          </div>
          
          <button type="submit" class="btn btn-primary">Återställ lösenord</button>
        </form>
        <?php endif; ?>
    <?php endif; ?>
  </div>
</div>

<?php require_once './includes/footer.php'?>