Just to elaborate on using HTTP_REFERER:
It's generally recommended that you do not depend on this Referer header for any reason. For one, it isn't a required header, so it's possible that a user's browser is configured not to send it.
Second, it doesn't add any security because this header can easily be modified/altered at will by the user.