http_build_query将20添加到分页

I've just started using http_build_query. I've implemented it into a pagination script of mine however it's not working right.

What I am expecting is for my urls to be http://example.com/categories.php?cat=category&page=2 but for some reason it is adding 20 to my page numbers making the urls like so http://example.com/categories.php?cat=category&page=22 <<< This is actually for page 2.

Here is my original script:

  $img_start=0;
  $img_limit=8;

  if(isset($_GET['page'])) {
   $page=$_GET['page'];
   $img_start=($page-1)*$img_limit;
  }
  else { $page = 1; }

  if($_COOKIE['age_verification'] == "adult") {
   $img_total = mysqli_num_rows(mysqli_query($conn, "select * from gallery_img WHERE $cat = 1"));
  }
  else if($_COOKIE['age_verification'] == "child") {
   $img_total = mysqli_num_rows(mysqli_query($conn, "select * from gallery_img WHERE $cat = 1 AND WHERE nude != 1"));
  }
  $img_total_count = ceil($img_total/$img_limit);

  if($img_limit != $img_total) {
  echo '<nav aria-label="Page navigation">
         <ul class="pagination">'  . PHP_EOL;
  if($page>1) {
  echo '<li><a href="?page='.($page-1).'" aria-label="Previous"><span aria-hidden="true">Previous</span></a></li>'  . PHP_EOL;
  }
  for($i=1;$i<=$img_total_count;$i++) {
   if($i==$page) { echo "<li class='active'><a href='?page=".$i."'>".$i."</a></li>"  . PHP_EOL; }
   else { echo "<li><a href='?page=".$i."'>".$i."</a></li>"  . PHP_EOL; }
  }
  if($page!=$img_total_count) {
   if(!isset($page)) { echo '<li><a href="?page='.($page+2).'" aria-label="Next"><span aria-hidden="true">Next</span></a></li>'  . PHP_EOL; }
   else { echo '<li><a href="?page='.($page+1).'" aria-label="Next"><span aria-hidden="true">Next</span></a></li>'  . PHP_EOL; }
  }

echo '</ul>
      </nav>' . PHP_EOL;
  }

And then these are the lines I modified which are adding 20 to my page numbers:

$pageParameters = http_build_query(array_merge($_GET, array("page"=>2)));

  if($img_limit != $img_total) {
  echo '<nav aria-label="Page navigation">
         <ul class="pagination">'  . PHP_EOL;
  if($page>1) {
  echo '<li><a href="'.htmlspecialchars("$_SERVER[PHP_SELF]?$pageParameters").($page-1).'" aria-label="Previous"><span aria-hidden="true">Previous</span></a></li>'  . PHP_EOL;
  }
  for($i=1;$i<=$img_total_count;$i++) {
   if($i==$page) { echo "<li class='active'><a href='".htmlspecialchars("$_SERVER[PHP_SELF]?$pageParameters").$i."'>".$i."</a></li>"  . PHP_EOL; }
   else { echo "<li><a href='".htmlspecialchars("$_SERVER[PHP_SELF]?$pageParameters").$i."'>".$i."</a></li>"  . PHP_EOL; }
  }
  if($page!=$img_total_count) {
   if(!isset($page)) { echo '<li><a href="'.htmlspecialchars("$_SERVER[PHP_SELF]?$pageParameters").($page+2).'" aria-label="Next"><span aria-hidden="true">Next</span></a></li>'  . PHP_EOL; }
   else { echo '<li><a href="'.htmlspecialchars("$_SERVER[PHP_SELF]?$pageParameters").($page+1).'" aria-label="Next"><span aria-hidden="true">Next</span></a></li>'  . PHP_EOL; }
  }

echo '</ul>
      </nav>' . PHP_EOL;
  }

Why would this be adding 20 to my page numbers?

Your code is not adding 20 to the existing page number, you're actually doing a concatenation here, which is wrong. Look at the following code snippet,

... htmlspecialchars("$_SERVER[PHP_SELF]?$pageParameters").($page-1) ...
                                                          ^ see here

If your existing URL is http://example.com/categories.php?cat=category&page=2, 2 would simply get appended after the above operation, making it http://example.com/categories.php?cat=category&page=22. And this holds true for all the cases where you have used the above operation. So the solution would be to change your pagination links section like this:

parse_str($_SERVER["QUERY_STRING"], $url_array);
unset($url_array['page']);
$pageParameters = http_build_query($url_array);

if($img_limit != $img_total) {
    echo '<nav aria-label="Page navigation"><ul class="pagination">';
    if($page>1) {
        ?>
        <li><a href="<?php echo $_SERVER['PHP_SELF'].'?page='.($page-1); echo isset($pageParameters) && !empty($pageParameters) ? "&" . $pageParameters : ""; ?>" aria-label="Previous"><span aria-hidden="true">Previous</span></a></li>
        <?php
    }
    for($i=1;$i<=$img_total_count;$i++) {
        if($i==$page) { 
            ?>
            <li class='active'><a href="<?php echo $_SERVER['PHP_SELF'].'?page='.$i; echo isset($pageParameters) && !empty($pageParameters) ? "&" . $pageParameters : ""; ?>"><?php echo $i; ?></a></li>
            <?php
        }
        else { 
            ?>
            <li><a href="<?php echo $_SERVER['PHP_SELF'].'?page='.$i; echo isset($pageParameters) && !empty($pageParameters) ? "&" . $pageParameters : ""; ?>"><?php echo $i; ?></a></li>
            <?php
        }
    }
    if($page!=$img_total_count) {
        if(!isset($page)) { 
            ?>
            <li><a href="<?php echo $_SERVER['PHP_SELF'].'?page='.($page+2); echo isset($pageParameters) && !empty($pageParameters) ? "&" . $pageParameters : ""; ?>" aria-label="Next"><span aria-hidden="true">Next</span></a></li>
            <?php
        }else { 
            ?>
            <li><a href="<?php echo $_SERVER['PHP_SELF'].'?page='.($page+1); echo isset($pageParameters) && !empty($pageParameters) ? "&" . $pageParameters : ""; ?>" aria-label="Next"><span aria-hidden="true">Next</span></a></li>
            <?php
        }
    }
    echo '</ul></nav>';
}

look at how you're printing the links..

The first two comes from this array

$pageParameters = http_build_query(array_merge($_GET, array("page"=>2)));

pageParameters already ends with &page=2 and then you add arbitrary numbers to the end of the url, so these will now print &page=20, &page=21, etc.

("$_SERVER[PHP_SELF]?$pageParameters").$i."
("$_SERVER[PHP_SELF]?$pageParameters").($page-1)
("$_SERVER[PHP_SELF]?$pageParameters").($page+1)