2

If https://www.google.com/search?q=cs50 submits form data using GET to search, what is search? What is its extension? Is it possible to visit this page without passing the parameter called q? (well, I tried that already and looks like Google sends me to somewhere else).

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?

It's worth mentioning that I tried to write something like

if ($_SERVER["REQUEST_METHOD"] == "GET")
{
    // do something
}
else
{
    // render the form
}

and the if statement always executes ($_SERVER["REQUEST_METHOD"] == "GET" is always true) so the form never gets rendered.

I also thought about submitting the data to another page, but I wanted to know if what I mentioned earlier is possible.

1 Answer 1

2

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:

  1. The URLs are still valid even if the underlying script language changes, say from .php to .asp.
  2. It's easier to balance traffic amongst multiple servers.
  3. 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 REQUEST_METHOD.

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. PUT, HEAD, and 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.

if (isset($_REQUEST['q'])) 
{
    if (empty($_REQUEST['q'])) 
    {
        // q is provided, but contains an empty string, render apology
    }
    else
    {
        // q is provided, render search result
    }
}
else 
{
    // q is not provided, render input form
}

The reason $_REQUEST is used here, is that it will contain the value q regardless of the request method used. You can use $_GET or $_POST instead if you only expect to handle a specific request method.

2
  • 1
    Perfect answer as usual except that the function that should be used to check for the existence of the parameter named q, in your example, should be isset() rather than empty() because q can be provided, but empty. In this case, I want to apologize instead of re-rendering the form.
    – kzidane
    Commented Aug 10, 2014 at 18:52
  • 1
    @Kareem Thanks for point that out. Updated the answer to cater for both scenarios, just in case it turns out to be useful to other readers. Commented Aug 10, 2014 at 22:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .