HTTP REFERRALS

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lloydsmods
Forum Newbie
Posts: 22
Joined: Wed Aug 14, 2002 2:03 pm
Location: NC

HTTP REFERRALS

Post by lloydsmods »

Okay, I'm using $_SERVER['HTTP_REFERER'] to track down where my visitors are coming from. I'm posting the results to a MySQL database, as well. Question is this: How do I trim the URL down so that I only capture the site or the site and page without all the extra garbage?

For example, I get this kind of stuff:

http://www.google.com/search?hl=en&lr=& ... ts&spell=1

Suppose I just want to trim off everything after google.com? I know there is a way to do this but I haven't had much luck.
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

Sure using parse_url()

Code: Select all

<?php
    $referral = $_SERVER['HTTP_REFERER'];
    if (isset($referral)) {
        $pieces = parse_url($referral);
        $referer = $pieces[host];
        $pagereferer = $pieces[path];
    } else {
        $referer = 'unknown';
    }
    echo 'http://' . $referer . $pagereferer;
?>
lloydsmods
Forum Newbie
Posts: 22
Joined: Wed Aug 14, 2002 2:03 pm
Location: NC

Excellent

Post by lloydsmods »

Worked like a charm. I never cease to be amazed at how quickly you can find quality info in this forum.

Thanks a million!
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

No problem, glad I could be of assistance to you ;)
Post Reply