search
HomeBackend DevelopmentPHP Tutorial如何开发一个虚拟域名系统_php基础

大家在使用诸如yourname.yeah.net这样的简记域名时都感到十分方便,有很多人在想:我要是能让自己的服务器也能够实现简记域名就好了。其实这并不复杂。看完了本文,你也可以做一个简记域名系统。  

  简记域名系统的关键技术在于:实现Web页面的重定向(Redirctory)。在本质上,简记域名系统和虚拟机系统完全不同。虚拟机的虚拟域名和IP是存在一一对应关系的。而简记域名系统不需要将域名和IP做一一映射。也就是说,它根本不需要复杂的域名解析机制和虚拟机来完成,它所做的事情就是当你在请求yourname.somedomain时,将你的浏览器重新定向到你本来存放Html页面的地方。  

  为了说明的更完善,下面图例:  

  我提供的源程序是运行环境是:RedHat 5.1 Linux下的Apache1.3.6 Web服务器+PHP3语言。 在编写程序之前,我们首先要设置好我们的服务器。首先要让Apache服务器支持php3。到ftp.redhat.com下载mod_php-2.0.1-9.i386.rpm,安装后,修改/etc/httpd/conf/http.conf文件,去掉#LoadModule php3_module一句前面的#注释号,同样在/etc/httpd/conf/srm.conf文件里去掉#AddType application/x-httpd-php3 .php3前面的注释号,同时在DirectoryIndex一项后添加index.php3。重新启动Apache Server,此时服务器就支持标准的php3语言脚本文件了并能将index.php3作为默认的首页。  
  设置DNS服务器,使其能对泛域名解析。一般的Unix和Linux系统的DNS解析都是由Bind守护程序完成的,Bind4和Bind8的配置文件分别/etc/named.boot和name.conf,配置时根据你的系统修改。设置Bind的配置文件/etc/named.boot,在其中加入“primary domain.com db.domain”一句,添加一个新的域记录。在/etc/name.conf中加入:  

  zone "domain.com" {  
  type master;  
  file "db.domain”;  
  };  
  在/var/name/中新建主域记录文件db.domain,其格式为:  
  N SOA dns.domain.com root.domain.com (  
  199811291 ;Serial  
  28800 ;refresh  
  7200 ;retry  
  604800 ;expire  
  86400) ;minimum  
  dns  
  MX 10 dns.domain.com.  
  dns A 202.115.135.50  
  www A 202.115.135.50  
  * A 202.115.135.50  
  关键是最后一句,即将整个域可能出现未做标记的所有Hostname全部指向同一IP。 执行/usr/sbin/ndc reload,重新加载域名数据库。测试一下,此时应该随便ping一个domain域内的主机(除已经标记的),都指向了指定的IP,那么DNS服务器设置完成。  


  最后一步是编制PHP3脚本。我们刚才已经在图中详细的说明了整个的原理,所以写一个重新定向的程序就不是很难了。  


  让我们来看一个由IE5.0送出的完整HTTP头信息:  


  Accept: application/vnd.ms-excel, application/msword, application/vnd.ms-powerpoint,      image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*  
  Accept-Encoding: gzip, deflate  
  Accept-Language: zh-cn  
  Connection: Keep-Alive  
  Host:ww.yahoo.com  
  User-Agent: Mozilla/4.0 (compatible; MSIE 5.0b1; Windows 98)  
  我们需要在整个HTTP头信息中取出Host信息,然后将http://www.jj.jx.cn/www.xxx.xxx/default.htm形式的第一部分“www”,即HostName(也即是用户注册的name)单独取出,作为重定向检索的关键字。  


  检索到用户注册的URL信息后,我们给用户浏览器送一个重定向命令“Localtion: http://www.jj.jx.cn/somewhere/sample.html”,将用户重定向到指定页面。  


  在PHP3中,有函数GetAllHeader(),取得浏览器送出的HTTP头信息。我们主要需要使用此函数来完成整个程序。  


  后面附有源程序,由于只是实验性质的,所以在查询用户信息时,没有使用数据库,如果整个系统要实际应用的话,一定要和数据库挂接起来,不然查询用户信息的过程将是十分漫长,大大影响效率,而且用户数据的管理也不方便。(由于篇幅限制,没有给出注册和管理所需的写记录程序,请自行添加)  


  在源程序中,所有用户信息记录在data子目录下user.dat文件中。其格式为:  


  username:  


  http://octopus.cdit.edu.cn/~qap213/index.html  


  附PHP3源程序:  


    


  //Get HTTP's Header and parse it//  


  $headers = getallheaders();  


  while (list($header, $value) = each($headers)) {  


  if($header=="Host"){$username= strtok($value,".");}}  


  //Jump out the Banner's Window//  


  echo '';  


   

  // seek the user information from the recorded file//  


  if(!$usrinfo=file("data/user.dat")){echo "Open Data File Error!!";}  


  $url="http://";  


  for($i=0;$i

  if(strtok($usrinfo[$i],":")==$username){  


  $url=$usrinfo[$i+1];  


  }  


  if($url=="http://"){echo "not found the uesrname of Data!";}  


  else{  


  echo '';}  


  ?>  

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment