Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting the domain name 1

Status
Not open for further replies.
Jul 28, 2005
358
FR
Hi,

Has anybody got an idea how to get the domain name from a url?

I know it's not something you usually need, but I have a page where a header is used on different domains and I need to know the domain the pages is on so a language change link can be used.

I tried $_SERVER['SERVER_NAME'] but that gives me the server subdomain that the domain is located in (this is a cpanel addon domain that resides in teh subdomain of my main one.)

Thanks,

Richard

 
Update:

I tried changing to $_SERVER['HTTP_HOST'], and it seemed fine at first. However if I click on a link that goes to a different domain (these domains are in fact directories under the main directory tree, but are pointed to by external DNS entries), then the original domain is shown with the full directory path to the file.

Better than the subdomain problem I was having but still not correct.

I have no access to the extra domains for admin purposes, someone else has created some sort of domain forwarding to the space on my clients hosting package.

Anyone got any idea?

Richard
 
Richard,

Have a look at this post - think you'll find the answer there:

thread434-1268954

Regards
 
Doesn't seem to be it as if one of the 'other' domain names is used that are forwarded it gives the actual main domain address on the server, not the forwarded domain name.

Sorry if this doesn't make sense, I think this is what's happening. To illustrate it, I'll give an example.

OK, there is a main domain (set up as the main domain in cpanel on my hosting) which I'll call 'maindomain.com'

There is also an addon domain configured which is in reality a subdomain of the maindomain but has a fully qualified domain name pointed to it via DNS (I think that's how cpanel does it). This I'll call addondomain.com

So, in a lot of ways, the server is set up as addondomain.com.maindomain.com and the directory structure is

maindomain.com
addondomain.com

Fine in itself. If I run a script
Code:
<?
echo $_SERVER['SERVER_NAME'] ?>
from the addondomain.com then I get addondomain.com.maindomain.com which is not any good at all

Now, if I change to
Code:
<?
echo $_SERVER['HTTP_HOST'] ?>
I get which is great as it goes.

However, there have been some domains set up that point to another directory which is located at newdirectory like this:

maindomain.com
addondomain.com
newdirectory

These domains have not been created using my hosting cpanel and as far as I'm aware they use some sort of forwarding/alias to get to the correct directory on my server.

Now, when I run a $_SERVER['HTTP_HOST'] in these domains, I get the result of not as I would want.

I can see why this happens as the script obviously queries my server, not the external forward/alias one.

So, how do I get the correct domain name to use in the php files that need to be run? I am thinking now that javascript might be needed to read the domain from the browser but that's not really the route I would like to take but may have no choice.

Sorry this has been a bit convoluted and I hope it makes sense.

Richard
 
the HTTP_HOST is not clever. it gives only what has been actually requested by the browser.

so my guess is that there is something wrong with the actual link in your page. make sure each link is fully qualified (including the protocol).
 
Presumably it only reports what the server sent out, which is why I think it's doing what it is doing.

The links are fine in so much as they are constructed properly, it's just what is being sent to the browser by php that is causing the problem and I think it must be because of the externally configured dpmains and not php etc.

Oh well, nobody's complaining at the mo so I've still got time to try to work it out.

Richard
 
it reports the host that was submitted by the browser. i do not believe that any webservers modify this information before passing it to php but it is possible.

the links should all be constructed as "<a href=" if they are constructed relatively such as "<a href="page.php">Link</a>" the browser will assume that you want the host of the current page.

php does not do anything automagically with links (other than for sessions in some circumstances). the user gets whatever the programmer sends.
 
I definitely have them set as fully constructed links, rather than relative ones (because of the change of domains etc.)

Seems odd that it mucks it up with the external domains from what you say, that's why I thought php must be reporting teh domain from the server rather than the browser. Hmm, major pain.

Thanks for the help though.

If it helps, the way the links are coded is below:

Code:
This bit should work out the Domain and the rest of the URL

function selfURL() { $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : ""; $protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s; $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]); return $protocol."://".$_SERVER['HTTP_HOST'].$port.$_SERVER['REQUEST_URI']; } function strleft($s1, $s2) { return substr($s1, 0, strpos($s1, $s2)); }
 $frurl=selfURL().'&langue=fr'; 
  $enurl=selfURL().'&langue=en' ;
   $deurl=selfURL().'&langue=de';

Code:
And then this is where the links are actually made;  I have only included the French one here, but there are German and English too; just the same but with teh different language bit on the end.

<? $indexurl=strstr($_SERVER['REQUEST_URI'], 'php');	
	if ($indexurl==""){
	$frurl="index.php?langue=fr";
	}?>
          <a href="<? echo $frurl?>"  target="_top"><img src="[URL unfurl="true"]http://www.maindomain.com/image/FRAN001.png"[/URL] alt="<? echo tradnamed("top_languages","fr"); ?>" width="32" height="20" hspace="5" border=0 align=middle></a>

The second bit of script checks if the page is the index or not while the first bit should take the domain name that is being used and append the rest of the URL on as when they change language we want them to be on the same page (property details for an estate agent).

Richard
 
Couldn't update this yesterday as tektips seemed to have some problems.

When I looked at the response headers using FF's webdeveloper toolbar it came up with this:

Code:
Date: Thu, 24 Aug 2006 13:04:45 GMT
Server: Apache
X-Powered-By: PHP/4.4.3
X-UD-Host: webspace.udag.de
X-UD-Method: urlhiding
X-UD-Target: [URL unfurl="true"]http://www.originaldomain.com/folder/folder[/URL]
X-Handling: relax
X-UD-Loopcounter: 3
X-UD-REMOTE_ADDR: **.***.***.***
Connection: close
Content-Length: 19681
Content-Type: text/html
rather than what I expected which would have been
Code:
Date: Thu, 24 Aug 2006 13:04:45 GMT
Server: Apache
X-Powered-By: PHP/4.4.3
X-UD-Host: webspace.udag.de
X-UD-Method: urlhiding
X-UD-Target: [URL unfurl="true"]http://www.newdomain.com[/URL]
X-Handling: relax
X-UD-Loopcounter: 3
X-UD-REMOTE_ADDR: **.**.**.**
Connection: close
Content-Length: 19681
Content-Type: text/html

upon further investigation it seems the new domain was set up to invisibly forward to the folder using url-hiding and this is causing the target domain to be reported.

I reckon I have to use htaccess now to redirect,but am having a few problems with that so have posted in the apach forum.

Thanks for trying to help.

Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top