Develop a "virtual domain name" system using PHP programming

Source: Internet
Author: User
Tags strtok name database

If your server can also implement a simple domain name. In fact, this is not complicated. You can also create a simple domain name system.

The key technology of the simplified Domain Name System is to achieve Redirctory on the web page ). In essence, the domain name system is completely different from the virtual machine system. There is a one-to-one correspondence between virtual domain names and IP addresses of virtual machines. The simplified domain name system does not need to map domain names and IP addresses one by one. That is to say, it does not need complicated domain name resolution mechanisms or virtual machines. What it does is when you request yourname. in somedomain, redirect your browser to the place where you originally stored the Html page.

The running environment is Apache1.3.6 Web SERVER + PHP3 in RedHat 5.1 Linux. Before writing a program, we must first set up our server. First, let the Apache server support php3. Go to ftp.redhat.com to download the mod_php-2.0.1-9.i386.rpm. After installing, modify/etc/httpd/conf/http. in the conf file, remove the # comment # In Front Of The # LoadModule php3_module statement, also in/etc/httpd/conf/srm. remove # AddType application/x-httpd-php3 In the conf file. comment number before php3, and add index after DirectoryIndex. php3. Restart Apache Server. Now the Server supports standard php3 script files and uses index. php3 as the default homepage.

Set the DNS server so that it can resolve wildcard domain names. Generally, DNS resolution for Unix and Linux systems is completed by the Bind daemon. The Bind4 and Bind8 configuration files are/etc/named respectively. boot and name. conf, which is modified according to your system. Set the Bind configuration file/etc/named. boot, add "primary domain.com db. domain" to add a new domain record. Add the following to/etc/name. conf:

Zone "domain.com "{

Type master;

File "db. domain ";};

Create the master domain record file db. domain in/var/name/in the format:

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

The key is the last sentence, that is, all hostnames not marked in the entire domain may point to the same IP address. Run/usr/sbin/ndc reload to reload the domain name database. In this test, you should ping a host in the domain (except the host that has been marked) and point to the specified IP address. Then, the DNS server settings are complete.

The last step is to compile the PHP3 script. We have already explained the entire principle in detail in the figure, so it is not difficult to write a retargeting program.

Let's take a look at the complete HTTP header information sent by IE5.0:

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)

We need to retrieve the Host information from the entire HTTP header, and then retrieve the first packet in the form of http://www.jj.jx.cn/www.xxx.xxx/default.htm, that is, the hostname (also the name registered by the user) separately as the keyword for redirect retrieval.

After retrieving the URL information registered by the user, we will send a redirection command "Localtion: http://www.jj.jx.cn/somewhere/sample.html#to redirect the user to the specified page.

In PHP3, The GetAllHeader () function is provided to obtain the HTTP header information sent by the browser. We mainly need to use this function to complete the entire program.

The source program is appended. Because it is only experimental, the database is not used when querying user information. If the entire system needs to be applied, it must be linked to the database, otherwise, the process of querying user information will be very long, greatly affecting the efficiency, and the management of user data is not convenient.

In the source program, all user information is recorded in the user. dat file under the data subdirectory. The format is:

Username:

Http://octopus.cdit.edu.cn /~ Qap213/index.html

Appendix PHP3 source code:

<?

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

= Getallheaders ();
'Www .knowsky.com
While (list (,) = each ()){

If (= "Host") {= strtok (,".");}}

// Jump out the Banner's Window //

Echo '<script language = "JavaScript"> ',

'Msg = window. open ("banner.htm", "sample ",

"Toolbar = no", "directories = no", directories = no "," menubar = no ");',

'</Script> ';

// Seek the user information from the recorded file //

If (! = File ("data/user. dat") {echo "Open Data File Error !! ";}

= "Http ://";

For (= 0; <count (); ++)

If (strtok (, ":") = ){

= ;}

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

Else {

Echo '<meta http-equiv = "refresh" content = "0;', 'url = ','"> ';}

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.