发布时间:2013-09-19 11:33:46来源:阅读(1302)
昨晚写好的类,在使用起来时发现不怎么方便,而朋友也指出,不如使用$_SERVER['PATH_INFO']更方便,查看了一些关于$_SERVER['PATH_INFO']的资料,对模拟 URL Rewrite 重新进行了修改。
目的是一样的,但并没有使用类了,而是直接将获取到的信息写入 $_GET 和 $_SERVER["QUERY_STRING"] 预定义变量,跟昨天的做法一样,新增 $_SERVER["R_QUERY_STRING"] 来存储模拟后的 QueryString。
并增加了对URL中存在 / 符号的解决办法,自动添加花括号以表示区别,测试用$_GET取值和生成Rewrite都很正常。
下面是代码的内容:
测试1的URL地址为:http://localhost:808/test.php/a/123/b/456/d/789/{u/rl}/{http:/www.163.com/}.html ————————————————————- 测试2的URL地址为:http://localhost:808/test.php?a=123&b=456&c=&d=789&u/rl=http://www.163.com/ ————————————————————-
function BuildRewriteURL($string, $UrlStyle=0) {
if (strpos($string, '?')) {
$StrArray = explode('?', $string);
$Page = $StrArray[0];
$ParaString = $StrArray[1];
}
else $ParaString = $string;
$ParaString = str_replace('?', '', $ParaString);
if (!strpos($ParaString, '&')) return $this->ParaString;
$URLArray = explode('&', $ParaString);
for ($i=0; $i
if (!empty($Para[1])) {
if (!empty($UrlString)) $UrlString .= '/';
if (strpos($Para[0], '/')) $Para[0] = '{' . $Para[0] . '}';
if (strpos($Para[1], '/')) $Para[1] = '{' . $Para[1] . '}';
$UrlString .= $Para[0] . '/' . $Para[1];
}
}
switch ($UrlStyle) {
case 1: $AddString = '/'; break;
case 2: $AddString = ''; break;
default: $AddString = '.html'; break;
}
return str_replace('//', '/', $Page . '/' . $UrlString . $AddString);
}
$QueryString = !empty($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : str_replace($_SERVER["SCRIPT_NAME"], '', $_SERVER["REQUEST_URI"]);
$QueryString = str_replace(array('"', "'", '<', '>'), array('%22', '%27', '%3C', '%3E'), $QueryString);
if (strrpos($QueryString, "&") && strrpos($QueryString, "&")) { // 当URL为原始状态时
$ParaArray = explode('&', $_SERVER["QUERY_STRING"]);
for ($i=0; $i
$Para = explode('=', $ParaArray[$i]);
if (!empty($Para[1])) {
if (strpos($Para[0], '/')) $Para[0] = '{' . $Para[0] . '}';
if (strpos($Para[1], '/')) $Para[1] = '{' . $Para[1] . '}';
$RequestURL .= '/' . $Para[0] . '/' . $Para[1];
}
}
}
$_SERVER["R_QUERY_STRING"] = $RequestURL;
}
elseif (strrpos($QueryString, "/")) { // 当URL为模拟 URL Rewrite 时
$QueryString = str_replace('.html', '', $QueryString);
$_SERVER["R_QUERY_STRING"] = $QueryString;
preg_match_all ("/{(.*)}/U", $QueryString, $matches);
for ($i=0; $i
}
$ParaArray = explode('/', $QueryString);
for ($i=0; $i
if (!empty($RequestURL)) $RequestURL .= '&';
if (strpos($ParaArray[$i+1], '%2F')) $ParaArray[$i+1] = rawurldecode($ParaArray[$i+1]);
if (strpos($ParaArray[$i+2], '%2F')) $ParaArray[$i+2] = rawurldecode($ParaArray[$i+2]);
$StrArray[$ParaArray[$i+1]] = $ParaArray[$i+2];
$RequestURL .= $ParaArray[$i+1] . "=" . $ParaArray[$i+2];
}
}
$_GET = $StrArray;
$_SERVER['QUERY_STRING'] = $RequestURL;
}
unset($QueryString, $ParaArray, $Para, $StrArray, $RequestURL, $matches, $i);
echo '
';
echo '
$_SERVER["REQUEST_URI"] = ' . $_SERVER['REQUEST_URI'];
echo '
$_SERVER["QUERY_STRING"] = ' . $_SERVER['QUERY_STRING'];
echo '
$_SERVER["R_QUERY_STRING"] = ' . $_SERVER['R_QUERY_STRING'];
echo '
';
echo '$_GET[\'a\'] = ' . $_GET['a'] . '
';
echo '$_GET[\'b\'] = ' . $_GET['b'] . '
';
echo '$_GET[\'c\'] = ' . $_GET['c'] . '
';
echo '$_GET[\'d\'] = ' . $_GET['d'] . '
';
echo '$_GET[\'u/rl\'] = ' . $_GET['u/rl'] . '
';
echo '
';
echo 'BuildRewriteURL(\'/test.php?a=123&b=456&c=&d=789&u/rl=http://www.163.com/\') = ' . BuildRewriteURL('/test.php?a=123&b=456&c=&d=789&u/rl=http://www.163.com/') . '
';
echo 'BuildRewriteURL(\'/?a=123&b=456&c=&d=789&u/rl=http://www.163.com/\', \'1\') = ' . BuildRewriteURL('/?a=123&b=456&c=&d=789&u/rl=http://www.163.com/', '1') . '
';
echo 'BuildRewriteURL(\'a=123&b=456&c=&d=789&u/rl=http://www.163.com/\', \'2\') = ' . BuildRewriteURL('a=123&b=456&c=&d=789&u/rl=http://www.163.com/', '2') . '
';
?>
输出结果:
$_SERVER["REQUEST_URI"] = /test.php/a/123/b/456/d/789/{u/rl}/{http:/www.163.com/}.html
$_SERVER["QUERY_STRING"] = a=123&b=456&d=789&u/rl=http:/www.163.com/
$_SERVER["R_QUERY_STRING"] = /a/123/b/456/d/789/{u/rl}/{http:/www.163.com/}
————————————————————-
$_GET['a'] = 123
$_GET['b'] = 456
$_GET['c'] =
$_GET['d'] = 789
$_GET['u/rl'] = http:/www.163.com/
————————————————————-
BuildRewriteURL('/test.php?a=123&b=456&c=&d=789&u/rl=http://www.163.com/') = /test.php/a/123/b/456/d/789/{u/rl}/{http:/www.163.com/}.html
BuildRewriteURL('/?a=123&b=456&c=&d=789&u/rl=http://www.163.com/', '1') = /a/123/b/456/d/789/{u/rl}/{http:/www.163.com/}/
BuildRewriteURL('a=123&b=456&c=&d=789&u/rl=http://www.163.com/', '2') = /a/123/b/456/d/789/{u/rl}/{http:/www.163.com/}
输出结果:
$_SERVER["REQUEST_URI"] = /dcep2/test.php?a=123&b=456&c=&d=789&u/rl=http://www.163.com/
$_SERVER["QUERY_STRING"] = a=123&b=456&c=&d=789&u/rl=http://www.163.com/
$_SERVER["R_QUERY_STRING"] = /a/123/b/456/d/789/{u/rl}/{http://www.163.com/}
————————————————————-
$_GET['a'] = 123
$_GET['b'] = 456
$_GET['c'] =
$_GET['d'] = 789
$_GET['u/rl'] = http://www.163.com/
————————————————————-
BuildRewriteURL('/test.php?a=123&b=456&c=&d=789&u/rl=http://www.163.com/') = /test.php/a/123/b/456/d/789/{u/rl}/{http:/www.163.com/}.html
BuildRewriteURL('/?a=123&b=456&c=&d=789&u/rl=http://www.163.com/', '1') = /a/123/b/456/d/789/{u/rl}/{http:/www.163.com/}/
BuildRewriteURL('a=123&b=456&c=&d=789&u/rl=http://www.163.com/', '2') = /a/123/b/456/d/789/{u/rl}/{http:/www.163.com/}
关键字: PHP
1179
1599
1035
863
1619
1677
1293
991
963
1920
9598
6001
5528
5122
4573
4276
3421
3340
3339
3274