Is there a good way to 开发者_如何学JAVAkeep consistency in the $_GET
For example if i have a url
http://www.example.com/index.php?Id=5&sort=new
And i want to now add a new $_GET
field at the end of that so is there any way i can create a function that will keep the Id
and sort
and add my new var lim
?
So the URL Now looks like: http://www.example.com/index.php?Id=5&sort=new&lim=100
maybe something like:
add_new_get('http://www.example.com', 'lim=100');
Im sure i can come up with a function but just wondering if someone has implemented such thing that they would like to share :D
you could always just do $_SERVER['REQUEST_URI'].'&yourstuff=whateves';
I guess you could wrap that in a function if you really wanted to.
the above includes everything before the file name. if you want ONLY the get requests you can do:
$new_req = "?";
foreach ($_GET as $key=>$val){
$new_req .= "&".$key."=".$val;
}
$new_req .= $your_new_value;
You might find it preferable to use a class to manage your query string handling rather than writing your own.
See HTTPQueryString
It isn't terribly complex to write a function to take a url and build your query string, but with a class like HTTPQueryString, you don't have to worry about making sure you have the right encoding, tests for whether there are already variables present in the string, etc.
Edit
e.g.
$query = new HttpQueryString(false, 'Id=5&sort=new');
$query->mod('lim=100');
echo $query->toString(); // echoes http://www.example.com/index.php?Id=5&sort=new&lim=100
A slightly more hackish solution is to use output_add_rewrite_var and output_rewrite_vars
精彩评论