可能是重复问题:
如何在分页时保留URL参数
我想使用PHP将参数添加到当前URL中,但我如何知道URL是否已经包含了一个参数?
例如:
foobar.com/foo/bar/index.php => foobar.com/foo/bar/index.php?myparameter=5 foobar.com/index.php?foo=7 => foobar.com/index.php?foo=7&myparameter=5
主要问题在于我不知道是否需要添加“?”。
我的代码(从某处找到,但它无效):
<?php if(/?/.test(self.location.href)){ //if url contains ?
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]&myparameter=5";
} else {
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]?myparameter=5";
}?>