Skip to main content

GET vs POST

Understanding PHP Form Handling: GET vs POST

When dealing with PHP forms, two primary methods are used to send data - GET and POST. These methods, also known as HTTP methods, are integral to understand when learning PHP form handling. Let's delve into what each means and the differences between them.

The GET Method

The GET method sends encoded user data appended to the page request. The information is visible in the URL, which means it's very easy to see and edit by the user. This method is ideal when the amount of data to be transferred is small and doesn't contain sensitive information such as passwords.

Here's an example of a form using GET method:

<form action="welcome_get.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

In the above example, when the user clicks the 'Submit' button, the form data is sent to 'welcome_get.php' to be processed. The form data is visible in the page address like http://www.example.com/welcome_get.php?name=John&[email protected].

The POST Method

The POST method transfers data inside the body of the HTTP request, which means it isn't visible in the URL. This method has no size limitations, and it can be used to send large amounts of data. POST is a safer and more secure way of transferring data than GET as the parameters are not stored in browser history or in web server logs.

Here's an example of a form using POST method:

<form action="welcome_post.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

In the above example, the form data is sent to 'welcome_post.php' to be processed when the user clicks the 'Submit' button. The form data is not visible in the page address.

Differences Between GET and POST

Here are the key differences between GET and POST:

  1. Data Visibility: GET method data is appended to the URL and is visible in the browser's address bar, while POST method data is sent in the body of the HTTP request and is not visible in the address bar.

  2. Data Size: GET is limited to the maximum URL length (about 2K characters), while POST has no size limitations.

  3. Data Type: GET only allows ASCII data, while POST has no restrictions, binary data is also allowed.

  4. Security: GET is less secure compared to POST because data is visible in the URL and is stored in browser history. POST is a better method when sending sensitive data.

  5. Form Resubmission: If a GET request is refreshed, it won't prompt any alert message. A POST request, on the other hand, will prompt an alert message before a form is re-submitted.

  6. Bookmarking: You can bookmark a GET request, while you can't bookmark a POST request.

In summary, GET and POST methods in PHP form handling serve different purposes. The choice between using GET or POST depends on the specific requirements of your application.