Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Breaking my URL apart - Extracting server ALIAS from URL address 1

Status
Not open for further replies.

southbeach

Programmer
Jan 22, 2008
879
US
Hello!

Given URL

Code:
alias.mydomain.com

How do I extract "alias"? Now mind you, "alias" can be anything under the sun. I know the structure since it has to be FQDN but I do not know what the string is (could be: appl.mydomain.co, orange.mydomain.com, grapes.mydomain.com ...).

Also, if the URL address is
Code:
alias.mydomain.com/SOME-DIRECTORY/

how can I extract "SOME-DIRECTORY" given that this is also dynamic as the "ALIAS"?

I know and do not know enough
Code:
list($var,$var,$var...)=explode(".", [NOT SURE WHAT TO USE HERE]);


I want to set $_SESSION['ALIAS'] where 'ALIAS' of the above URL is stored
I want to set $_SESSION['HOST'] where 'MYDOMAINNAME.COM' of the above URL is stored
I want to set $_SESSION['SUBDIR'] where 'SOME-DIRECTORY' of the above URL is stored



Thank you all for your help!




--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Code:
$url = 'alias.mydomain.com/SOME-DIRECTORY/';
$bits = parse_url($url);
print_r($bits);

$bits will contain the whole host and path data.

to be more granular, you can get what you are calling the alias from the host string like this

Code:
list($alias, $host, $tld) = explode('.', $bits['host']);
$host = $host.'.'.$tld;

to get the directory data from the path is slightly more complex
Code:
$pathbits = pathinfo($bits['path']);
$directory = $pathbits['dirnmae'];

putting it all together

Code:
function assignSessionVars( $url ){
  if(session_id() == '') session_start();
  $bits = parse_url($url);
  list($_SESSION['ALIAS'], $host, $tld) = explode('.', $bits['host']);
  $_SESSION['HOST'] = $host.'.'.$tld;
  $pathbits = pathinfo($bits['path']);
  $_SESSION['SUBDIR'] = $pathbits['dirnmae'];
}
 
Just thought I'd put this out as a warning.

Watch out for any domains that may have more parts like:
Or someone might even have their own multi-level alias: alias1.subalias.domain.com.

If you _know_ that you will only ever have to deal with the three pattern that jpadie shows - then jpadie is correct.
 
type
Code:
$_SESSION['SUBDIR'] = $pathbits[[COLOR=#A40000]'dirname[/color]'];
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top