Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP add/modify a query string param & get the URL

How can redirect the user back to the same page with the existing Query Strings but have 1 added/modified like "page".

I suppose 1 method is:

  1. parse the $_SERVER['QUERY_STRING'] into an array
  2. if page exists in the array, modify the value, else add it
  3. use http_build_query to get the query string to append to $_SERVER['PHP_SELF']

but is there a better/more direct way?

like image 772
JM at Work Avatar asked May 05 '11 04:05

JM at Work


People also ask

How can I add or update a query string parameter?

set('PARAM_HERE', VALUE_HERE); history. pushState(null, '', url); This will preserve everything about the URL and only change or add the one query param. You can also use replaceState instead of pushState if you don't want it to create a new browser history entry.

How do you change a query string?

Edit / Update a ParameterAfter setting the new value you can get the new query string with the toString() method. This query string can be set as the new value of the search property of the URL object. The final new url can then be retrieved with the toString() method of the URL object.

How to modify query-string parameters in PHP?

Two quick functions, one for adding a query string variable and another to remove a query string variable. Either of these functions can be used in any PHP script to modify query-string parameters. Here is a function that will add the specified query-string variable (key and value) to the specified URL:

How to parse query strings into variables in PHP?

With the help of the parse_str () function, you can parse query strings into variables. The syntax of this function is like so: Now, let’s see examples of how to use these two functions for getting the parameters from the URL string.

How to get parameters from a URL string in PHP?

Almost all developers at least once in their practice have wondered how to get parameters from a URL string with the help of PHP functions. In our tutorial, we will provide you with two simple and handy functions such as pase_url () and parse_str () that will allow you to do that. Read on and check out the options below.

What is a parameter in a SQL query string?

If parameters are used, they are referred to in the query string as $1, $2, etc. The same parameter may appear more than once in the query; the same value will be used in that case. params specifies the actual values of the parameters. A null value in this array means the corresponding parameter is SQL NULL .


1 Answers

Just to make sure you know about it, use parse_str to parse your query string:

<?php
parse_str($_SERVER['QUERY_STRING'], $query_string);
$query_string['page'] = basename($_SERVER['PHP_SELF']);
$rdr_str = http_build_query($query_string);
like image 95
Jim Rubenstein Avatar answered Sep 23 '22 05:09

Jim Rubenstein