PHP 终极重定向和链接制造商
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 终极重定向和链接制造商相关的知识,希望对你有一定的参考价值。
function redirect($src=null, $rtnString=false) {
global $path;
switch(true) {
case preg_match("/\:\/\//", $src) :
$host = $uri = "";
break;
case ($src == null || $src[0] == "/") :
$host = "http://".$_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\')."/".$path;
$src = substr($src, 1);
break;
default :
$host = "http://".$_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\')."/";
break;
}
$src = generateGetValue($src);
if($rtnString) {
echo $host.$uri.$src;
return;
}
header("Location: ".$host.$uri.$src);
exit();
}
function getLink($value, $src=null, $att=null, $post=false, $stringReturn=null) {
global $path;
if($src == null) $src = "#";
if($src[0] == "/") { $src = $path.substr($src, 1); }
if($att != null) $att = " ".$att;
$src = generateGetValue($src);
if($post) {
$id = "link_post_".uniqid();
$postAction = explode("?", $src);
$action = ($postAction[0] != null) ? $postAction[0] : $_SERVER["REQUEST_URI"] ;
if(isset($postAction[1])) {
$param = explode("&", $postAction[1]);
$return = "<form style=\"display:none;\" id=\"".$id."\" action=\"".$action."\" method=\"post\" enctype=\"application/x-www-form-urlencoded\">";
$params = explode("&", $postAction[1]);
foreach($params as $key) {
$param = explode("=", $key);
$return .= "<input type=\"hidden\" name=\"".$param[0]."\" value=\"".$param[1]."\" />";
}
$return .= "</form>";
$return .= "<a href=\"javascript:void(0);\" onclick=\"getElementById('".$id."').submit();\"".$att.">".$value."</a>";
} else {
$post = false;
}
}
if(!$post) {
$return = "<a href=\"".$src."\"".$att.">".$value."</a>";
}
if($stringReturn) return $return;
echo $return;
}
function generateGetValue($src=null, $byPassException=true) {
if(strpos($src, "&") !== false && strpos($src, "?") === false) {
if(!isset($_SERVER["QUERY_STRING"]) || $_SERVER["QUERY_STRING"] == "") {
$src[strpos($src, "&")] = "?";
} else {
$_src = explode("&", $src);
$arg = array();
$src = array_shift($_src)."?";
$argv = explode("&", $_SERVER["QUERY_STRING"]."&".implode("&", $_src));
foreach($argv as $value) {
$pos = strpos($value, "=");
if($pos === false) {
$arg[$value] = "";
} else {
$arg[substr($value, 0, $pos)] = substr($value, $pos+1);
}
}
foreach($arg as $key => $value) {
if($byPassException && ($key[0] == "!" || array_key_exists("!".$key, $arg))) continue;
$src .= $key;
if($value != "") $src .= "=".$value;
$src .= "&";
}
if(substr($src, -1) == "&") $src = substr($src, 0, -1);
}
}
return $src;
}
以上是关于PHP 终极重定向和链接制造商的主要内容,如果未能解决你的问题,请参考以下文章