One way to do it is to use a database to store the results of the query (you could also use that to cache queries - faster for often repeated queries - if your database does not support that)
see this pseudocode:
This would only be good for small result sets... I don't even recommend showing search that has more than 10-20 pages ... I would probably display a message at the last page saying that there are a lot of results, and ask the user to narrow down the query.