How to find out the source of the transition to the page?

All within the same site.
The idea is this: a person got to the 404 page, the script immediately starts, sends the page and a link from where they got to 404 to the admin’s e-mail.

The question is, is this achievable? Can I know the page and the link of the transition?


Answer 1, authority 100%

getenv("HTTP_REFERER");

<?php
$a = $_SERVER['HTTP_REFERER'];
if (isset ($a))
{
echo "   :" .$a; // ,  (mail()).
}
?>