note 93877 added to reserved.variables.get
| From: | AlbertoLepedev at alepe dot com | Date: | Mon, 05 Oct 2009 04:23:09 +0000 |
| Subject: | note 93877 added to reserved.variables.get | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-161339@lists.php.net to get a copy of this message | ||
This Function will help you to manage your GET parameters to facilitate coding and prevent
duplication. This is a basic version but it can be easily extended.
<?php
// Author: Alberto Lepe (www.alepe.com)
/* Process $_GET to preserve user custom parameters
* the arguments is a list of URL parameters that should be removed/changed from URL
* for example:
*
* URL = "index.php?s=1&fi=2&m=4&p=3
*
* if called: fixGet("s"); the result has to be: ?fi=2&m=4&p=3
* if called: fixGet("s&m"); the result has to be: ?fi=2&p=3
* if called: fixGet("s=4"); the result has to be: ?s=4&fi=2&m=4&p=3
* if called: fixGet("s=2&m"); the result has to be: ?s=2&fi=2&p=3
* if called: fixGet("s=&m=3"); the result has to be: ?s=&fi=2&m=3&p=3
* if called: fixGet("s=2&m="); the result has to be: ?s=2&fi=2&m=&p=3
* Special: when it ends with a =":" its to leave it open at the end
* (just first occurrence) to facilitate concatenation:
* if called: fixGet("s=2&m:"); the result has to be: ?s=2&fi=2&p=3&m
* if called: fixGet("s=2&m:="); the result has to be: ?s=2&fi=2&p=3&m=
*
* Usage with HTML (using the URL example above and $id = 99):
*
* <a href="index.php<?php echo fixGet('m=2&s&fi:=').$id ?>"
>Link</a>
* Explanation: change "m" to 2, delete "s" and "fi" gets the $id
value. ("p" is kept as it is not specified)
* will output: <a href='index.php?m=2&p=3&fi=99'>Link</a>
*/
public function fixGet($args) {
if(count($_GET) > 0) {
if(!empty($args)) {
$lastkey = "";
$pairs = explode("&",$args);
foreach($pairs as $pair) {
if(strpos($pair,":") !== false) {
list($key,$value) = explode(":",$pair);
unset($_GET[$key]);
$lastkey = "&$key$value";
} elseif(strpos($pair,"=") === false)
unset($_GET[$pair]);
else {
list($key, $value) = explode("=",$pair);
$_GET[$key] = $value;
}
}
}
return "?".((count($_GET) > 0)?http_build_query($_GET).$lastkey:"");
}
?>
To test, copy+paste the following code into testFixGet.php
<?php
/*
* Unit Test for fixGet()
*/
$cases = array (
0 => array("s" => 1, "fi" => 2, "m" => 4,
"p" => 3),
1 => array("s" => "", "fi" => "", "m"
=> 4, "p" => 3),
);
$test[0] = array(
"s" => "fi=2&m=4&p=3",
"s&m" => "fi=2&p=3",
"s=4" => "s=4&fi=2&m=4&p=3",
"s=2&m" => "s=2&fi=2&p=3",
"s=&m=3" => "s=&fi=2&m=3&p=3",
"s=2&m=" => "s=2&fi=2&m=&p=3",
"s=2&m:=" => "s=2&fi=2&p=3&m=",
"z=9" => "s=1&fi=2&m=4&p=3&z=9",
"z:" => "s=1&fi=2&m=4&p=3&z",
"s:&m=3" => "fi=2&m=3&p=3&s",
"s&m=3" => "fi=2&m=3&p=3",
);
$test[1] = array(
"s" => "fi=&m=4&p=3",
"s&m" => "fi=&p=3",
"s=4" => "s=4&fi=&m=4&p=3",
"s=2&m" => "s=2&fi=&p=3",
"s=&m=3" => "s=&fi=&m=3&p=3",
"s=2&m=" => "s=2&fi=&m=&p=3",
"s=2&m:=" => "s=2&fi=&p=3&m=",
"z=9" => "s=&fi=&m=4&p=3&z=9",
"z:" => "s=&fi=&m=4&p=3&z",
);
foreach($cases as $x => $value) {
echo "<hr> CASE: $x <hr>\n";
foreach($test[$x] as $arg => $expected) {
$_GET = $cases[$x];
$res = myForm::fixGet($arg);
echo (($res === "?".$expected)?"OK":"NG ($res)")."
[$arg]<br>\n";
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 211.6.179.194
----
Manual Page -- http://www.php.net/manual/en/reserved.variables.get.php
Edit -- https://master.php.net/note/edit/93877
Del: integrated -- https://master.php.net/note/delete/93877/integrated
Del: useless -- https://master.php.net/note/delete/93877/useless
Del: bad code -- https://master.php.net/note/delete/93877/bad+code
Del: spam -- https://master.php.net/note/delete/93877/spam
Del: non-english -- https://master.php.net/note/delete/93877/non-english
Del: in docs -- https://master.php.net/note/delete/93877/in+docs
Del: other reasons-- https://master.php.net/note/delete/93877
Reject -- https://master.php.net/note/reject/93877
Search -- https://master.php.net/manage/user-notes.php