How can one determine if a link is opened directly or in a new window/tab in PHP?

When working with links in PHP, it can be useful to determine if a link is opened directly or in a new window/tab. One way to achieve this is by checking the HTTP_REFERER variable, which contains the URL of the page that referred the user to the current page. If the HTTP_REFERER is the same as the current page URL, then the link was likely opened directly. If the HTTP_REFERER is different, then the link was likely opened in a new window/tab.

if(isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] == $_SERVER['REQUEST_URI']) {
    echo "Link opened directly";
} else {
    echo "Link opened in a new window/tab";
}