Skip to content
PHP Logo

How can PHP developers ensure secure access to sensitive data through links sent via email?

To ensure secure access to sensitive data through links sent via email, PHP developers can generate unique tokens for each link and store them in a database along with an expiration time. When the user clicks on the link, the token can be validated against the database to ensure its authenticity and validity before granting access to the sensitive data.

php
// Generate a unique token
$token = bin2hex(random_bytes(16));

// Store the token and expiration time in the database
$expiry = date('Y-m-d H:i:s', strtotime('+1 hour'));
// Save $token and $expiry in the database

// Send the link with the token to the user via email
$email = 'user@example.com';
$link = 'https://example.com/access.php?token=' . $token;
mail($email, 'Access Link', $link);