The selected solution is wrong. This is not how to solve this problem. $_SERVER['HTTP_REFERER']
is not reliable. It can be spoofed and omitted which breaks this solution.
To prevent this you need to implement the POST/REDIRECT/GET
pattern. Here is a tutorial that shows how it is done in PHP.