Solved

display just domain

Posted on 2006-11-06
5
206 Views
Last Modified: 2008-03-06
hello,
I have this in my page
<? echo $_SERVER["HTTP_HOST"]; ?>
but if i type in the (address bar) www.domain.com
I see the echo as www.domain.com
is there a way to only display domain.com ?
0
Comment
Question by:Sabrin
  • 3
5 Comments
 
LVL 2

Expert Comment

by:eheimer
ID: 17886730
have you tried $_SERVER["SERVER_NAME"] instead?  The output of that should depend on what you have setup as the domain in your web server config (httpd.conf).

The other thing you could do is:

<?php
    $host = $_SERVER["HTTP_HOST"]
    if (!(strpos($host, 'www.')==false)) {
        $host = substr($host, 4);
    }
    echo $host;
?>
0
 

Author Comment

by:Sabrin
ID: 17886828
this code

<?php
    $host = $_SERVER["HTTP_HOST"]
    if (!(strpos($host, 'www.')==false)) {
        $host = substr($host, 4);
    }
    echo $host;
?>

is not working
0
 

Author Comment

by:Sabrin
ID: 17886832
this works

    $host = $_SERVER["HTTP_HOST"];
    if (!(strpos($host, 'www.')==true)) {
        $host = substr($host, 4);
    }
    echo $host;
0
 

Author Comment

by:Sabrin
ID: 17886833
is there a ways to have all subdomains too ?
0
 
LVL 43

Accepted Solution

by:
ravenpl earned 500 total points
ID: 17887638
Use http://pl.php.net/manual/en/function.explode.php to chop the host into parts.
$host_arr = explode('.', $host);
$curr_domain = "";
while($last = array_pop($host_arr)) {
 $curr_domain = $last . ($curr_domain ? "." :  "" ) . $curr_domain;
 print "$curr_domain\n";
}
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question