Notice: Undefined index: HTTP_REFERER

HTTP_REFERER is not guaranteed to be sent by the client:

The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.

In your case it's clearly not being sent, so really all you can do is

if(isset($_SERVER['HTTP_REFERER'])) {
  //do what you need to do here if it's set    
   }
else
{
   //it was not sent, perform your default actions here
}

if (isset($_SERVER['HTTP_REFERER'])) {$THE_REFER=$_SERVER['HTTP_REFERER']}