duanjia7607 2013-09-01 14:49
浏览 83
已采纳

php:重定向到referer url但是向querystring添加一些内容

I have some code that is accessible from several different pages. Based on which page it comes from and which variables it sends, it does slightly different things.

I guess I could write totally new pages for each referring page but really don't want to do that as the functionality is similar.

In the case of errors, I want to redirect back to the referer page. Similarly, upon success I also want to redirect back to the referer page. However, I'd like to add a parameter that describes the error or let's the user know it was a success to distinguish between a failure and success.

Let's say there are two referer pages, emailstory.php?id=231 and emailsignup.php One has a quesion mark and the other doesn't. I want to send the user back to the referer but add error=1. I need some code that appends &error=1 to the first referer but ?error=1 to the second and is robust to many variations.

php

$referer = $_SERVER['HTTP_REFERER'];
$id = $_POST['id'];
$returnpage = $referer."&error=1";
if (!isset($_REQUEST['id'])) {
header("Location:$returnpage");
}

Above would work in some instances but only if there is already a ? launching a querystring

Is there an easy, foolproof way to do this?

Thanks

  • 写回答

2条回答 默认 最新

  • donglugou6652 2013-09-01 14:56
    关注
    <?php
    $returnpage = $_SERVER['HTTP_REFERER'] . '?querystring';
    
    if (!isset($_REQUEST['id'])) 
    {
        header("Location:$returnpage");
        exit();
    }
    

    Well, since after the first '?' all other '?' will be ignored you can do what I did above... It will work perfectly and it is totally valid.

    Moreover, do not forget to exit or kill the application as after the header function the application will still continue to run if you do not quit explicitly.

    But if you still insist on having one question mark then the snippet below will do the work...

    $mark = strpos($_SERVER['HTTP_REFERER'], '?') === false ? '?' : '&';
    $returnpage = $_SERVEr['HTTP_REFERER'] . $mark . 'abc=1&def=2&is=t';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度