Hi all,

I’m currently using a php page, as index.php, for promoting some domains for sale. i.e. if some one enters in browser, say: [url]http://www.certain_domain.com,[/url] the php page displays the domain name entered among several other wording.

However, if the user types only, [url]http://certain_domain.com[/url] (without www), then, only ’.com’ is displayed in this case.

The code I’m using is:

<?php
$data = explode(".", $GLOBALS["HTTP_HOST"]);
$domain = $data[1] . "." . $data[2];
?> <h1 align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="3">Bla, bla, bla, bla</font><br>
<h1 align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" color="#33CC99">
<?php echo $domain; ?> </font></b></h1>

What do I need to add/modify in the coding to make this also workable when user omits the ‘www’ when entering the url in the browser?

I will highly appreciate your help.

ACUMARA

    you want to look at WHY that is happening. These two lines:

    $data = explode(".", $GLOBALS["HTTP_HOST"]); 
    $domain = $data[1] . "." . $data[2];

    take the domain apart and ASSUME there are three parts. So, you should count() the parts in the array before deciding which parts to use.

    good luck!

      Thank you Craigh for your prompt reply.

      Yes these php lines assume they are allways 3 parts.

      Now, I feel very bad but I have to confess that I don't know how to modify the lines.

      Can give some idea or where to read how to do it?

      Thank you again.

      ACUMARA

        $data = explode(".", $GLOBALS["HTTP_HOST"]); 
        $domain = $data[1] . "." . $data[2]; 
        

        should be

        $data = explode(".", $GLOBALS["HTTP_HOST"]); 
        $n = sizeof ($data);
        $domain = $data[n-2] . "." . $data[$n-1]; 
        

        At least that's one way.

        If there are 3 parts, $n=3 and $domain will consist of $data[1] and $data[2] (as in your orig. code) ... if there are two parts, $n=2 and $domain will consist of $data[0] and $data[1] which would resolve your stated problem. If someone enters 4+ parts, $domain will still only consist of the last two. Get it?

        p.s. If you expect me to be really anal, I'd say my code only works if n is at least 2 ... i.e., if there is a period somewhere in the user's input. Data validation is always a good idea as much as I like to skip it sigh

          amachargue,

          I do really appreciate your help.

          For n to be =, >, than 2, it's all right. A domain name is expected to be composed of at least two parts anyway. So for me it's all right.

          I will try this now.

          Thank you again

          acumara

          ps.- It works, just added the missing '$' before first 'n'

          Thnak you.

            parse_url() be easier as it gives you a hash which is easier to check 🙂

            $urlparts = parse_url("www.test.com");
            empty($urlparts['host'])
              or $domain = $urlparts['host'];
            

            maybe...

              i'll be damned, this PHP has a function for everything. But looks like the hash that parse_url returns just contains a "host" which would still need to be parsed to accomplish what's at issue here. Does make it easy to strip off the 'http://' though.

                Write a Reply...