PHP Crew

Discussion in 'OT Technology' started by o2, Apr 25, 2007.

  1. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    11
    Location:
    Toronto
    I came up with this little function for filtering the domain name from a referrer (for stat tracking purposes).

    PHP:
    function getDomainName($url) {
       
    $ref parse_url($url);  
        if (
    $ref['host'] != "") { 
            
    $ref_check substr($ref['host'], 04);
        
            
    // check for www
            
    if ($ref_check == "www.") {
                   
    $ref['host'] = substr($ref['host'], 4); 
            } 
            
    // Output filtered url
        
    return $ref['host'];
        } 
        
    My testing showed that it seems to work pretty well for any url I tried, but someone told me its not a very good way to go about it.

    What do you think?
     
  2. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    All it does is chop "www." off the front if it's there. Is that what you want? It sounds like what you want is the domain name of the referer, in which case it seems to me that it would make more sense to go from the right.
     
  3. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    11
    Location:
    Toronto
    Uhhhh no. Go look up the parse_url function.
     
  4. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    I just looked at parse_url. Your code still just chops "www." off the front of the host, if it's there. If thoe host doesn't start with "www." then it doesn't do anything. If that's your intended behavior then great (although it would probably be nicer to do it with a regular expression), but if what you want is to always get the domain name, then it's not doing what you intend.
     
  5. retorq

    retorq What up bitch??

    Joined:
    Dec 14, 2006
    Messages:
    6,061
    Likes Received:
    0
    Location:
    Mohave Desert
    Woudn't it be better to cut the ref into sections at the . and keep the last two sets?? That way it'll drop what ever the first set is, www. ftp. intranet., etc. keeping the rest, www.offtopic would be oftopic.com, uploads.offtopic.com would also be offtopic.com, offtopic.com would be offtopic.com.

    amirite
     
  6. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    11
    Location:
    Toronto
    I want to turn suff like

    http://www.domain.com/wdqd.php?awdw=231
    and
    http://domain.com/wqdqdwq

    into domain.com

    And this script seems to do that just fine.
     
  7. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
  8. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    11
    Location:
    Toronto
    Thats exactly how I want it to stay. My current system strips off the subdomains, and I dont want it doing that anymore.
     
  9. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    Ah, OK, cool.

    In that case, the only thing I would do differently is to use a regular expression instead of substr:

    PHP:
    $ref parse_url($url);
    $ref['host'] = preg_replace("/^www./"""$ref['host']);
    Just a bit cleaner looking :dunno:
     
  10. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    11
    Location:
    Toronto
    Hmm that works :)

    What are the solution for totally stripping off the subdomains as well (for another project)?

    Right now I currently use
    PHP:
    function getDomainName($url) {
                
    $url strtolower($url);
                if (
    substr($url07) == 'http://')
                        
    $url substr($url7);
                if (
    strpos($url'/'))
                        
    $url substr($url0strpos($url'/'));
                while (
    strpos($url'.') != strrpos($url'.'))
                        
    $url substr($urlstrpos($url'.')+1);
                return 
    $url;
        }
    But it doesn't work with 2 part extensions (such as .co.uk).
     
  11. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    Yeah, I was thinking about how one might do that earlier. You might have to just compile a list of things valid domains can end in and then check them one-by-one. It gets especially tricky with things like .ca because there are .ca domains and also .bc.ca, .ab.ca, etc for each province, so you'd probably want to compare from longest to shortest.

    Maybe someone else has a better solution? There's probably a module somewhere that does all this.
     

Share This Page