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条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