What are the best practices for handling data in frames within a PHP website?

When handling data in frames within a PHP website, it is important to ensure that the data being passed between frames is sanitized to prevent any security vulnerabilities such as cross-site scripting (XSS) attacks. One best practice is to validate and sanitize the data before displaying it in the frame to prevent any malicious code from being executed.

// Sanitize data before displaying in frame
$data = isset($_GET['data']) ? htmlspecialchars($_GET['data']) : '';
echo "<iframe src='frame.php?data=$data'></iframe>";