What are the security implications of using JavaScript for pop-up windows in PHP?

Using JavaScript for pop-up windows in PHP can introduce security vulnerabilities such as cross-site scripting (XSS) attacks. To mitigate this risk, it is recommended to sanitize any user input before using it to generate JavaScript code. Additionally, consider using server-side validation to ensure that only trusted sources can trigger the pop-up window.

<?php

// Sanitize user input before using it to generate JavaScript code
$popup_message = htmlspecialchars($_GET['popup_message']);

// Server-side validation to ensure only trusted sources can trigger the pop-up window
if($_SERVER['HTTP_REFERER'] == 'https://www.trustedsite.com') {
    echo "<script>alert('$popup_message');</script>";
} else {
    echo "Access denied.";
}

?>