简体   繁体   中英

php handling pagination $_GET requests past limit

I have a template page that paginates through results with a $_GET request.

How should I handle any $_GET errors where a user vists a page that would exceed the total number of avaliable results?

I've thought of the following solutions but I'm not sure which is the ideal choice:

  • 404 - display 404 error message
  • 301/2 redirect back to the last available page of results
  • Alter the data request to show the last available batch of results (not a redirect)
  • 301/2 redirect back to the first page
  • Just display an 'no results' message (not a redirect)

I'll need to take into considering that the total number of results may increase or decrease over time. Meaning my-page.php?p=3 may or may not display results.

I'm also wonder if I need to consider the possibility of a user or bot spaming my template with $_GET for any page number my-page.php?p=9999999 , or am I overthinking this ?

Thanks

Bots are going to bot. Treat all user input as hostile by default and if the query turns out to be valid treat it as a bonus. If someone requests such a high offset, you won't get any results, so it's not a 404, just a "previous page" link on an otherwise empty list.

Don't get all clever by trying to redirect. Stick with the "garbage in, garbage out" principle.

See how it works on other sites for comparison purposes. Most just show an empty list with a previous page button that goes to page-1. It's not your fault they put in some wonky number, or page=ham+sandwich .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM