November 12th in PHP Scripts by .

How to remove GET variable within $_SERVER['QUERY_STRING'] in PHP

I was working on a mysql search script with paging where i had to pass the same GET parameters to page numbers like

search.php?q=what+europe+scripts&page=1

I used $_SERVER['QUERY_STRING'] to get the GET parameters. But the problem i got was when i was in page 2 it would show query string like

search.php?q=what+europe+scripts&page=1&page=2

and if i am in page 5, it would show like

search.php?q=what+europe+scripts&page=1&page=2&page=3&page=4&page=5

This is an unwanted problem and i wanted to strip/remove only the page variable.

If you are in such a situation here is the solution.

1. Use ereg_replace to strip unwanted variable. (or)

list($q) = explode('&',$_SERVER['QUERY_STRING']);

Here the GET variables separated using & and we take only the first value in the array (say q variable).

2. Use SESSIONS to hold just the query variable.

Just store the GET variable by registering a session and use it on pages.

Similar Posts:

One Comment

  • Kathiravan Manoharan
    January 17, 2009

Leave A Comment.