Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] extracting sub domain from url


[SOLVED] extracting sub domain from url

Reply
  #1 (permalink)  
Old 07-23-08, 05:06 PM
phpdoctor's Avatar
phpdoctor phpdoctor is offline
Code Guru
 
Join Date: Feb 2007
Location: New Zealand
Posts: 767
Thanks: 4
Thanked 2 Times in 2 Posts
[SOLVED] extracting sub domain from url

Ok i made a thread before about sending the subdomain to php and decided to go
with wildcard subdomain: *.domain.co.nz (used cPanel and works good)

Now i want to get the sub domain out with php...
I tryed using parse_url() but i doesnt work and i also tryed user made code and that didnt that well...

Examples:
http://www.sub.mysite.co.nz
http://sub.mysite.co.nz

These will return "sub" or false if the url is normal like this:
http://www.mysite.co.nz
http://mysite.co.nz

Also what do i use to even get the url... i used $_SERVER['SERVER_NAME']

Any ideas?

Thanks!
Lex
__________________
01010000 01001000 01010000
Reply With Quote
  #2 (permalink)  
Old 07-23-08, 05:18 PM
Ligx Ligx is offline
Newbie Coder
 
Join Date: Nov 2005
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

$url $_SERVER['SERVER_NAME'];
$sub explode('.'$url);
echo 
$sub[0]; 
That should do it.

Reply With Quote
  #3 (permalink)  
Old 07-23-08, 06:23 PM
phpdoctor's Avatar
phpdoctor phpdoctor is offline
Code Guru
 
Join Date: Feb 2007
Location: New Zealand
Posts: 767
Thanks: 4
Thanked 2 Times in 2 Posts
Returns www if... http://www.sub.mysite.co.nz
I know just removed the 'www.' then done something similar to your code...

I relised that this easier then i thought lol

Thanks,
Lex
__________________
01010000 01001000 01010000
Reply With Quote
  #4 (permalink)  
Old 07-23-08, 06:26 PM
Ligx Ligx is offline
Newbie Coder
 
Join Date: Nov 2005
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
you got an whole array there, so if you do
print_r($sub);
you would get all the items separated by the dot.
if your site is www.mine.mysite.com and you want to get out <mine>, you could:
PHP Code:

$url $_SERVER['SERVER_NAME'];
$sub explode('.'$url);
echo 
$sub[1]; 
Reply With Quote
  #5 (permalink)  
Old 07-23-08, 06:53 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Another rare use for the do-while loop!
PHP Code:

$host explode'.'$_SERVER['HTTP_HOST'] );

do
{
    
$sub array_shift$host );    
}
while ( 
'www' == $sub && ! empty( $host ) ); 
Or:
PHP Code:

// specify root hostname and empty $sub
$root 'domain.co.nz';
$sub  '';

// parse current hostname
$host preg_replace'/' preg_quote$root'/' ) . '$/i'NULL$_SERVER['HTTP_HOST'] );

// grab sub hostname
$host explode'.'$host );
do
{
    
$sub array_pop$host );    
}
while ( empty( 
$sub ) && ! empty( $host ) ); 
__________________
The toxic ZCE

Last edited by Keith; 07-23-08 at 07:12 PM.
Reply With Quote
  #6 (permalink)  
Old 07-23-08, 08:55 PM
phpdoctor's Avatar
phpdoctor phpdoctor is offline
Code Guru
 
Join Date: Feb 2007
Location: New Zealand
Posts: 767
Thanks: 4
Thanked 2 Times in 2 Posts
omg array_shift() and do-while... awesome!
Thanks for that nice code
__________________
01010000 01001000 01010000
Reply With Quote
  #7 (permalink)  
Old 07-23-08, 10:03 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
Glad you solved it.
Reply With Quote
  #8 (permalink)  
Old 07-24-08, 08:31 AM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Quote:
Originally Posted by phpdoctor View Post
omg array_shift() and do-while... awesome!
Thanks for that nice code
Take a look at both examples. They will produce different results.

When given the hostname: www.this.is.a.username.domain.co.nz:

Example #1: $sub === 'this';
Example #2: $sub === 'username';
__________________
The toxic ZCE
Reply With Quote
  #9 (permalink)  
Old 07-24-08, 02:53 PM
phpdoctor's Avatar
phpdoctor phpdoctor is offline
Code Guru
 
Join Date: Feb 2007
Location: New Zealand
Posts: 767
Thanks: 4
Thanked 2 Times in 2 Posts
Ye i tryed some scenarios with your examples to test the differences...
I wont be using the long usernames like that so example #1 will work nicely.

Cheers,
Lex
__________________
01010000 01001000 01010000
Reply With Quote
  #10 (permalink)  
Old 07-24-08, 04:35 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
But wildcards will resolve everything to your root.

According to your logic (using example 1), username.domain.co.nz will act the same as username.something.else.domain.co.nz.

In my honest opinion, example 2 would better serve you, making username.domain.co.nz and username.something.else.domain.co.nz two separate accounts.
__________________
The toxic ZCE

Last edited by Keith; 07-24-08 at 04:41 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
New Year 2007 Promotions: We are almost 7 years old! mxhub General Advertisements 0 01-12-07 01:15 PM
New Year 2007 Promotions: We are almost 7 years old! mxhub General Advertisements 1 01-05-07 12:14 PM
Myhomehost.com |Web Hosting & Domain Solutions | Egold & Credit Cards Accepted extreme90 General Advertisements 0 07-19-06 04:09 PM
Extracting URL Links from Google Search Page musmanm80 Perl 1 09-20-05 04:18 AM
extracting info about a url & then manipulate it? Tim Mousel PHP 2 07-21-05 11:09 AM


All times are GMT -5. The time now is 07:50 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.