SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: Getting a URL

  1. #1
    SitePoint Zealot
    Join Date
    Feb 2004
    Location
    UK
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Getting a URL

    Not sure if this is the right forum but anyway.

    Is there any way I can get the url the user has entered and work out what it is?

    I need to extract the middle, eg from www.msn.co.uk I want the msn bit.

  2. #2
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You probably want $_SERVER['HTTP_HOST'] and then remove the .co.uk part.

  3. #3
    SitePoint Evangelist Waffles's Avatar
    Join Date
    Nov 2005
    Posts
    435
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm thinking use a combination of explode() and substr()...or maybe some regex?

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Where are they entering the URL? Do you mean you need to get the URL they came from or something else?
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spikeZ
    Where are they entering the URL? Do you mean you need to get the URL they came from or something else?
    Now I'm not sure about what he means anymore. Is the URL entered into a form or into the browser's address bar? My solution would of course only make sense in the latter scenario.

  6. #6
    SitePoint Zealot
    Join Date
    Feb 2004
    Location
    UK
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for the late reply, I didn't realise I had got some many replies. I forgot to subscribe to the thread!

    The address is entered into the browser address bar.

  7. #7
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, then use my suggestion.

  8. #8
    SitePoint Zealot
    Join Date
    Feb 2004
    Location
    UK
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, thats worked perfect. I am trying to find out how to remove the .co.uk but can't find anything. I have read something about using explode() but can't get it to work.

  9. #9
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    $url_parts parse_url($_SERVER['HTTP_HOST']);
    echo 
    $url_parts['host']; 
    That should get you the domain (or host) name.

  10. #10
    SitePoint Zealot
    Join Date
    Feb 2004
    Location
    UK
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks but I think I get the same result from just $domain = $_SERVER['HTTP_HOST'];

  11. #11
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Ah you're right. Hmmm... I thought that parse_url split the domain up too. Perhaps that was another function I used somewhere, I'll see if I can find it for you.

    Edit>>
    Can't find it, sorry.

    Will the TLD always be .co.uk?
    In that case you could simply try a str_replace('.co.uk', '', $url_bit); to get rid of the extension. You might need to do something similar to get rid of the subdomain as well.

  12. #12
    SitePoint Zealot
    Join Date
    Feb 2004
    Location
    UK
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For the moment all the domains are .co.uk but this could change which would be a problem if I were to use explode, i think.

  13. #13
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    explode() might be tough too though, as you might not know if it's a tld with 1 or two parts. If you generally will know which tlds there will be, you could delete them from the host name using a regex. I'm not very good at regex's though, sorry.


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
  •