Heim  >  Artikel  >  php教程  >  玩转虚拟域名◎+

玩转虚拟域名◎+

WBOY
WBOYOriginal
2016-06-13 11:20:101006Durchsuche

不知道大家最近上网是否发现一个新现象,就是有一些网站开始提供“username@server”的虚拟域名服务。由于“@”的魅力,大家纷纷申请,你或许会想:“如果我也能提供这种服务,该多好阿:)人气肯定不错!”本文将给大家揭开“@”的“神秘”面纱,让大家都可以来“@”!(Do u @ today?)
别急,这个并不是电子邮件的地址,是一种虚拟域名,不相信的话可以在浏览器中访问“bbs@zphp.com”。部分朋友应该使用过IE的FTP功能,就是在浏览器的地址栏中键入“password:username@server”IE就会自动登陆FTP服务器;而在Http1.1协议中,就规定了Http访问授权功能,形式同样为“password:username@server”,其中“password:”可以省略,也是是访问“bbs@zphp.com”实际上是以bbs的身份访问“zphp.com”这个服务器。
那么我们只是需要将具体的URI传送给PHP程序,在数据库中搜索出真实的URL重定向就可以了。
首先我们需要制作一个传送URI的页面(作为服务器的默认文档,一般命名为index.htm);在JS的Window对象中就可以实现这项功能,下面是index.htm的源代码:
<script> <br />this.location = &lsquo;gotourl.php?url=&rsquo; + this.location.href; <br /></script>
上面的代码会将浏览器重定向到gotourl.php,并且通过QueryString给变量$url赋值为当前的URI。
成功将URI传递给PHP程序后,就可以进入数据库查找真实URL,下面是SQL数据库相对应的table 的结构:
CREATE TABLE domain(
Id int(3) UNSIGNED DEFAULT ‘0’ NOT NULL, # 域名ID
Domain char(20) NOT NULL, # 域名
Gotourl char(255) NOT NULL, # 真实的URL
);
建立好了Table,就可以开始编写gotourl.php了,程序分为三个部分:
1、 分析URL:
$url = preg_replace(“/^http:///I”, “”, $url); // 将URL前面的“http://”去掉,不区分大小写
$url = preg_replace(“/@.+$/”, “”, $url); // 将“@”后面的部分去除
那么,剩下的URL就只含有“username”的部分了。
为了给数据库应用,需要对铭感的字符进行处理:
$url = addslashes($url);
2、 搜索真实的URL:

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn