鍍金池/ 問(wèn)答/PHP  網(wǎng)絡(luò)安全/ php URL $_GET 累加上去?

php URL $_GET 累加上去?

想問(wèn)一下
我想讓網(wǎng)址get能夠累加上去
比如說(shuō)現(xiàn)在的網(wǎng)址是

region=abc&status=1

當(dāng)我可能進(jìn)入另一個(gè)網(wǎng)址後
我想讓他在後面加上 &xxx=1
但是不會(huì)洗掉前面的GET參數(shù)

region=abc&status=1&xxx=1

無(wú)論前面有多少GET參數(shù)都會(huì)加上去,但重複的就不加
這樣我就不需要再網(wǎng)址那邊重新定義有幾個(gè)GET參數(shù)了
只要網(wǎng)址上沒(méi)有相同的參數(shù)就累加上去
這個(gè)有什麼代碼可以做到?

補(bǔ)充
但如果網(wǎng)址沒(méi)有任何參數(shù) 此時(shí)要加參數(shù)時(shí)
它能自動(dòng)判別,也就是說(shuō)變成

xxx.php?xxx=xxx

而不是&
除非前面已經(jīng)有?了則會(huì)是&

回答
編輯回答
心上人

http_build_query

2018年7月14日 11:27
編輯回答
孤慣

你可以試試這個(gè)

$url = 'xxx.php?xxx=xxx';
$params = [
    'region' => 'abc',
    'status' => 1
];
$urlParams = http_build_query($params);

if(strpos($url, '?')){
    echo $url.$urlParams;
}else{
    echo $url.'?'.$urlParams;
}
2018年6月12日 18:46
編輯回答
寫(xiě)榮

你好這個(gè)問(wèn)題很簡(jiǎn)單:
1)一般這種需求用在商品列表?xiàng)l件篩選的業(yè)務(wù)上面
比如:鞋 我想選擇紅色,42號(hào)
2)使用js先獲取當(dāng)前的url,然后直接拼接到上面就可以了
代碼如下:
var host = window.location.href; //獲取url
window.location.href = url+ "/xxx/1"; //拼接url
解決問(wèn)題

2017年12月27日 16:42
編輯回答
巫婆
// 先來(lái)一個(gè)也是在當(dāng)前網(wǎng)站上看到的函數(shù)
function parse_url(url)
{
    var a = document.createElement('a');
    a.href = url;
    return {
        source:url,
        protocol:a.protocol.replace(':', ''),
        host:a.hostname,
        port:a.port,
        query:a.search,
        params:(function(){
            var ret = {}, seg = a.search.replace(/^\?/, '').split('&'), len = seg.length, i = 0, s;
            for(;i<len;i++) {
                if(!seg[i]) {
                    continue;
                }
                s = seg[i].split('=');
                ret[s[0]] = s[1];
            }
            return ret;
        })(),
        file:(a.pathname.match(/\/([^\/?#]+)$/i) || [, ''])[1],
        hash:a.hash.replace('#', ''),
        path:a.pathname.replace(/^([^\/])/, '/$1'),
        segments:a.pathname.replace(/^\//, '').split('/')
    };
}
  1. 通過(guò)js將所有鏈接添加點(diǎn)擊事件
  2. 事件中通過(guò)parse_url解析當(dāng)前頁(yè)面地址及目標(biāo)頁(yè)面地址
  3. 合并解析后兩地址的請(qǐng)求參數(shù)
  4. 拼接最終跳轉(zhuǎn)地址
  5. 阻止默認(rèn)行為

如果要通過(guò)php的話, 那得通過(guò)函數(shù)來(lái)生成鏈接

  1. 解析目標(biāo)鏈接
  2. 合并參數(shù)
  3. 返回最終訪問(wèn)鏈接
2017年4月17日 07:21