5 Replies - 245 Views - Last Post: 05 February 2012 - 11:16 AM Rate Topic: -----

Topic Sponsor:

#1 Joshdw  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 205
  • Joined: 23-March 09

HTTP_Referer always empty, works fine on other pages.

Posted 04 February 2012 - 10:59 AM

Hello once again DiC members!
I have stumbled across a problem that is really really bugging me, I can't seem to figure out why it is happening.

So I have a PHP page that simply adds the visitors referring url to a database table, and then redirects them to another page. This is the code (the very first lines of code, it's where I "grab" the referring url):
<?php 
include 'dbconfig.php';

if (isset($_SERVER['HTTP_REFERER'])) {
	$ref = strtoupper(htmlspecialchars($_SERVER['HTTP_REFERER']));
	$ref = str_replace("HTTP://", "", $ref);
	$ref = str_replace("HTTPS://", "", $ref);
	$ref = str_replace("WWW.", "", $ref);
	$refsplit = explode('/', $ref);
	$ref = $refsplit[0];
} else {
	$ref = "NA";
}


It always returns an empty value (or if I remove the isset:
Undefined index: HTTP_REFERER on line 3
)

Now if I post the exact same code on a diferent page (for example, the index page) it will work fine. Could it have something to do with the file where the code is written, doesn't actually render a page, it just redirects?

Is This A Good Question/Topic? 0
  • +

Replies To: HTTP_Referer always empty, works fine on other pages.

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 876
  • View blog
  • Posts: 2,250
  • Joined: 15-February 11

Re: HTTP_Referer always empty, works fine on other pages.

Posted 04 February 2012 - 11:41 AM

It depends on where they're coming from. It's not a coding error but some servers don't send the HTTP_REFERER header. You will see that with anonymous proxies and so forth. Users can also remove it with a firewall if configured to do so.

So you see HTTP_REFERER isn't very reliable. My suggestion is to leave the if (isset($_SERVER['HTTP_REFERER'])) where it is.
Was This Post Helpful? 0
  • +
  • -

#3 Joshdw  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 205
  • Joined: 23-March 09

Re: HTTP_Referer always empty, works fine on other pages.

Posted 04 February 2012 - 11:51 AM

I understand that, but the thing is, the same code works perfectly on a diferent page (that renders a layout and that doesn't redirect), with the same referer.
Was This Post Helpful? 0
  • +
  • -

#4 Joshdw  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 205
  • Joined: 23-March 09

Re: HTTP_Referer always empty, works fine on other pages.

Posted 05 February 2012 - 10:26 AM

Bump, still need help! Would really like to find out the problem as soon as possible, it's been bugging me for hours.
Thanks!
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1942
  • View blog
  • Posts: 7,296
  • Joined: 08-August 08

Re: HTTP_Referer always empty, works fine on other pages.

Posted 05 February 2012 - 11:11 AM

It depends on how the page is being accessed. Look at this code:
refer.php:
<?php
if (isset($_SERVER['HTTP_REFERER'])) {
	echo $_SERVER['HTTP_REFERER'];
} else {
	echo "No referrer.";
}
?>

If I go to local host and click on the link to refer.php then I get an address. If I open the same link in a new window, it says "No referrer."
Was This Post Helpful? 2
  • +
  • -

#6 Joshdw  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 205
  • Joined: 23-March 09

Re: HTTP_Referer always empty, works fine on other pages.

Posted 05 February 2012 - 11:16 AM

Woah, thanks so much for the reply, I can't believe it was something so silly.
Kudos to you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1