Web Hosting Talk







View Full Version : grabbing part of a domain from a wildcard


splatcatballa99
12-23-2007, 01:53 PM
Ok I have a wildcard setup on my domain. Now I'm going to assign users subdomains (in actuallity there not) but I want to know how to grab the username out of the domain. Like so

username.domain.com

How would I grab that with PHP?

Renard Fin
12-23-2007, 03:47 PM
$_SERVER['HTTP_HOST'];

normally, no ?

azizny
12-23-2007, 06:05 PM
echo current(explode('.','username.domain.com'));


Peace,

splatcatballa99
12-26-2007, 12:26 AM
I'm talking about grabbing the

USERNAME part of username.domain.com

abluegrape
12-26-2007, 07:14 AM
How about something like:


<?php
$domain = "username.domain.com";
($user, $dom, $ext) = split('.', $domain);
echo "Username: $user";
?>


Thats off the top of me head - should work.

Ry

splatcatballa99
12-26-2007, 01:48 PM
This is the current URL not just some domain i'm supplied with. I need it to grab the domain itself.

jmichalicek
12-26-2007, 03:24 PM
This is the current URL not just some domain i'm supplied with. I need it to grab the domain itself.

You've been given a solution to grab the full domain/hostname already, right?
$_SERVER['HTTP_HOST'];

And you've been given a solution to split that string so that it gets just the bit that you want, right?

$domain = "username.domain.com";
($user, $dom, $ext) = split('.', $domain);
echo "Username: $user";

So combine those two concepts/techniques together and you've got your solution.

splatcatballa99
12-27-2007, 02:00 PM
How about something like:


<?php
$domain = "username.domain.com";
($user, $dom, $ext) = split('.', $domain);
echo "Username: $user";
?>
Thats off the top of me head - should work.

Ry
That way errors out but If I do this

$domain = $_SERVER['HTTP_HOST'];
list($user, $domain, $tld) = split('.', $domain);
echo "Username: $user";
it works. But the only problem is I don't think you can use . i nthe split function because it's supposed to use regexp. I don't know how to match it to a period.

jmichalicek
12-27-2007, 02:18 PM
Backslash should escape special characters like '.' in regular expressions. If it's working, though, then I would think that it understands the way it is written just fine. If you're unsure, the best way to find out is to go have a look at the php documentation on split() and see what it says.

splatcatballa99
12-27-2007, 02:23 PM
Backslash should escape special characters like '.' in regular expressions. If it's working, though, then I would think that it understands the way it is written just fine. If you're unsure, the best way to find out is to go have a look at the php documentation on split() and see what it says.


It's not working, and i've been looking at the documentation thats how I know it needs regexp.

jmichalicek
12-27-2007, 02:36 PM
It's not working, and i've been looking at the documentation thats how I know it needs regexp.
I'm not a php developer, so I'm basing this guess on examples I've seen.

Try changing split('\.',$_SERVER["HTTP_HOST") to split("\.",$_SERVER["HTTP_HOST").

If that doesn't do the trick, try this:
explode(".",$_SERVER["HTTP_HOST"]) and see if that helps.

splatcatballa99
12-27-2007, 02:49 PM
I'm not a php developer, so I'm basing this guess on examples I've seen.

Try changing split('\.',$_SERVER["HTTP_HOST") to split("\.",$_SERVER["HTTP_HOST").

If that doesn't do the trick, try this:
explode(".",$_SERVER["HTTP_HOST"]) and see if that helps.

thank you the \. worked.