所以例如我登陆页面。我的网址现在是example.com/page?pagenum=1,我点击了第二页,所以我的网址现在是example.com/page?pagenum=1&pagenum=2。现在一切正常,但你可以想象会变得有点乱,所以宁愿它更新已经在URL中的参数。我目前正在使用以下内容获取当前页面网址:
<?php
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>
然后链接就像:
<a href='<?php echo curPageURL(); ?>&pagenum=<?php echo "1"; ?>'> 1 </a>
更新 我在需要保留的URL中有其他参数,我只需要更新'pagenum'
答案 0 :(得分:0)
你可以这样使用http_build_query
:
$all_params = $_GET;
$all_params["page"] = "2";
$link = "page.php?" . http_build_query($all_params); // "page.php?page=2&foo=bar"
答案 1 :(得分:0)
问题的存在是因为REQUEST_URI
包含路径和查询字符串,并且每次翻页时都会附加一个新的查询字符串。要提取路径,您可以使用此代码,取自this answer:
$path = strtok($_SERVER["REQUEST_URI"], '?');
然后,您可以复制现有的查询字符串字段,但删除pagenum
:
$fields = $_GET;
unset($fields['pagenum']); // remove any existing pagenum value
$path .= '?' . http_build_query($fields); // re-append the query string
然后您可以使用或多或少的现有链接代码:
<a href='<?php echo $path; ?>&pagenum=<?php echo "1"; ?>'> 1 </a>