Ultimate Redirection and Link Maker


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function redirect($src=null, $rtnString=false) {
  2. global $path;
  3. switch(true) {
  4. case preg_match("/\:\/\//", $src) :
  5. $host = $uri = "";
  6. break;
  7. case ($src == null || $src[0] == "/") :
  8. $host = "http://".$_SERVER['HTTP_HOST'];
  9. $uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\')."/".$path;
  10. $src = substr($src, 1);
  11. break;
  12. default :
  13. $host = "http://".$_SERVER['HTTP_HOST'];
  14. $uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\')."/";
  15. break;
  16. }
  17. $src = generateGetValue($src);
  18.  
  19. if($rtnString) {
  20. echo $host.$uri.$src;
  21. return;
  22. }
  23. header("Location: ".$host.$uri.$src);
  24. exit();
  25. }
  26.  
  27. function getLink($value, $src=null, $att=null, $post=false, $stringReturn=null) {
  28. global $path;
  29. if($src == null) $src = "#";
  30. if($src[0] == "/") { $src = $path.substr($src, 1); }
  31. if($att != null) $att = " ".$att;
  32. $src = generateGetValue($src);
  33.  
  34. if($post) {
  35. $id = "link_post_".uniqid();
  36. $postAction = explode("?", $src);
  37. $action = ($postAction[0] != null) ? $postAction[0] : $_SERVER["REQUEST_URI"] ;
  38. if(isset($postAction[1])) {
  39. $param = explode("&", $postAction[1]);
  40. $return = "<form style=\"display:none;\" id=\"".$id."\" action=\"".$action."\" method=\"post\" enctype=\"application/x-www-form-urlencoded\">";
  41. $params = explode("&", $postAction[1]);
  42. foreach($params as $key) {
  43. $param = explode("=", $key);
  44. $return .= "<input type=\"hidden\" name=\"".$param[0]."\" value=\"".$param[1]."\" />";
  45. }
  46. $return .= "</form>";
  47. $return .= "<a href=\"javascript:void(0);\" onclick=\"getElementById('".$id."').submit();\"".$att.">".$value."</a>";
  48. } else {
  49. $post = false;
  50. }
  51. }
  52.  
  53. if(!$post) {
  54. $return = "<a href=\"".$src."\"".$att.">".$value."</a>";
  55. }
  56.  
  57. if($stringReturn) return $return;
  58. echo $return;
  59. }
  60.  
  61.  
  62. function generateGetValue($src=null, $byPassException=true) {
  63. if(strpos($src, "&") !== false && strpos($src, "?") === false) {
  64. if(!isset($_SERVER["QUERY_STRING"]) || $_SERVER["QUERY_STRING"] == "") {
  65. $src[strpos($src, "&")] = "?";
  66. } else {
  67. $_src = explode("&", $src);
  68. $arg = array();
  69. $src = array_shift($_src)."?";
  70. $argv = explode("&", $_SERVER["QUERY_STRING"]."&".implode("&", $_src));
  71.  
  72.  
  73. foreach($argv as $value) {
  74. $pos = strpos($value, "=");
  75. if($pos === false) {
  76. $arg[$value] = "";
  77. } else {
  78. $arg[substr($value, 0, $pos)] = substr($value, $pos+1);
  79. }
  80. }
  81.  
  82. foreach($arg as $key => $value) {
  83. if($byPassException && ($key[0] == "!" || array_key_exists("!".$key, $arg))) continue;
  84. $src .= $key;
  85. if($value != "") $src .= "=".$value;
  86. $src .= "&";
  87. }
  88. if(substr($src, -1) == "&") $src = substr($src, 0, -1);
  89. }
  90. }
  91. return $src;
  92. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.