PHP program: DIY virtual Domain Name Service

Source: Internet
Author: User
Tags net domain subdomain name

Introduction: This is Php.Program: Virtual Domain Name Service DIY detailed page, describes and PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 324802 'rolling = 'no'>

Editor's note: friends who have used Netease's my.yeah.net domain name to switch to the service must be interested in its powerful functions? Once you have applied for your own virtual domain name, such as coolfish.yeah.net, visitors can use this virtual domain name to access your website accurately no matter how your real website changes. This is really convenient. In fact, virtual domain names are not complex. If you have a little knowledge about the basics of network programming and read this article, you can also implement virtual domain name services on your own and enjoy ISP addiction.

With the rapid development of the Internet, various network services are emerging. The virtual domain name service we are exploring today is one of the many free lunch dishes.

What is a virtual domain name?

The virtual Domain Name Service is spread along with the emergence of free personal homepage space. The personal homepage service in China started in early 1996, followed by many large-scale personal homepage space services such as Zhanjiang "bihai yinsha" and Netease personal homepage base, this has paved the way for the popularization and development of the Internet in China. However /~ Myname and other style URLs are complex in form and hard to remember. Therefore, converting this complicated address into a simple form of virtual domain name service such as myname.yeah.net came into being.

We know that common domain names are. com ,. net and so on, similar to 5945.net domain names, we are called top-level domain names, but also pay-as-you-go domain names, and such as abc.5945.net domain names, we are often called third-level domain names, it can be defined by the user and does not need to be paid. The so-called virtual domain name is usually made on a third-level domain name or multi-level domain nameArticleThe task of the virtual Domain Name Service is to redirect a simple and easy-to-remember third-level or multi-level domain name to a relatively complex and difficult-to-remember URL address. The significance of this service is that.

What is a wildcard domain name?

Xiami familiar with NT knows that if you want to set up a third-level and multi-level domain name on NT, you must first set up a DNS server so that the domain name such as abc.5945.net can be resolved to the Host IP address we specified, then we need to set the IIS (Internet Information Server) of the host so that the third-level domain name can access the corresponding URL address. It can be seen that this process is not complex. However, it is obviously impossible to have the corresponding configuration permissions for each host. At the same time, when a large number of intensive services are provided, the setup process is slow and cumbersome. So how is the real virtual Domain Name Service implemented? The answer is simple: Use a wildcard domain name.

A wildcard domain name is similar to a wildcard in DOS. For example, if you create a wildcard domain name * .5945.com in DNS, all domain names, such as www.5945.net, abc.5945.net, and ftp.abc.5945.net, do not need to be set separately in the DNS and "automatically take effect ", during DNS resolution, they all point to the same IP address specified when the wildcard domain name is created. In this way, we only need to apply for a wildcard domain name resolution service for our primary domain name. The domain name resolution problem is solved and resolved to the same IP address, all we need to do is to write an article on the default web page of the host corresponding to this IP address, so that different subdomains can redirect URLs to the desired URLs. It should be noted that each host that provides Web Services has a default web service. If a domain name is resolved to this host, when the corresponding document directory is not set in the web server software, the request for this URL is submitted to the default web service. This is the basic process of our virtual Domain Name Service, one host can only provide one virtual host service.

Routine parsing:

Let's take the PHP program as an example to introduce this process.

Suppose we need to provide the virtual Domain Name Service with the domain name 5945.net, and the default Web Service document for the 5945.net host is index. PHP (the default document page name can be set by Web Server), the page program should be responsible for redirecting abc.5945.net to my free space, such as: www.netease.com /~ ABC/default.htm. Therefore, our program first needs to analyze what the third-level domain name is accessed by the user.
Index. php:

$ Domain = $ SERVER_NAME; // obtain the full name of the domain name accessed by the user

$ Domain = eregi_replace ('.5945.net', ', $ domain); // capture the full name of the domain name, obtain the third-level domain name to be accessed by the user, and obtain the full name of the accessed domain name, you can directly use the PHP system variable $ SERVER_NAME or $ http_host. Note that the internal variables of the system must be referenced in uppercase. In fact, the PHP system has thoroughly analyzed the access request header information sent by the user's browser and divided it into and saved into corresponding system variables for convenient use by the program. In the second statement, replace the regular expression with ".5945.net" and replace it with an empty character. In this way, the third-level domain name is obtained.

Generally, a large virtual Domain Name Service puts the user's subdomain name and its corresponding URL in the database, so that the user can register a new third-level domain name at any time and change the corresponding URL of the domain name, the following work is to retrieve the URL corresponding to the third-level domain name from the database (the database retrieval procedure is omitted ).

... // Retrieve the database based on the third-level domain name and get the variable $ URL, which stores the URL address corresponding to the domain name

Header ("Location: $ URL"); // system redirection to redirect the user's browser to the corresponding URL

After obtaining the corresponding URL address, we can use the PHP header function to send a redirection message to the user's browser for the user to transfer to the corresponding URL address, this complete virtual domain name service process is over. Here we need to pay attention to the header function before, can not have any output to the browser, otherwise it will produce an error, in addition to the redirection operation in the URL address should be expressed completely, such as: http://www.netease.com /~ ABC /~ Index.htm.

Pop-up of the advertisement window:

Careful readers certainly find that common virtual domain name services usually have pop-up Advertisement Windows. How can this problem be achieved? To achieve unified control, the advertisement box must be displayed before the user's browser is redirected, so the redirection operation cannot simply use the header function, because this function requires no output to the browser before use. To implement it, we must change the method:

... // Retrieve the database based on the third-level domain name and get the variable $ URL, which stores the URL address corresponding to the domain name
Echo '<script language = "JavaScript"> window. Open ("adv.htm"); </SCRIPT> ';
// Output a piece of JavaScriptCodeTo display the ad page adv.htm.
Echo '<meta http-equiv = "refresh" content = "0; url ='. $ URL. '"> ';
// Use the meta Statement of HTML to implement system redirection and redirect the user's browser to the corresponding URL

Now, the secret of the virtual Domain Name Service has been fully presented to us. Finally, I would like to ask a smart reader again, the program snippet here assumes that the user only enters a virtual domain name in the form of abc.5945.net. What should we do in the form of abc.5945.net/test.htm? <

More articles about "php program: DIY virtual Domain Name Service"

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/324802.html pageno: 14

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.