douji6940 2009-07-30 23:15
浏览 130
已采纳

如何从URL中删除特定的$ _GET变量

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.

  • 写回答

7条回答 默认 最新

  • duanjiwang2927 2009-07-30 23:23
    关注

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?