$_SERVER['HTTP_REFERER'] Could Be Undefined in Some Cases

Notice: Undefined index: HTTP_REFER

Generally, you don't need using function isset() to test the existence of every $_SERVER element before using, because they seems always available to use. As a matter of fact, at least in one case, you cannot use $_SERVER directly without checking the existence.

Element $_SERVER['HTTP_REFERER'] can be use to link back to the previous referer, which is a URL refer to the current page. If your browser have no cookies about the website at all, or the page is loaded before a new session started, the element 'HTTP_REFERER' should not have been set at the moment. Your page may throw the error:

Notice: Undefined index: HTTP_REFER in 'html_path/file_name' on line xxxx

In such case, you must use function isset() to check the existence of the element before using it. Thus, your web pages will be safer and robust.

Of course, you can check the existences of all $_SERVER in your codes for 100% safe, but IMHO, it's too trivial and looks a little messy, you can just take care the potential pitfalls and not necessarily to check common elements of $_SERVER. This would be acceptable in most cases.

Leave a Reply

Your email address will not be published. Required fields are marked *