liberate Posted May 14, 2013 Share Posted May 14, 2013 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 Quote Link to comment Share on other sites More sharing options...
trq Posted May 14, 2013 Share Posted May 14, 2013 You could (and should) use preg_match to match a pattern instead of exploding on a period. Quote Link to comment Share on other sites More sharing options...
requinix Posted May 14, 2013 Share Posted May 14, 2013 If you use $url elsewhere there's an unrelated problem: $_SERVER['QUERYSTRING'] needs an underscore: QUERY_STRING. And there needs to be a ? between PHP_SELF and the QUERY_STRING. Quote Link to comment Share on other sites More sharing options...
liberate Posted May 14, 2013 Author Share Posted May 14, 2013 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 Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted May 15, 2013 Share Posted May 15, 2013 (edited) 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 May 15, 2013 by jazzman1 Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted May 15, 2013 Share Posted May 15, 2013 (edited) 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 May 15, 2013 by jazzman1 Quote Link to comment Share on other sites More sharing options...
requinix Posted May 15, 2013 Share Posted May 15, 2013 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); } Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted May 15, 2013 Share Posted May 15, 2013 @requinix, with respect to you but that example is a pretty static for me, that's why I like RegEX. Quote Link to comment Share on other sites More sharing options...
liberate Posted May 15, 2013 Author Share Posted May 15, 2013 (edited) 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 May 15, 2013 by liberate Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted May 15, 2013 Share Posted May 15, 2013 (edited) @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 May 15, 2013 by jazzman1 Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted May 15, 2013 Share Posted May 15, 2013 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 ) Quote Link to comment Share on other sites More sharing options...
liberate Posted May 15, 2013 Author Share Posted May 15, 2013 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 Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted May 15, 2013 Share Posted May 15, 2013 (edited) What is the of $_SERVER["HTTP_HOST"]? To work this RegEx the value must begin with : "http://", "https//", "http://www" or "www". Edited May 15, 2013 by jazzman1 Quote Link to comment Share on other sites More sharing options...
liberate Posted May 15, 2013 Author Share Posted May 15, 2013 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. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted May 15, 2013 Share Posted May 15, 2013 (edited) 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 May 15, 2013 by jazzman1 Quote Link to comment Share on other sites More sharing options...
liberate Posted May 15, 2013 Author Share Posted May 15, 2013 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. Quote Link to comment Share on other sites More sharing options...
liberate Posted May 15, 2013 Author Share Posted May 15, 2013 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. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted May 15, 2013 Share Posted May 15, 2013 It's a little late in Canada Give me back the result of: echo $_SERVER["HTTP_HOST"]; Quote Link to comment Share on other sites More sharing options...
liberate Posted May 15, 2013 Author Share Posted May 15, 2013 (edited) www.liberate.mysite.com So yes that picked up the username. Edited May 15, 2013 by liberate Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted May 15, 2013 Share Posted May 15, 2013 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. Quote Link to comment Share on other sites More sharing options...
liberate Posted May 15, 2013 Author Share Posted May 15, 2013 Good night jazzman1 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 Quote Link to comment Share on other sites More sharing options...
liberate Posted May 15, 2013 Author Share Posted May 15, 2013 Hi requinix I see you browsing. more to come... Quote Link to comment Share on other sites More sharing options...
liberate Posted May 15, 2013 Author Share Posted May 15, 2013 (edited) Hi requinix I tried your script. Couldn't figure out what to put for $user= Tried $user=$host but didn't work. Suggestions? Tom Edited May 15, 2013 by liberate Quote Link to comment Share on other sites More sharing options...
liberate Posted May 15, 2013 Author Share Posted May 15, 2013 (edited) 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 May 15, 2013 by liberate Quote Link to comment Share on other sites More sharing options...
requinix Posted May 15, 2013 Share Posted May 15, 2013 (edited) Did you also change the -11s? [edit] And the last line should be $host = substr($host, 0, -11); Edited May 15, 2013 by requinix Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.