0

I plan to make a query to a php file on my server as follows:

example.com/download.php?value=7

If this value meets a certain condition, the server should respond with actual content. If the value fails this condition, I want the server to respond with some type of 'null' value. Would the best practice be to create an empty temporary file and return this, or is there a more 'standard' way to respond there is nothing to download?

Daiwik Daarun
  • 3,804
  • 7
  • 33
  • 60

2 Answers2

1

The best response in this case is to have the server return a 404 error. From the spec:

The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent. The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address. This status code is commonly used when the server does not wish to reveal exactly why the request has been refused, or when no other response is applicable. 

The best way to do that can be found at How can I create an error 404 in PHP?

Matthew B
  • 84
  • 8
1

The best practice in this case is to return a meaningful HTTP response code. You can choose a 404 NOT FOUND, in case the request is for a missing file or resource, or 400 BAD REQUEST, in case the request is not in the correct format. Check out this tutorial for more RESTful best practices in PHP.

Stiliyan
  • 1,154
  • 1
  • 9
  • 15