Jump to content

Value of Subdomain


liberate

Recommended Posts

Hi Everyone

 

This stuff is well beyond my understanding.

 

I have been using the code below written by JKG http://forums.phpfreaks.com/topic/234954-how-to-get-the-value-of-subdomain/?hl=%2Bfind+%2Bsubdomain&do=findComment&comment=1208823  And it has been working flawlessly, until I ran into something unexpected. 

 

It doesn't work with usernames (subdomains) that contain a dot "." For instance john.doe http://www.john.doe.mysite.com

<?php
$url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . $_SERVER['QUERYSTRING'];
$urlParts = explode('.', $_SERVER['HTTP_HOST']);
if(strpos($url, 'www')){
$subdomain_value = $urlParts[1];
} else { 
$subdomain_value = $urlParts[0]; 
};
echo $subdomain_value;
?>

For my application, anything between the www. and .mysite.com is the subdomain ie username. So with http://www.this.is.the.username.mysite.com username =  this.is.the.username  Although I have not run across anyone with more then one dot in their username, the possibility exists. Doubtful if any username would have more than 2 dots.

 

Background info: I have wildcard subdomain redirected to root. Works without needing any htaccess. The subdomain is just a placeholder for username. Then I use php to pull out the username (subdomain) and retrieve their saved info from mysql.

 

As I said, I am way over my head. Anyone know of a solution? 

 

Thanks Tom

 

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

Thankyou for the reply trq and requinix

 

I have not been using the PHP_SELF or QUERY_STRING parts so the error hasn't shown up.

 

I am googling for preg_match() and subdomain, so far every example I have seen is trying to either replace the subdomain or alter css for a particular subdomain, anything but retreive the subdomain.

 

Found this in http://ca2.php.net/preg_match  which returns php.net (domain and TLD)

<?php
// get host name from URL
preg_match('@^(?:http://)?([^/]+)@i',
    "http://www.php.net/index.html", $matches);
$host = $matches[1];

// get last two segments of host name
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
echo "domain name is: {$matches[0]}\n";
?>

So would the code start out something like this? 

<?php
$url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ? $_SERVER['QUERY_STRING'];
$urlParts = preg_match(????, $_SERVER['HTTP_HOST'], $matches);

I assume the idea is to match out the http://www. and the .mydomain.com and anything left between is the subdomain.

 

This is really outside of my meager knowledge of php.

 

Any help would be appreachiated.

 

Thnaks Tom

 

 

Link to comment
Share on other sites

Take a look at this regular expression which I've builded for you.

 

Next URL's will be matched:

<?php 

// https://www.john.doe.mysite.com
// https://www.john.doe.mysite.com/
// http://www.john.doe.mysite.com
// http://www.john.doe.mysite.com/
// https://john.doe.mysite.com
// https://john.doe.mysite.com/
// http://john.doe.mysite.com
// http://john.doe.mysite.com/
// www.john.doe.mysite.com
// www.john.doe.mysite.com/

// OR their variants
// http://www.test.doe.my.siteName.mysite.com/


// get host name from URL
$host = "http://test.doe.mysite.com/";

preg_match('/((\bhttps?:\/\/(www.)?|\bwww\.)([a-z.]+)?\.([a-z]+\.[a-z]{2,4}(\/)?)$)/i', $host, $matches);

echo '<pre>'.print_r($matches, true).'</pre>';

If you want to get only the prefix, domain, sub-domain, etc....just call the array, something like:

echo "My domain name is: ".$matches[5];

echo "My sub-domain name is: ".$matches[6];

Hope, this will help you.
        
jazz..

Edited by jazzman1
Link to comment
Share on other sites

So, few things:

 

If you want to match the URL only as domain name for example -  $host = "http://mysite.com/", this regEx won't work b/s we need to make a period optional before the domain name.

 

Take that:

preg_match('/((\bhttps?:\/\/(www.)?|\bwww\.)([a-z.]+)?\.?([a-z]+\.[a-z]{2,4}(\/)?)$)/i', $host, $matches);
Edited by jazzman1
Link to comment
Share on other sites

I'm pretty sure this thread is spiraling away from the really simple problem of removing "www" and "mysite.com" from the HTTP_HOST.

$host = "www.john.smith.mysite.com"; // $_SERVER["HTTP_HOST"]
if (strncmp($host, "www.", 4) == 0) {
    $host = substr($host, 4);
}
if (substr_compare($host, ".mysite.com", -11 /* -strlen(".mysite.com") */) == 0) {
    $host = substr($host, -11);
}
Link to comment
Share on other sites

Thank you requinix  and  jazzman1

 

As long as requinix solution works with usernames that contain dots or dashes, simple is always better to me.

 

http://www.any.user-name.mysite.com username = any.user-name

 

http://any.user-name.mysite.com username = any.user-name  (with or without www)

 

I have been reading for hours goggle searches on ``preg_match() subdomain url`` and not geting anywhere, except very confused.

 

requinix so at the end of your script does $host = username

 

 

Since I am not scanning random urls, RegEX did seem like overkill. In the past few hours I have read more RegEX expressions then I care to count, not that I understood any of them.

 

Thanks again

Edited by liberate
Link to comment
Share on other sites

@libarate, don't underestimate the power of RegEx....

 

If you want to match links that contains a "-" symbol just add it between the square brackets, containing sub-domain expression.

 

Example:

// get host name from URL
$host = "http://www.any.user-name.mysite.com";

preg_match('/((\bhttps?:\/\/(www.)?|\bwww\.)([a-z.-]+)?\.?([a-z]+\.[a-z]{2,4}(\/)?)$)/i', $host, $matches);

