Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
How to get consistent results with ($_SERVER['SERVER_NAME'])
Old 05-16-2009, 03:21 AM How to get consistent results with ($_SERVER['SERVER_NAME'])
TWD
TWD's Avatar
King Spam Talker

Posts: 1,190
Trades: 0
The trouble I am having is that depending on whether the user accesses my page using:

http://www.mypage.com

or

http://mypage.com

I get different results.

$_SERVER['SERVER_NAME'] recognizes the "www" as part of the domain name so it makes writing scripts based on this variable quite problematic.

I know it is possible to set a canonical URL through the .htaccess file but that doesn't seem a very robust solution. If the .htaccess file gets deleted, corrupted or whatever it screws up my PHP.

I want to know how I can strip out the "www" to get consistent results.

I suppose a Regex script is one solution but I was hoping there is a purely PHP solution.
TWD is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-16-2009, 04:15 AM Re: How to get consistent results with ($_SERVER['SERVER_NAME'])
TWD
TWD's Avatar
King Spam Talker

Posts: 1,190
Trades: 0
After researching PHP array functions I came up with a script that seems to do the job. The reason for wanting this solved is that I am using a switch statement to serve up different Included content based on which domain name is used to access the files on my server.
There are two domain names with different TLDs pointed at the same site.

Here is the solution in case anyone is interested.

PHP Code:
<?php
//strips out the "www" from the server name
$hostName $_SERVER['SERVER_NAME'];
$hostSlices explode(".",$hostName);
if(
$hostSlices[0]=="www"){array_shift($hostSlices);}
$domain join(".",$hostSlices);
//depending on which domain is used, different content is "included"
switch ($domain){
    case 
"mydomain.com":
        include (
'american.inc.php');
        break;
    case 
"mydomain.co.uk":
        include (
'british.inc.php');
        break;
    }
?>
TWD is offline
Reply With Quote
View Public Profile
 
Old 05-16-2009, 05:44 AM Re: How to get consistent results with ($_SERVER['SERVER_NAME'])
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
if you have access to the apache virtualhost defiinition, there is a neater solution.
what I do is that I put an
Code:
setenv DOMAIN prod
in the vhost definition, and in my prepend initialisation stuff, I do a switch on
PHP Code:
$_SERVER['DOMAIN'
to get that value back.

I use this to have a generic db connection, and to switch the connection string depending of this variable.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to How to get consistent results with ($_SERVER['SERVER_NAME'])
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.11639 seconds with 12 queries