Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is the actual code for what's described above as the "the safest 'correct' method"...</p> <pre><code>function reduce_query($uri = '') { $kill_params = array('gclid'); $uri_array = parse_url($uri); if (isset($uri_array['query'])) { // Do the chopping. $params = array(); foreach (explode('&amp;', $uri_array['query']) as $param) { $item = explode('=', $param); if (!in_array($item[0], $kill_params)) { $params[$item[0]] = isset($item[1]) ? $item[1] : ''; } } // Sort the parameter array to maximize cache hits. ksort($params); // Build new URL (no hosts, domains, or fragments involved). $new_uri = ''; if ($uri_array['path']) { $new_uri = $uri_array['path']; } if (count($params) &gt; 0) { // Wish there was a more elegant option. $new_uri .= '?' . urldecode(http_build_query($params)); } return $new_uri; } return $uri; } $_SERVER['REQUEST_URI'] = reduce_query($_SERVER['REQUEST_URI']); </code></pre> <p>However, since this will likely exist prior to the bootstrap of your application, you should probably put it into an anonymous function. Like this...</p> <pre><code>call_user_func(function($uri) { $kill_params = array('gclid'); $uri_array = parse_url($uri); if (isset($uri_array['query'])) { // Do the chopping. $params = array(); foreach (explode('&amp;', $uri_array['query']) as $param) { $item = explode('=', $param); if (!in_array($item[0], $kill_params)) { $params[$item[0]] = isset($item[1]) ? $item[1] : ''; } } // Sort the parameter array to maximize cache hits. ksort($params); // Build new URL (no hosts, domains, or fragments involved). $new_uri = ''; if ($uri_array['path']) { $new_uri = $uri_array['path']; } if (count($params) &gt; 0) { // Wish there was a more elegant option. $new_uri .= '?' . urldecode(http_build_query($params)); } // Update server variable. $_SERVER['REQUEST_URI'] = $new_uri; } }, $_SERVER['REQUEST_URI']); </code></pre> <p>NOTE: Updated with <code>urldecode()</code> to avoid double encoding <code>via http_build_query()</code> function. NOTE: Updated with <code>ksort()</code> to allow params with no value without an error.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload