Register now and start sharing your code snippets.
-->
How to link back to current page with or without same parameters (PHP)
Apache posted 6 months ago by christian
1 function link_back($blacklist = array()) 2 { 3 $parameters = split("&", $_SERVER['QUERY_STRING']); 4 5 $uri = array_shift(split("\?", $_SERVER['REQUEST_URI'])); 6 7 $index = 0; 8 9 foreach($parameters as $parameter) 10 { 11 $parameter = split("=", $parameter); 12 13 $name = $parameter[0]; 14 $value = $parameter[1]; 15 16 if(in_array($name, $blacklist)) 17 { 18 unset($parameters[$index]); 19 } 20 21 $index++; 22 } 23 24 if(count($parameters) > 0) 25 { 26 $parameters = "?".join("&", $parameters); 27 } 28 else 29 { 30 $parameters = ''; 31 } 32 33 return $uri.$parameters; 34 }
Usage:
1 # URI is /wow_amazing_code?not_wanted=23&wanted=1 2 3 echo link_back(array('not_wanted')); # prints out /wow_amazing_code?wanted=1