Search code examples
phpget

how to remove all $_GET params except my owns


need to remove all $_GET params from address bar - set by facebook etc - except my own id and title

something like:

if(isset($_GET['any-key-except-id-or-title'])){
    header('location: my-native-url');
}

Solution

  • Loop through $_GET and remove any elements with another key. Then use http_build_query() to create a query string based on the remainder.

    foreach (array_keys($_GET)) as $key) {
        if ($key != 'id' && $key != 'title') {
            unset($_GET[$key]);
        }
    }
    
    header('Location: my-native-url?' . http_build_query($_GET));