echo '<pre>'.print_r($matches, true).'</pre>';

Results:

Array
(
    [0] => http://www.any.user-name.mysite.com
    [1] => http://www.any.user-name.mysite.com
    [2] => http://www.
    [3] => www.
    [4] => any.user-name.mysit
    [5] => e.com
)
Edited by jazzman1
Link to comment
Share on other sites

Ops.....my typo sorry for that:

<?php 

// get host name from URL
$host = "http://www.any-user.name.mysite.com";

preg_match('/((\bhttps?:\/\/(www.)?|\bwww\.)([a-z.-]+)?\.([a-z]+\.[a-z]{2,4}(\/)?)$)/i', $host, $matches);

echo '<pre>'.print_r($matches, true).'</pre>';

Results:

Array
(
    [0] => http://www.any-user.name.mysite.com
    [1] => http://www.any-user.name.mysite.com
    [2] => http://www.
    [3] => www.
    [4] => any-user.name
    [5] => mysite.com
)
Link to comment
Share on other sites

Hi jazzman1

 

I don't underestimate the power of RegEx just don't understand it.

 

 

So array [4] is always the username?

 

 

Does this work?

<?php 

$host = "$_SERVER["HTTP_HOST"]";
preg_match('/((\bhttps?:\/\/(www.)?|\bwww\.)([a-z.-]+)?\.([a-z]+\.[a-z]{2,4}(\/)?)$)/i', $host, $matches);

$user = $matches[4];

Thanks Tom

Link to comment
Share on other sites

How would you use $host = http://www.any-user.name.mysite.com; ?

 

 

For lack of the technical way of saying it.....The script needs to retrieve the url from the address bar.

 

When a registered member comes to mysite.com they use their personal url http://www.their-username.mysite.com or http://their-username.mysite.com or http://theirusername.mysite.com

 

It is a multi-user self-replicating site. The admin.php file then retrieves the username from the "address bar"  and looks up the rest of their info from mysql.

Link to comment
Share on other sites

Ok, I've created a new one that will get all posible variants to this moment.

 

The sub-domain always will be -  $user = $matches[4];

 

The domain - $user = $matches[5];

 

You can use $host = $_SERVER["HTTP_HOST"] to get the domain (sub-domain) name for every user.

 

Next URL's will be matched:

<?php

// http://www.test-net.mysite.com
// https://www.test-net.mysite.com/
// www.test-net.mysite.com
// test-net.mysite.com
// http://mysite.com (only with domain name)
// etc......

    // get host name from URL
    $host = "canada.host-name.mysite.com";
     
    preg_match('/^((\bhttps?:\/\/(www.)?|\bwww\.)?([a-z.-]+)?\.)?([a-z]+\.[a-z]{2,4}\/?)$/i', $host, $matches);
     
    echo '<pre>'.print_r($matches, true).'</pre>';
Edited by jazzman1
Link to comment
Share on other sites

Hi jazzman1

 

You're up late. I live about 1 1/2 hours from you.

 

So in my admin.php I would do.

<?php 

$host = $_SERVER["HTTP_HOST"];

preg_match('/^((\bhttps?:\/\/(www.)?|\bwww\.)?([a-z.-]+)?\.)?([a-z]+\.[a-z]{2,4}\/?)$/i', $host, $matches);

$user = $matches[4];

And then look up $user in mysql.

 

I will give it a try.

 

Thanks

Tom

my email liberate at uc2 dot biz (old address that gets lots of spam) email me. 

Link to comment
Share on other sites

I put this into the top of my_admin.php

<?php 

$host = $_SERVER["HTTP_HOST"];

preg_match('/^((\bhttps?:\/\/(www.)?|\bwww\.)?([a-z.-]+)?\.)?([a-z]+\.[a-z]{2,4}\/?)$/i', $host, $matches);

// $user = $matches[4];

echo '<pre>'.print_r($matches, true).'</pre>';

echo returned empty

Array()

I assume I am doing something wrong.

Link to comment
Share on other sites

You are doing something wrong:

  // get host name from URL
    $host = "http://www.liberate.mysite.com/";
     
    preg_match('/^((\bhttps?:\/\/(www.)?|\bwww\.)?([a-z.-]+)?\.)?([a-z]+\.[a-z]{2,4}\/?)$/i', $host, $matches);
     
    echo '<pre>'.print_r($matches, true).'</pre>';

Results:

Array
(
    [0] => http://www.liberate.mysite.com/
    [1] => http://www.liberate.
    [2] => http://www.
    [3] => www.
    [4] => liberate
    [5] => mysite.com/
)

But.... it's too late for me :)

 

I'm going bed.

Link to comment
Share on other sites

Good night jazzman1 :sleeping:

 

So $host has to be a known variable? Then I don't see how this can work. Username changes each time a different member comes to the site.

 

My username might be liberate, but what about the other users? $host = "http://www.liberate.mysite.com/";  would program the site only to work for me.

 

Am I missing something here?

 

Tom

Link to comment
Share on other sites

Going back to @requinix script

 

 

I put this into the top of my_admin.php

<?php 

$host = $_SERVER["HTTP_HOST"]; i
if (strncmp($host, "www.", 4) == 0) {
    $host = substr($host, 4);
}
if (substr_compare($host, ".mysite.com", -11 /* -strlen(".mysite.com") */) == 0) {
    $host = substr($host, -11);
}

echo $host;

Echo returned:

liberate.mysite.com

 

So $host is not subdomain but contains both sub and domain, and yes I did alter the "mysite.com" in the code to the real domain.com.

 

Something not quite right.

Edited by liberate
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.