Closed Thread
Results 1 to 8 of 8

Thread: PHP Referral Question

  1. #1
    BubbaGump64 is offline Newbie
    Join Date
    Feb 2010
    Location
    Buffalo, NY
    Posts
    3
    Rep Power
    0

    PHP Referral Question

    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. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    alienkinetics's Avatar
    alienkinetics is offline Programmer
    Join Date
    Feb 2010
    Location
    Australia
    Posts
    154
    Rep Power
    0

    Re: PHP Referral Question

    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

  4. #3
    BubbaGump64 is offline Newbie
    Join Date
    Feb 2010
    Location
    Buffalo, NY
    Posts
    3
    Rep Power
    0

    Re: PHP Referral Question

    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:
    Code:
    <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":
    Code:
    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.
    Code:
    <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

  5. #4
    alienkinetics's Avatar
    alienkinetics is offline Programmer
    Join Date
    Feb 2010
    Location
    Australia
    Posts
    154
    Rep Power
    0

    Re: PHP Referral Question

    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

  6. #5
    BubbaGump64 is offline Newbie
    Join Date
    Feb 2010
    Location
    Buffalo, NY
    Posts
    3
    Rep Power
    0

    Re: PHP Referral Question

    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

  7. #6
    Feral is offline Programmer
    Join Date
    Jul 2008
    Posts
    163
    Rep Power
    15

    Re: PHP Referral Question

    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.

  8. #7
    joyo is offline Newbie
    Join Date
    Feb 2010
    Posts
    17
    Rep Power
    0

    Re: PHP Referral Question

    you can get the referer from the variable $_SERVER['HTTP_REFERER']

  9. #8
    webcodez's Avatar
    webcodez is offline Programmer
    Join Date
    Feb 2010
    Posts
    148
    Rep Power
    0

    Re: PHP Referral Question

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

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. CodeCall runs on Linode (referral code)
    By Roger in forum Hosting and Registrars
    Replies: 2
    Last Post: 08-11-2010, 05:40 PM
  2. Question:technical question answering system algorithm?
    By vbehzadan in forum General Programming
    Replies: 1
    Last Post: 04-28-2010, 12:41 PM
  3. QUESTION Strcat/Strstr/strcpy Question
    By wgre0111 in forum C and C++
    Replies: 1
    Last Post: 10-19-2008, 03:12 PM
  4. Get the wesbite before the referral
    By TcM in forum PHP Development
    Replies: 13
    Last Post: 07-12-2007, 05:35 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts