Jump to content

Value of Subdomain


liberate

Recommended Posts

Legthened the domain name to correctly represent real condition.

<?php 

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

// $user = $host
echo $host;

echo returned:

.mysite1234.com

 

So this returned the domain instead of the subdomain.

 

So what has to be altered to get the subdomain?

Edited by liberate
Link to comment
Share on other sites

Hey Tom,

 

I like that forum b/s the people especially gurus (moderators) they provide us lots of useful information.

 

So, going back to my RegEx if you're planning to match any domains and sub-domains containing integers inside their names just set those characters to this particular regex class. 

 

For example:

<?php

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

I know that my example is a little complicate to you, but in fact that it's much more powerful and flexible.

 

If you want to learn more about regex, I highly recommend you to start from here.

 

Regards,

 

jazz.

Link to comment
Share on other sites

Hi Jazz

I didn't mean I was giving up on your method. requinix showed up after you went to bed. I had not spent anytime on requinix method. Initially I didn't get anywhere with it but i soon realized that the numbers were digits of offset, and I needed to more closely represent the length of my domain.

 

So far neither method has worked.

 

The one main question I have with your approach is how are you retrieving the visitor's username without $host = $_SERVER["HTTP_HOST"]; ?

 

If you need http:// then build it with $host = "http://" . $_SERVER['HTTP_HOST'], so that $host holds a variable that changes with every visitor's username. I can't see how a static host=http://www.username.mysite1234.com can work in a dynamic self-replicated multi-user website.

 

I am either missing something here or we are taking about two different uses.

 

I know you like the RegEx method because it will work with any domain, so anyone else can use the same code, without having to alter it to suit them.

 

Like most sites usernames are alpha-numeric with allowed punctuation of dot and dash (.-) In my case underscore is not allowed (_)

 

I appreciate your help.

 

Tom

Edited by liberate
Link to comment
Share on other sites

Hey Tom,

 

you have a lot of things to do if you're planning to administrate that server by php.

 

In mine point of you php is not suited to do this.

 

So, are you able to run as root bash (perl) scripts on this machine or you have only access to the admin control panel? 

Edited by jazzman1
Link to comment
Share on other sites

Hi Jazz

 

Your code worked once I changed to $host = "http://" . $_SERVER['HTTP_HOST'];

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

echo returned:

Array
(
    [0] => http://www.us.er-na.me-s-er.mysite1234.com
    [1] => http://www.us.er-na.me-s-er.
    [2] => http://www.
    [3] => www.
    [4] => us.er-na.me-s-er
    [5] => mysite1234.com)

 

Hurray

 

(was getting tired of  :facewall:  beating my head against a brick wall)

 

So now I should just need to make $user = $matches[4];

 

Hurray!!  $user = $matches[4]; worked. And I see my username and my info retrieved from mysql.

 

I just registered a new fictitious new member with the username 12.er-3x.5g-3v and it worked.. Retrieved corresponding info from mysql.

 

So here's my new my_admin.php, thanks of course to jazzman1 !!

<?php 


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

Not sure why you think php isn't suited. It works.

 

 

 

To complete this story I would still like to know what it would take for requinix method to work.

 

 

Thanks Tom

Edited by liberate
Link to comment
Share on other sites

You don't have to prefix the URL by http://.

 

Here is it:

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

Also, when a new user try to make a registration, make sure (validate) what characters are acceptable to create this sub-domain.  

Link to comment
Share on other sites

To complete this story I would still like to know what it would take for requinix method to work.

This:

<?php 

$host = $_SERVER["HTTP_HOST"]; 
if (strncmp($host, "www.", 4) == 0) {
    $host = substr($host, 4);
}
$baseDomain = '.mysite1234.com';
$baseLen = strlen($baseDomain);
if (substr_compare($host, $baseDomain, -$baseLen) == 0) {
    $host = substr($host, 0, -$baseLen);
}
An alternative non-regex solution, going back to explode, would be:

$host = explode('.', $_SERVER["HTTP_HOST"]);
if ($host[0] == 'www') array_shift($host);
//Remove the base domain.  This assumes your base domain contains only one dot (example.com) rather than two (example.co.uk)
$host = array_slice($host, 0, -2);
$username = implode('.', $host);
Link to comment
Share on other sites

Hi kicken

 

re: $host = explode('.', $_SERVER["HTTP_HOST"]);

 

And that works when usernames contain dot (.)?  My original my_admin.php failed when usernames contained a dot. That is what started this exercise.

 

Tom

Edited by liberate
Link to comment
Share on other sites

And that works when usernames contain dot (.)?  My original my_admin.php failed when usernames contained a dot. That is what started this exercise.

Yes. Try it. The problem with your original was that it assumed the username was in the first/second element. This version makes no such assumption.

Link to comment
Share on other sites

Hi Kicken

 

Your right it works.. This was the tweak to my original code that I was looking for.

$host = explode('.', $_SERVER["HTTP_HOST"]);
if ($host[0] == 'www') array_shift($host);
//Remove the base domain.  This assumes your base domain contains only one dot (example.com) rather than two (example.co.uk)
$host = array_slice($host, 0, -2);
$username = implode('.', $host);

Just tried your code below 

<?php 

$host = $_SERVER["HTTP_HOST"]; 
if (strncmp($host, "www.", 4) == 0) {
    $host = substr($host, 4);
}
$baseDomain = '.mysite1234.com';
$baseLen = strlen($baseDomain);
if (substr_compare($host, $baseDomain, -$baseLen) == 0) {
    $host = substr($host, 0, -$baseLen);
}

$username = $host // added this but it isn't right (500 internal server error)

Can't figure out what to make $username=

 

Thanks Tom

Link to comment
Share on other sites

Hi  requinix

 

I just tried:

<?php 


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

echo $host;

echo returned:

the username 12.er-3x.5g-3v  correctly,  so at this stage working.

 

now make it

$username = $host;

and working !!!

 

Thanks requinix  That's 3 working solutions to one problem

Link to comment
Share on other sites

var_dump(str_replace(array('www.', '.mysite.com'), '', ('www.john.smith.mysite.com'))); // => "john.smith"
 
var_dump(str_replace(array('www.', '.mysite.com'), '', ('john.smith.mysite.com'))); // => "john.smith"

Link to comment
Share on other sites

Thanks to requinix  jazzman1  and kicken

 

I now have 3 solutions to the problem of how to retrieve the value of a subdomain ie. username, when that username can have dot or dash punctuation.


jazzman1:

 

<?php // get host name from URL $host = $_SERVER['HTTP_HOST']; preg_match('/^((\bhttps?:\/\/(www.)?|\bwww\.)?([a-z.-\d]+)?\.)?([a-z\d]+\.[a-z]{2,4}\/?)$/i', $host, $matches); $username = $matches[4];


kicken:

 

<?php $host = explode('.', $_SERVER["HTTP_HOST"]); if ($host[0] == 'www') array_shift($host); //Remove the base domain. This assumes your base domain contains only one dot (example.com) rather than two (example.co.uk) $host = array_slice($host, 0, -2); $username = implode('.', $host);


requinix:

 

<?php $host = $_SERVER["HTTP_HOST"]; if (strncmp($host, "www.", 4) == 0) { $host = substr($host, 4); } if (substr_compare($host, ".mysite1234.com", -15 /* -strlen(".mysite1234.com") */) == 0) { $host = substr($host, 0, -15); } $username = $host;

 

 

All as far as I can tell they all generating the same results.

 

So if I wanted to really "stir the pot", I would ask which one is better?

 

Thnaks again

Tom

 

Background info for anyone just landing here:

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. It is a multi-user self-reolicated site. usernames are alpha-numeric with dot and dash allowed. Personal web addresses are in http://username,mydomain.com format, and the backbone of the site is an autoresponder program.

Edited by liberate
Link to comment
Share on other sites

How the he** did the code jump out of their code wrapper (for lack of a better term) as soon as I went to post this. It all looked fine until I hit post. Frustrating or what? and edit didn't fix it they jumped out again.

Edited by liberate
Link to comment
Share on other sites

Hi Andy-H

 

Tried to build on what you wrote. Here is what I used.

<?php 

$host = $_SERVER["HTTP_HOST"]; 
var_dump(str_replace(array('www.', '.mysite1234.com'), '', ($host)));

$username = $host;

returned:

string(14) "12.er-3x.5g-3v"

 

So it correctly found the crazy username I am using as a test. But I assume a little more code is needed.

 

Tom

Link to comment
Share on other sites

Nope, that's all you need, it will work for anything:

 

 var_dump(str_replace(array('www.', '.mysite.com'), '', ('www.r34l-crazy.u53rn^m3$./john.smith?/i.mysite.com'))); // => r34l-crazy.u53rn^m3$./john.smith?/i
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.