php 域名转发程序
php 域名转发程序是我从其它地方看到了,通过xml与php交换实现的,下面我们来看看源文件吧。
<xml version = "1.0" encoding = "utf-8" ?> <urls> <num>1</num> <url id="1"> <domain>test.domain.com</domain> <to>http://www.phprm.com/</to> </url> </urls>
index.php文件。
<?php
/*
域名伪跳转方案
By IVershuo.cn
*/
header("content-type:text/html; charset=utf-8");
$xml = simplexml_load_file("url.xml"); //xml文件路径
$to = 'http://www.phprm.com'; //默认跳转地址
foreach ($xml as $key => $value) {
if ($value->domain == $_SERVER['SERVER_NAME']) {
$to = $value->to;
break;
}
}
header('Location: ' . $to);
?>处理文件
<?php
/*
域名伪跳转方案,域名跳转配置脚本
###注意:没设置验证可配置,使用时请修改该文件名和xml文件名!!!
*/
$xmlFile = 'url.xml'; //xml文件的路径
$xml = simplexml_load_file($xmlFile);
$num = $xml->num[0];
if (@$_POST['domain'] && $_POST['to']) {
$xmlUrl = $xml->addChild('url');
$xmlUrl->addAttribute('id', $num+= 1);
$xmlUrl->addChild('domain', $_POST['domain']);
$xmlUrl->addChild('to', $_POST['to']);
$xml->num[0] = $num;
}
if (@$_GET['del']) {
removeNode($xml, "//url[@id='" . $_GET['del'] . "']", 'all');
$xml->num[0] = $num;
}
$sp = fopen($xmlFile, "wb");
fwrite($sp, $xml->asXML());
fclose($sp);
function removeNode($xml, $path, $multi = 'one') {
$result = $xml->xpath($path);
// for wrong $path
if (!isset($result[0])) return false;
switch ($multi) {
case 'all':
$errlevel = error_reporting(E_ALL & ~E_WARNING);
foreach ($result as $r) unset($r[0]);
error_reporting($errlevel);
return true;
case 'child':
unset($result[0][0]);
return true;
case 'one':
if (count($result[0]->children()) == 0 && count($result) == 1) {
unset($result[0][0]);
return true;
}
default:
return false;
}
}
echo '<?xml version="1.0" encoding="utf-8"?>' . "n";
?><!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>域名添加</title>
<meta name="author" content="阿肆" />
<style type="text/css">
label {display:block;margin:10px 0;}
input {width:300px;}
label span {color:#666;}
</style>
<script type="text/javascript" src=""></script>
</head>
<body>
<h1>域名转向添加</h1>
<div>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<fieldset>
<legend>域名添加</legend>
<label for="domain">
域名:
http://<input type="text" name="domain" id="domain" />
<span>不用加"http://"</span>
</label>
<label for="to">
转向:
<input type="text" name="to" id="to" value="http://" />
<span>前面添加"http://"或"https://"等</span>
</label>
<button type="submit">提交</button>
</fieldset>
</form>
</div>
<div>
<ul id="listed">
<?php
$i = 0;
foreach ($xml as $key => $value) {
if ($key == 'url') {
echo '<li><em>http://' . $value->domain . '</em>跳转到<a href="' . $value->to . '">' . $value->to . '</a> <a href="' . $_SERVER['PHP_SELF'] . '?del=' . $xml->url[$i]['id'] . '">>删除<</a></li>';
$i++;
}
}
?>
<script type="text/javascript">
var li = document.getElementById('listed').getElementsByTagName('li');
for (var i = 0, l = li.length; i < l; i++) {
li[i].getElementsByTagName('a')[1].onclick = function () {
var u = this.parentNode.getElementsByTagName('em')[0].innerHTML;
if (confirm('确定要删除 ' + u + ' 的转向?')) {
return true;
} else {
return false;
}
}
}
</script>
</ul>
</div>
</body>
</html>文章链接:http://www.phprm.com/code/b7f65d9656470328cafc3c3144d74d95.html
随便收藏,请保留本文地址!