Jump to content

PHP Referral Question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
BubbaGump64

BubbaGump64

    Newbie

  • Members
  • Pip
  • 3 posts
Hey guys,

I'm new here and I tried to do a bit of searching to try and make sure that my question has not already been answered, but I was unsuccessful. I apologize if I have missed the solution. I'd also like to point out that I am not very knowledgeable when it comes to PHP. I can do the basics, but that's about it.

Sorry for all the spaces in between the URLs, I guess that since I don't have 10 posts, I can't have links in my post.

What I'm looking to do is display the URL from a referring page on the page my visitors arrive at. So for example, if someone is coming to my site from something like, "h t t p : / / w w w . g o o g l e . c o m / s e a r c h ? h l = . . . .", I want the page that they arrive at to say, "You Have Just Visited g o o g l e . c o m". I don't want it to say, "You Have Just Visited w w w . g o o g l e . c o m / s e a r c h ? h l = . . . .".

Originally I was using HTTP_REFERER, but after doing a bit of searching, I found that HTTP_REFERER could not be relied on due to third party software or different browsers not always sending the referral information. Would you guys agree with that, that's it's best not to rely on that?

Currently, I am using a javascript and php cookies to show the referral information, which is working fine. My problem is that the referral link shows the entire URL, when what I'd like it to say is simply the domain name (g o o g l e . c o m). Is there a way for me to strip the "h t t p : / / " and anything that occurs after the suffix (.com, .org, .us, etc) so that only the domain name is left? And is using a cookie the best and most reliable way to get this sort of information or is there another alternative that I'm missing.

I appreciate any help you guys can provide. If this question has been posted before, please point me in the right direction and feel free to lock/delete this thread.

Thanks,
BubbaGump

#2
alienkinetics

alienkinetics

    Programmer

  • Members
  • PipPipPipPip
  • 154 posts
In PHP use parse_url() to extract the components of a URL.

PHP: parse_url - Manual

In JavaScript you can use window.location.host

How To Get URL Parts in JavaScript: JavaScript Tutorial Describing the Window.location Object

How are you getting the referer if you arn't using HTTP_REFERER? I would have thought that if HTTP_REFERER was invalid, then document.referrer would also be invalid (?)

PS: Do you use JavaScript history functions? ie: history.previous

#3
BubbaGump64

BubbaGump64

    Newbie

  • Members
  • Pip
  • 3 posts
Thanks alien!

I reverted back to the HTTP_REFERER and using parse_url, it worked perfectly. My only concern is that some browsers may block the referral information. Have you heard of this? I've also heard anti-virus software does the same thing.

Here's the code to this page:
<html>

<head>

</head>

<body>

<?php

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

?>

<div>You Have Just Visited</div>

<div>

  <?php echo parse_url($referer, PHP_URL_HOST); ?>

</div>

</body>

</html>


I was also able to get the other method I was using to work. I'll be honest, I found this script earlier today, before I had even found out about HTTP_REFERER. Like I said, I'm a beginner with PHP, but I'm an even bigger beginner with javascript, so I'm not 100% sure what's going on in this script. All I know is that it appears to be creating a cookie and it works. So if this is more reliable than using HTTP_REFERER, I guess I'd rather use this.

Here's the code for the javascript page called "cookie.js":
var cDomain = self.location.hostname; 

if(cDomain.indexOf(".") < cDomain.lastIndexOf(".")){ 

  var domainOffset = cDomain.indexOf(".")+1  

  cDomain = cDomain.substr(domainOffset); 

}


if(document.referrer.indexOf(cDomain)==-1 && document.referrer!="" && document.cookie.indexOf("referrer=")==-1){

var expDays = 90;

var exp = new Date();

exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

var refdate = new Date();

document.cookie = "referrer=" + escape(document.referrer);

}


if(document.cookie.indexOf("URL=")==-1){

var expDays = 90;

var exp = new Date();

exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

document.cookie = "URL=" + escape(document.URL);

}


var allCookies = document.cookie;

var cPos = allCookies.indexOf("referrer=");

if(cPos != -1){

var cdstart = cPos + 9;

var cdend = allCookies.indexOf(";", cdstart);

if(cdend == -1) cdend = allCookies.length;

var cookieContent = allCookies.substring(cdstart,cdend);

cookieContent = unescape(cookieContent);

var cdatestart = cookieContent.indexOf("&&&", 0);

var cdateend = cookieContent.length;

var cRefer = cookieContent.substring(0,cdatestart);

var cDateRef = cookieContent.substring(cdatestart +2,cdateend)

}

else{

var cRefer = "No cookie";

var cDateRef = "No cookie";

}

Here's the code to the arrival page calling for the referral URL.
<html>

<head>

</head>

<body>


<?php 

  $url = $_COOKIE['referrer'];

?>


<script type="text/javascript" src="cookie.js"></script>

<div>You Have Just Visited</div>

<div><?php echo parse_url($referer, PHP_URL_HOST); ?></div>

</body>

</html>


Sorry for the extremely long posts. I really appreciate your help. But is one of the two above methods better than the other?

Thanks,
BubbaGump

#4
alienkinetics

alienkinetics

    Programmer

  • Members
  • PipPipPipPip
  • 154 posts
My understanding is that both methods will yield the same value. I would use the PHP version.

I doubt modern browsers would not fill out the HTTP referer value, as a lot of sites wont work if the referer isn't set.

Unless they see it as a privacy issue. But, then a site could always use javascript to fill out a HTML form and auto submit it, which means, anyone that really wanted that information could get it.

mmmm

#5
BubbaGump64

BubbaGump64

    Newbie

  • Members
  • Pip
  • 3 posts
Thanks for the info alien, I really appreciate it.

I'm sure I'll be back here in the future posting my noob questions. I plan on going through a lot of the PHP tutorials on this site so hopefully those will help out a lot.

-BubbaGump

#6
Feral

Feral

    Programmer

  • Members
  • PipPipPipPip
  • 162 posts
There are third party programs and vary rarely some anti-virus or more likely firewalls will block the referer from being sent.

The issue is that if they prevent the referer from being sent then there is nothing you can do about it its simply not going to be there.

So yes the PHP HTTP_REFERER global is your best bet.

#7
joyo

joyo

    Newbie

  • Members
  • PipPip
  • 17 posts
you can get the referer from the variable $_SERVER['HTTP_REFERER']

#8
webcodez

webcodez

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
Yep, as joyo said, referer can be requested. obtained, using the $_SERVER variable 'HTTP_REFERER'. That'd be best & easiest way.