Results 1 to 6 of 6

Thread: extract domain name from HTTP_ABC

  1. #1
    Join Date
    Jan 2010
    Posts
    70

    extract domain name from HTTP_ABC

    I would like to extract the domain name (e.g. techarena.in) from the URL (http://www.techarena.in/test/test-page.html) so that in any case the site tries to add the link to my site on any page of their site. Thus, I could test the domain name and forward emails accordingly. For this I tested the code with the help of explode() but that doesn't worked. So can you help me how should I do that?

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: extract domain name from HTTP_ABC

    I think the below code should work:
    PHP Code:
    <?php
    $siteurl 
    'http://username:password@hostname/path?arg=value#anchor';
     
    print_r(parse_url($siteurl));
     
    echo 
    parse_url($siteurlSITE_PATH);
    ?>
    This should result you with

    Array
    (
    [scheme] => http
    [host] => hostname
    [username] => username
    [password] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor
    )

  3. #3
    Join Date
    Nov 2008
    Posts
    1,054

    Re: extract domain name from HTTP_ABC

    Yes you can do that but instead of using explode(), you have to use parse_url() which will check the url and convert the webpage link into the link that you want. For this you specify the term that you want to check in the link. Modify your php code to parse the $_SESSION['techarena'].

  4. #4
    Join Date
    Jan 2010
    Posts
    70

    Re: extract domain name from HTTP_ABC

    Thank you for the help and for that information but I am unable to find it on php.net. And I recently understood how to use explode(), and according to me far more better than parse_url(). Here is my PHP code:

    PHP Code:
    $siteurl $_SESSION['techarena'];
    $refhost parse_url($siteurlSITE_HOST);
    if (
    $refhost == 'techarena.in' || $refhost == 'www.techarena.in') {
    $recipients "admin@techarena.in,my@email.com";
    }
    else {
    $recipients "my@email.com";


  5. #5
    Join Date
    Nov 2008
    Posts
    996

    Re: extract domain name from HTTP_ABC

    For example if the domain name is something like "www.techarena.in" then you code it as below:

    PHP Code:
    function GetDomain($siteurl)
    {
        
    $replacewww ereg_replace('www\.','',$siteurl);
        
    $domainname parse_url($replacewww);
        if(!empty(
    $domainname["host"]))
        {
            return 
    $domainname["host"];
        } 
        else
        {
            return 
    $domainname["path"];
        }


  6. #6
    Join Date
    Nov 2008
    Posts
    1,192

    Re: extract domain name from HTTP_ABC

    PHP Code:
     <?php
        preg_match
    ("/^(http:\/\/)?([^\/]+)/i""http://www.techarena.in/index.html"$domain);
        
    $domainname $domain[2];
        
    preg_match("/[^\.\/]+\.[^\.\/]+$/"$domainname$domain);
        echo 
    "domain name is: {$domain[0]}\n";
        
    /* Output is techarena.in */
    ?>

Similar Threads

  1. Replies: 7
    Last Post: 28-12-2010, 10:05 PM
  2. Replies: 1
    Last Post: 16-09-2009, 10:11 AM
  3. backup domain controller in a small business server 2003 domain
    By Ashish Goenkar in forum Small Business Server
    Replies: 4
    Last Post: 31-01-2009, 12:16 AM
  4. Replies: 3
    Last Post: 11-12-2008, 12:28 PM
  5. Replies: 1
    Last Post: 19-06-2008, 01:58 AM

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
  •  
Page generated in 1,713,482,604.48690 seconds with 17 queries