Return to Snippet

Revision: 2401
at March 17, 2010 14:06 by frankyfish


Updated Code
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;
}

Revision: 2400
at February 8, 2007 10:13 by frankyfish


Updated Code
function redirect($src=null) {
	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 = _parseGetValue($src);

	header("Location: ".$host.$uri.$src);
	exit();
}

function getLink($value, $src=null, $att=null, $isReturn=null) {
	global $path;
	if($src == null) $src = "#";
	if($src[0] == "/") { $src = $path.substr($src, 1); }
	if($att != null) $att = " ".$att;
	$src = _parseGetValue($src);
	if($isReturn) return "<a href=\"".$src."\"".$att.">".$value."</a>";
	echo "<a href=\"".$src."\"".$att.">".$value."</a>";
}

function _parseGetValue($src=null) {
	if(strpos($src, "&") !== false && strpos($src, "?") === false) {
		if(!isset($_SERVER["argv"][0])) {
			$src[strpos($src, "&")] = "?";
		} else {
			$_src = explode("&", $src);
			$arg = array();
			$src = array_shift($_src)."?";
			$argv = explode("&", $_SERVER["argv"][0]."&".implode("&", $_src));
			foreach($argv as $value) {
				$pos = strpos($value, "=");
				$arg[substr($value, 0, $pos)] = substr($value, $pos+1);
			}
			foreach($arg as $key => $value) {
				$src .= $key."=".$value."&";
			}
			$src = substr($src, 0, -1);
		}
	}
	return $src;
}

Revision: 2399
at February 8, 2007 10:12 by frankyfish


Initial Code
function redirect($src=null) {
	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 = _parseGetValue($src);

	header("Location: ".$host.$uri.$src);
	exit();
}

function getLink($value, $src=null, $att=null, $isReturn=null) {
	global $path;
	if($src == null) $src = "#";
	if($src[0] == "/") { $src = $path.substr($src, 1); }
	if($att != null) $att = " ".$att;
	$src = _parseGetValue($src);
	if($isReturn) return "<a href=\"".$src."\"".$att.">".$value."</a>";
	echo "<a href=\"".$src."\"".$att.">".$value."</a>";
}

function _parseGetValue($src=null) {
	if(strpos($src, "&") !== false && strpos($src, "?") === false) {
		if(!isset($_SERVER["argv"][0])) {
			$src[strpos($src, "&")] = "?";
		} else {
			$_src = explode("&", $src);
			$arg = array();
			$src = array_shift($_src)."?";
			$argv = explode("&", $_SERVER["argv"][0]."&".implode("&", $_src));
			foreach($argv as $value) {
				$pos = strpos($value, "=");
				$arg[substr($value, 0, $pos)] = substr($value, $pos+1);
			}
			foreach($arg as $key => $value) {
				$src .= $key."=".$value."&";
			}
			$src = substr($src, 0, -1);
		}
	}
	return $src;
}

Initial URL

                                

Initial Description

                                

Initial Title
Ultimate Redirection and Link Maker

Initial Tags
php

Initial Language
PHP