What are the best practices for adding a session ID or parameter to an email link in PHP to ensure security and prevent unauthorized access?

When adding a session ID or parameter to an email link in PHP, it is important to ensure security by using a secure method to generate the session ID and validate it on the receiving end to prevent unauthorized access. One way to achieve this is by using PHP's session functions to generate a unique session ID and encrypt it before adding it to the email link. Additionally, you can include a timestamp or expiration time in the session ID to limit its validity.

<?php
// Generate a unique session ID
$session_id = session_create_id();

// Encrypt the session ID
$encrypted_session_id = base64_encode($session_id);

// Add the encrypted session ID to the email link
$email_link = "https://example.com/page.php?session_id=" . $encrypted_session_id;

// Send the email with the link
// Make sure to include instructions for the recipient to click the link within a certain time frame
?>