Here is the way which i have used in my project for multiple subdomains;
In index.php
file extract the subdomain name (if exists any) and set it into a constant variable. This will be used later.
function extract_domain($domain) {
if(preg_match("/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i", $domain, $matches)) {
return $matches['domain'];
} else {
return $domain;
}
}
function extract_subdomains($domain) {
$subdomains = $domain;
$domain = extract_domain($subdomains);
$subdomains = str_replace('www', '', rtrim(strstr($subdomains, $domain, true), '.'));
return $subdomains;
}
$subDomainName = extract_subdomains($_SERVER['HTTP_HOST']);
//this const will be used in bootstrap file.
define('SUBDOMAIN_NAME', $subDomainName);
After login
save the name of the sub domain in a session variable on which you want to redirect a user. You can write below function in the bootstap
file of user
module:
protected function _initSubdomainCheck() {
//Get session in your own way for below line. I'm using action helper to manage the session.
$userSession = Zend_Controller_Action_HelperBroker::getStaticHelper('siteSession')->getUserSession();
$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
//check if user is logged in then redirect user to a subdomain
if(isset ( $userSession->logged_in ) && $userSession->logged_in == 1) {
//SUBDOMAIN_NAME is a const variable i have set in index.php. If user is already redirected to the subdomain which is stored in session then no need to have further redirection
if($userSession->urlSubDomainName != SUBDOMAIN_NAME) {
$redirector->gotoUrl(Zend_Controller_Action_HelperBroker::getStaticHelper('common')->getAfterLoginUrl());
}
}
}
Logic behind above function:
- Get the session variable
- Check whether user is logged in or not
- If user is logged in then check if user is already on a subdomain URL, if no then redirect user to a specific sub domain.
I have created an action helper common
in /application/helpers/Common.php
with below code:
Method of this action helper can be called in this way: Zend_Controller_Action_HelperBroker::getStaticHelper('common')->methodName();
class Zend_Controller_Action_Helper_Common extends Zend_Controller_Action_Helper_Abstract{
public function getSiteUrl(){
$baseUrl = Zend_Controller_Front::getInstance()->getRequest()->getBaseUrl();
return $this->getServerProtocol().$_SERVER['HTTP_HOST'] . $baseUrl;
}
public function getServerProtocol() {
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
return $protocol;
}
function extract_domain($domain) {
if(preg_match("/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i", $domain, $matches)) {
return $matches['domain'];
} else {
return $domain;
}
}
public function getSubdomainName() {
//get session object in your way.
$session = Zend_Controller_Action_HelperBroker::getStaticHelper('siteSession')->getUserSession();
$domainName = extract_domain($this->getSiteUrl());
return $this->getServerProtocol()
.$session->urlSubDomainName.'.'
.$domainName;
}
public function getAfterLoginUrl(){
return $this->getSubdomainName().'/yourController/Youraction';
}
}
Logic for above helper:
- We have written this helper to get the subdomain url. First we are getting the subdomain name from the
session
e.g. abc
- After that extract the domain name. For e.g. if you have
www.example.com
then get example.com
out of it.
- Finally create the subdomain url
Protocol + subdomain name + domain name
. E.g. http://abc.example.com
- return this URL.
Hope it helps.