0

This is the code i use for creating pagination links:

echo "<a href='".$_SERVER['PHP_SELF']."?".$_SERVER["QUERY_STRING"]."&page=$ni'><span>$ni</span></a>";

The only problem is that every time i click the link, the 'page' parameter keeps multiplying in the URL:

http:..php?para1=something&para2=something&para3=something&page=2&page=3&page=4

I can undestand why. Another 'page' parameter is added to existing URL every time i click the link.

Any ideas how to solve it?

Thanks

1
  • Never, ever use $_SERVER['PHP_SELF'] without at least htmlencodeing it first. Otherwise, your code is very susceptible to PHP injection. Same thing with any of the $_SERVER variables that can come from the client, really. Commented Jul 31, 2012 at 14:12

2 Answers 2

3

Do not append .$_SERVER["QUERY_STRING"]. every time your page loads.

Just use this:

echo "<a href='".$_SERVER['PHP_SELF']."?page=$ni'><span>$ni</span></a>";

If you need another parameter from QUERY_STRING, add it independently to your URL.

I.E.:

echo "<a href='".$_SERVER['PHP_SELF']."?page=$ni&para1=something...'><span>$ni</span></a>";
Sign up to request clarification or add additional context in comments.

Comments

0

On your sample code:

$_SERVER["QUERY_STRING"] = para1=something&para2=something&para3=something&page=2&page=3&page=4 

so when you are creating your link and pre-append $_SERVER["QUERY_STRING"] to &page=$ni, your link is really

para1=something&para2=something&para3=something&page=2&page=3&page=4&page=$ni

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.