justinl
justinl

Reputation: 10538

How to remove a specific $_GET variable from a URL

I have a website authored in PHP where any time a user receives an error I will redirect them to a another page (using header(Location:...)) and put the error ID in the URL so that I know which error to display.

E.g. If the user tries to access a product page but that item is no longer available I will redirect back to the category of items they were previously looking at and display an error based on the error ID I have specified in the URL.

www.example.com/view_category.php?product_category_id=4&error_id=5

There are two things I don't like about this approach:

  1. It displays the error_id in the URL.
  2. if the page is refreshed, the error will still display.

Is there a way to cleanly remove a specific $_GET variable from a URL while leaving the rest of the variables intact AFTER the page is loaded?

I'm thinking maybe it's using modRewrite or a redirect back to the page itself but removing the error_id from the URL or using a $_SESSION variable and avoiding putting the error_id in the URL. Your thoughts?

I really am learning a lot from this community and thought if I posed the question I might be able to learn something new or to get some varied ideas as I'm fairly new to scripting.

Upvotes: 4

Views: 18972

Answers (7)

john maulatul
john maulatul

Reputation: 1

i had same problem
try : http://www.azazia.com/kb/entry/26/

if (!empty($_GET['passvar'])) {
    unset($_GET['passvar']);
    echo "<META HTTP-EQUIV=\"refresh\" CONTENT=\"0; URL=".$_SERVER['PHP_SELF']."\" >";

}

work perfectly for me.

Upvotes: 0

user2392677
user2392677

Reputation: 1

Wouldn't this approach work?

<?php
    $params = array_diff($_GET, array("variable_name" => $value));
    $new_query_string = http_build_query($params);
?>
    <script>window.history.pushState('verify_email', 'Verify Email', '?<?php echo $new_query_string; ?>');</script>

Upvotes: 0

Leandro Muniz
Leandro Muniz

Reputation: 31

Yes, there is a way to remove especific $_GET from PHP...

      varToRemove = "anyVariable";

      foreach($_GET as $variable => $value){
        if($variable != varToRemove){
           $newurl .= $variable.'='.$value.'&';
        }
      }

      $newurl = rtrim($newurl,'&');

Then, put the $newurl in the link.. like this:

pageurl?something=something&<? echo $newurl; ?>

I know it´s an old post, but, other programers may be search for it!

Upvotes: 3

xentek
xentek

Reputation: 2645

Quick Hack: You could have also imploded()'d on "&" in the the $_SERVER['QUERY_STRING'] variable to manipulate that string and then explode()'d it back.

Upvotes: 0

Christopher Done
Christopher Done

Reputation: 5974

One way is to compare the HTTP_REFERER with the SCRIPT_NAME. They'll be the same if the user has hit Refresh.

Upvotes: 1

matpie
matpie

Reputation: 17512

First, log the error in your database :)

After that, set a cookie or session variable and then redirect the user to safe page. When that page is loaded, have it check for the variable, display the error, and then delete variable from the cookie or session array.

Upvotes: 2

Peter Bailey
Peter Bailey

Reputation: 105868

No, there's no way to do that explicitly - at least not without a page refresh but then you'd lose the data anyway.

You're better off using a temporary session variable.

if ( /* error condition */ )
{
  $_SESSION['last_error_id'] = 5;
  header( 'Location: http://www.example.com/view_category.php?product_category_id=4' );
}

Then, in view_category.php

if ( isset( $_SESSION['last_error_id'] ) )
{
  $errorId = $_SESSION['last_error_id'];
  unset( $_SESSION['last_error_id'] );

  // show error #5
}

Upvotes: 7

Related Questions