Database design: keys, relationships and normalization
Design tables that avoid duplicated data: primary keys, foreign keys, one-to-many and many-to-many relationships.
Send a one-time link that expires, and store only a hash of the token.
CREATE TABLE password_resets (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
token_hash CHAR(64) NOT NULL,
expires_at DATETIME NOT NULL,
used TINYINT(1) NOT NULL DEFAULT 0,
UNIQUE KEY (token_hash)
);<?php
$token = bin2hex(random_bytes(32));
$hash = hash('sha256', $token);
$stmt = $pdo->prepare('INSERT INTO password_resets (user_id, token_hash, expires_at) VALUES (?, ?, DATE_ADD(NOW(), INTERVAL 30 MINUTE))');
$stmt->execute([$userId, $hash]);
$link = 'https://example.com/reset.php?token=' . $token; // email this$hash = hash('sha256', (string)($_GET['token'] ?? ''));
$stmt = $pdo->prepare('SELECT user_id FROM password_resets WHERE token_hash = ? AND used = 0 AND expires_at > NOW()');
$stmt->execute([$hash]);
$row = $stmt->fetch();Hash it with password_hash(), mark the token as used, and end all other sessions of that user.
Design tables that avoid duplicated data: primary keys, foreign keys, one-to-many and many-to-many relationships.
A folder layout and a few habits that keep a growing PHP site tidy, secure and easy to maintain.