Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$_SERVER['HTTP_REFERER'] missing

Tags:

php

I want to use $_SERVER['HTTP_REFERER'] in my site but i get the following:

Notice: Undefined index: HTTP_REFERER  

I have tried printing $_SERVER. This outputs the following:

Array (     [HTTP_HOST] => 192.168.1.10     [HTTP_USER_AGENT] => Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:15.0) Gecko/20100101 Firefox/15.0     [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8     [HTTP_ACCEPT_LANGUAGE] => en-us,en;q=0.5     [HTTP_ACCEPT_ENCODING] => gzip, deflate     [HTTP_CONNECTION] => keep-alive     [PATH] => /sbin:/usr/sbin:/bin:/usr/bin     [SERVER_SIGNATURE] => Apache/2.2.3 (CentOS) Server at 192.168.1.10 Port 80     [SERVER_SOFTWARE] => Apache/2.2.3 (CentOS)     [SERVER_NAME] => 192.168.1.10     [SERVER_ADDR] => 192.168.1.10     [SERVER_PORT] => 80     [REMOTE_ADDR] => 192.168.1.77     [DOCUMENT_ROOT] => /var/www/html     [SERVER_ADMIN] => root@localhost     [SCRIPT_FILENAME] => /var/www/html/sandeep/test/hash.php     [REMOTE_PORT] => 53851     [GATEWAY_INTERFACE] => CGI/1.1     [SERVER_PROTOCOL] => HTTP/1.1     [REQUEST_METHOD] => GET     [QUERY_STRING] =>      [REQUEST_URI] => /sandeep/test/hash.php     [SCRIPT_NAME] => /sandeep/test/hash.php     [PHP_SELF] => /sandeep/test/hash.php     [REQUEST_TIME] => 1347365919 ) 

Can anyone help me to find HTTP_REFERER or suggest an alternative to HTTP_REFERER?

like image 789
Sandeep Solanki Avatar asked Sep 11 '12 12:09

Sandeep Solanki


People also ask

Why is Http_referer empty?

There might be several reasons why the referer URL would be blank. It will/may be empty when the enduser: entered the site URL in browser address bar itself. visited the site by a browser-maintained bookmark.

What is $_ server [' Http_referer ']?

$_SERVER['HTTP_REFERER'] Returns the complete URL of the current page (not reliable because not all user-agents support it) $_SERVER['HTTPS'] Is the script queried through a secure HTTP protocol.

In what cases will Http_referer be empty?

Some browsers limit access to not allow HTTP_REFERER to be passed. Type a address in the address bar will not pass the HTTP_REFERER. open a new browser window will not pass the HTTP_REFERER, because HTTP_REFERER = NULL.


2 Answers

From the documentation:

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.

http://php.net/manual/en/reserved.variables.server.php

like image 55
desimusxvii Avatar answered Sep 18 '22 14:09

desimusxvii


When a web browser moves from one website to another and between pages of a website, it can optionally pass the URL it came from. This is called the HTTP_REFERER, So if you don't redirect from one page to another it might be missing

If the HTTP_REFERER has been set then it will be displayed. If it is not then you won't see anything. If it's not set and you have error reporting set to show notices, you'll see an error like this instead:

 Notice: Undefined index: HTTP_REFERER in /path/to/filename.php 

To prevent this error when notices are on (I always develop with notices on), you can do this:

  if(isset($_SERVER['HTTP_REFERER'])) {       echo $_SERVER['HTTP_REFERER'];    } 

OR

 echo isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; 

It can be useful to use the HTTP_REFERER variable for logging etc purposes using the $_SERVER['HTTP_REFERER'] superglobal variable. However it is important to know it's not always set so if you program with notices on then you'll need to allow for this in your code

like image 39
Wearybands Avatar answered Sep 18 '22 14:09

Wearybands