MyBB Community Forums

Full Version: How to display my domain name in a page?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I've actually got a very simple question that I hope someone can help me resolve it.

I have a page like: subdomain.domain.com/page

I am wondering if there is any kind of code I could use to determine the subdomain and show it on the page like this:

subdomain.domain.com

If you're wondering why I need this, I just need to use it on 404 error pages. If there is a PHP or Javascript code available to do this, I would be grateful.

Thanks a lot.
If you're using cPanel I know when you go to customize your error pages (404 included) there is a shortcut code so that the page displays the referring page you were trying to reach. So if "subdomain.domain.com/page" wasn't there the 404 page would display the referring page as "subdomain.domain.com/page".
as found in phpinfo()...

<?php echo $_SERVER["HTTP_HOST"]; ?>
(2009-02-11, 07:57 PM)MiNT Wrote: [ -> ]as found in phpinfo()...

<?php echo $_SERVER["HTTP_HOST"]; ?>

Damn, beat me to it.
Great! Thanks a lot for all the help..
(2009-02-11, 07:57 PM)MiNT Wrote: [ -> ]as found in phpinfo()...

<?php echo $_SERVER["HTTP_HOST"]; ?>

Is it possible to display just the domain.com portion of the URL?

For example if my site is http://www.abc123.com, I can input a php snippet so the title on the page shows as ABC123.COM.
You can use preg_replace to remove the www. from the above string;

<?php echo preg_replace('/^www./','',$_SERVER["HTTP_HOST"]); ?>

if you want to make it upper case as well, use strtoupper.

<?php echo strtoupper(preg_replace('/^www./','',$_SERVER["HTTP_HOST"])); ?>

EDIT:
if you want to stip out all subdomains and not just www. then you can use this
<?php preg_match('/[^.]+\.[^.]+$/', $_SERVER["HTTP_HOST"],$match);
echo $match[0];?>
I use:
<?php
if(isset($_SERVER['SERVER_NAME']))
 {
  echo $_SERVER['SERVER_NAME'];
 }
?>
(2009-03-01, 09:47 PM)MiNT Wrote: [ -> ]You can use preg_replace to remove the www. from the above string;

<?php echo preg_replace('/^www./','',$_SERVER["HTTP_HOST"]); ?>

if you want to make it upper case as well, use strtoupper.

<?php echo strtoupper(preg_replace('/^www./','',$_SERVER["HTTP_HOST"])); ?>

EDIT:
if you want to stip out all subdomains and not just www. then you can use this
<?php preg_match('/[^.]+\.[^.]+$/', $_SERVER["HTTP_HOST"],$match);
echo $match[0];?>

Awesome. Thanks, worked perfectly.