If https://www.google.com/search?q=cs50 submits form data using GET to search, what is search?
search is just a path that Google's web servers use as an entry point into the search engine. Behind the scenes the web server will redirect the request to a specific server instance, chosen from a cloud of servers, to handle the request. So
search doesn't exist as an actual script like
index.php, but is a virtual location that server as a doorway into the rest of the system. More information on how Google works is available here.
This isn't just for Google though. Web servers are often configured to provide access through clean URLs like this, which are re-mapped by the web server to the actual programs that handle the request. Re-mapping has many benefits including:
- The URLs are still valid even if the underlying script language changes, say from .php to .asp.
- It's easier to balance traffic amongst multiple servers.
- The URL is more user friendly.
Anyway, in general, is it possible to do something like this -- to have a page named sample.php, for example, that renders a form initially whose data is submitted using GET to sample.php itself (the URL becomes sample.php?var=val) which in turn handles this request?
Yes, this is definitely possible, and used often. There are a few ways to accomplish this.
First to address the question of checking the
REQUEST_METHOD: The value of
$_SERVER["REQUEST_METHOD"] is determined by the HTTP method passed by the browser when accessing the the web page. The default method is
GET. So if you access a web page by entering the URL, or clicking on a link, the browser will use a
GET request method.
One way to specify the request method, is by setting the
method parameter in an HTML
<form> tag. For example:
<form action="sample.php" method="POST">
This will instead pass the request to the server using the
POST method. One side-effect of this is that you will not see the form data being appended to the URL, as it will be passed in the HTTP request body.
So one solution to the question might be to use a
POST method in the form, and check for it's usage in
That said, there are subtle semantics attached to the different request methods, which are intended to control things like caching. For example
GET should be used if the request returns data,
POST should only be used if the request actually modifies data on the server.
DELETE are other available alternatives, although these are not as commonly supported by browsers and servers.
How to Correctly Submit Form Data Using GET?
A better solution than using the
REQUEST_METHOD would be to rather inspect the presence or values of variables passed to the script, and alter the output depending on the information available at the time.
In your scenario,
sample.php is either called with
var=val or without it. You could check for the presence of this variable. If the variable is not provided, then render the one template (e.g. an input form), but if the variable is provided, then render the other template (e.g. a list of results from the input).
One way to check for this is using isset() and empty(), e.g.
// q is provided, but contains an empty string, render apology
// q is provided, render search result
// q is not provided, render input form
$_REQUEST is used here, is that it will contain the value
q regardless of the request method used. You can use
$_POST instead if you only expect to handle a specific request method.