How can session data be securely passed in a URL query string in PHP?

When passing session data in a URL query string in PHP, it is important to ensure that the data is secure and not easily tampered with by malicious users. One way to achieve this is by using encryption to protect the session data before appending it to the URL. This can help prevent unauthorized access or modification of the session data during transit.

<?php
// Start the session
session_start();

// Encrypt the session data before passing it in the URL query string
$encrypted_data = base64_encode(openssl_encrypt(session_encode(), 'AES-256-CBC', 'your_secret_key', 0, 'your_iv'));

// Append the encrypted data to the URL
$url = 'http://example.com/page.php?session_data=' . urlencode($encrypted_data);

// Redirect to the URL with the encrypted session data
header('Location: ' . $url);
exit;
?>