duandaiqin6080 2016-10-12 11:02
浏览 28
已采纳

如何检测站点链接中的“?”以进行重定向?

I am using a Function for redirection of external links in my website

How This Function works:- it will detect all nearbuy.com links and Apply a redirection Rule and then redirected.

For Example:- nearbuy.com is a domain So this function add prefix before nearbuy.com external url i.e http://tracking.nearbuy.com/aff_c?offer_id=9&aff_id=479&url=

Then http://www.nearbuy.com and url parameter to make it work.

What i want to do:-

1) If nearbuy.com link already has "?" then it will use "&"

2) If nearbuy.com link don't have "?" then it will use "?"

What is problem:-

I am using below Method but its not detecting "?" in links and add "?" again in end of links while redirection which is problem

Here is Code:-

if(stripos($url, "nearbuy.com")) {
    $url_args = parse_url($url);
    $url_params = "?utm_source=nap&utm_medium=cps&utm_campaign={affiliate_id}";
        if(!empty($durl["query"])) {    
            $url_params = "&utm_source=nap&utm_medium=cps&utm_campaign={affiliate_id}";
        }
    $redirect_url = "http://tracking.nearbuy.com/aff_c?offer_id=9&aff_id=479&url=" . urlencode($url . $url_params);
        goto redirect_to_uri;
    break;
}
  • 写回答

1条回答 默认 最新

  • duanhui1869 2016-10-12 11:06
    关注

    You could use parse_url and do something like:

    $yourQueryParams = [
        'offer_id' => 9,
        'aff_id' => 479,
    ];
    
    $url .= (parse_url($url, PHP_URL_QUERY) ? '&' : '?') . http_build_query($yourQueryParams);
    
    // redirect with your query params
    header("Location: $url");
    die();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line