php - Better way to check if subdomain exists without using database query or hosts file -


i'm using symfony2 , in application student accommodation, i'm creating dynamic subdomains each university, i'm configuring virtual host wildcard subdomain entry subdomain valid.

how check if subdomain registered , belongs university, , how differentiate randomly typed non user registered subdomain efficiently?

if go database query every random access curious users result in lot of db queries, , use of hosts file slow (not best practice)

please suggest efficient way using php or symfony or other techniques guys know of

(additional info) there 'free trial' option result in lot of subdomains, , every 1 start free trial, example of i'm trying achieve woudld studystays

-thanks

you cache each of subdomain requests (using doctrine cache wrapper whatever caching system use) each subsequent check need check cache rather database.

also when adding/removing/updating subdomain object update cache value keep date.

app/config/config.yml

set provider doctrine cache bundle, more info see the docs (you need add doctrine cache bundle vendors , appkernel, obviously).

doctrine_cache:     providers:         acme_subdomain:             type: filesystem # apc, array, redis, etc             namespace: acme_subdomain 

acme\yourbundle\registry\subdomainregistry

create registry can check subdomain state , update cache when required. example stores state string rather boolean (as far know) "not found" key return false rather null.

use doctrine\common\cache\cache; use doctrine\common\persistence\objectmanager;  class subdomainregistry implements subdomainregistry {     const registered   = 'registered';     const unregistered = 'unregistered';      /**      * @param objectmanager      */     protected $manager;      /**      * @param cache      */     protected $cache;      public function __construct(objectmanager $manager, cache $cache)     {         $this->manager = $manager;         $this->cache = $cache;     }      /**      * @param string $subdomain      * @return boolean      */     public function issubdomainregistered($subdomain)     {         // if subdomain not in cache update cache         if (!$this->cache->has($subdomain)) {             $this->updateregistry($subdomain);         }          return self::registered === $this->cache->get($subdomain);     }      /**      * @param string $subdomain      * @return boolean      */     public function registersubdomain($subdomain)     {         $this->cache->set($subdomain, self::registered);          return $this;     }      /**      * @param string $subdomain      * @return boolean      */     public function unregistersubdomain($subdomain)     {         $this->cache->set($subdomain, self::unregistered);          return $this;     }      /**      * @param string $subdomain      * @return null      */     private function updateregistry($subdomain)     {         $object = $this->manager->findoneby(array('subdomain' => $subdomain);          // $object->isactive() assume storing subdomains after cancelling         // , setting inactive. put own real logic here         if (null === $object || !$object->isactive()) {             $this->unregistersubdomain($subdomain);              return null;         }          $this->registersubdomain($subdomain);     } 

then when registering or unregistering subdomain add call registry in method.

for example...

$subdomain = new subdomain(); // subdomain property of subdomain seems weird me // can't think of better i'll go $subdomain->setsubdomain('acme'); // .. subdomain details $this->manager->persist($subdomain); $this->manager->flush(); $this->registry->registersubdomain($subdomain->getsubdomain()); 

Comments