We will explain the GET method and the POST method, which form the foundation of HTTP communication (the rules for data exchange between a Web server and a Web browser) and are extremely important for learning the basics of web development.


💡 What are POST and GET? Understanding their Roles in Web Communication

Behind the operations you perform daily on the internet, such as viewing websites or submitting data via forms, data exchange constantly occurs between your web browser (e.g., Chrome, Safari) and the web server. Determining **how** this data is sent during this communication is the role of the two main HTTP methods (means of communication): the **GET Method** and the **POST Method**.

These methods play an indispensable role in performing essential processes in web development, such as “data retrieval” and “data submission.”

📄 Fundamental Concepts of HTTP Methods (GET/POST)

HTTP (HyperText Transfer Protocol) methods are like **verbs** that tell the server what the client (web browser) wants to do. Besides GET and POST, there are others like **PUT** (to update data) and **DELETE** (to remove data), but the two methods beginners in web development should understand first are GET and POST.

  • **GET Method**: Primarily used when you want to retrieve (fetch or view) information from a web server. For example, it is used to display a website’s homepage or show search results.
  • **POST Method**: Primarily used when you want to send (register, create, or update) information to a web server. For example, it is used when submitting data entered in a contact form or sending login information.

🔎 What are the “Differences” Between POST and GET? Data Submission Methods and Security

Both GET and POST are used for data transmission, but they have significant differences in terms of **data submission methods** and **security**. Understanding this difference is critical for practical web development.

🔗 Differences in Data Submission Methods: URL vs. Request Body

The biggest difference between GET and POST is where the data is stored and submitted.

Features**GET Method****POST Method**
Data Submission LocationEnd of URL (Query Parameters)**HTTP Request Body** (Main part)
**Size Limit for Submitted Data****Yes** (limited by URL length)**Virtually None** (depends on server settings)
**Bookmarkability****Possible** (because data is included in the URL)**Not Possible**
**Data Visibility****High** (visible in the URL)**Low** (usually not visible)

Query parameters are data in the key=value format that follow the “?” at the end of a URL.

In the case of https://example.com/search?q=Web Development, the part “q=Web Development” is the query parameter.

The **Request Body** is the “main part” of the HTTP request, which is typically invisible to the user. In the POST method, form input data and similar information are stored and submitted here.

🔒 Differences in Security and Confidentiality

  • **GET Method Security**:
    • Since data is **displayed directly in the URL**, it **must never be used** for submitting **highly confidential data** such as passwords.
    • There is a risk that the URL may remain in history, be recorded in logs, or be easily seen by others.
  • **POST Method Security**:
    • Data is **stored in the request body** and not displayed in the URL. For this reason, it is suitable for submitting **highly confidential data** (such as login credentials or credit card information).
    • However, even with the POST method, if the communication itself is not encrypted (**HTTP communication**, i.e., URLs starting with http://), there is a risk of eavesdropping on the communication path. Using the safer **HTTPS communication** (i.e., URLs starting with https://) is essential in web development.

  • 💻 URL and $_GET: Mechanism and Usage for Receiving Data with the GET Method (PHP)

    Data sent from a web browser via the GET method can be received in PHP through the $_GET superglobal variable (a special variable accessible from anywhere).

    🌟 How $_GET Works

    In the GET method, data is stored in the URL’s query parameters (the part after ?). PHP’s $_GET automatically holds these query parameter values as an **associative array** (an array that holds data in key-value pairs).

    📝 Practical Code Example of the GET Method

    In the case where the URL is https://example.com/item.php?id=100&color=red, the contents of $_GET available on the PHP side will be as follows:

    // Contents of $_GET
    [
        'id' => '100',
        'color' => 'red'
    ]
    

    Here is an example where the file item.php receives the product ID (id) from the URL and displays the product information.

    <?php
        // Receive GET data submitted by the user
        // Check if the specified key ('id') exists using the isset() function
        if (isset($_GET['id'])) {
            $item_id = htmlspecialchars($_GET['id']); // Sanitize for XSS prevention
            
            echo "## Product Details (ID: {$item_id})";
            echo "<p>This is the product page for ID: {$item_id}.</p>";
            
            // In actual development, this ID is used to fetch product information from the database
            
        } else {
            echo "<p>No product ID has been specified to display.</p>";
        }
    ?>
    

    Key Point: It is fundamental practice to **sanitize** data received (e.g., $_GET['id']) using the htmlspecialchars() function before using it, as displaying it directly on the screen can lead to a security vulnerability known as XSS (**Cross-Site Scripting**).


    📥 Forms and $_POST: How to Securely Submit Data with the POST Method (PHP)

    The **POST Method** is primarily used to submit data to a server using HTML **forms**. The submitted data is received in PHP through the $_POST superglobal variable.

    🌟 How $_POST Works

    When method="post" is specified in the HTML <form> tag and a button is clicked, the input data is stored in the HTTP request body and sent to the server. PHP’s $_POST holds this form data from the body as an **associative array**.

    📝 Practical Code Example of the POST Method

    Here is an example of creating a contact form and receiving the submitted name (user_name) and message (message).

    1. Creating the HTML Form (contact_form.html)

    <form action="contact_receive.php" method="post">
        <label for="name">Your Name:</label>
        <input type="text" id="name" name="user_name" required><br><br>
        
        <label for="msg">Message:</label>
        <textarea id="msg" name="message" required></textarea><br><br>
        
        <input type="submit" value="Submit">
    </form>
    

    2. Receiving Data in PHP (contact_receive.php)

    <?php
        // Check if data was submitted via POST
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            
            // Check if data exists with isset() and sanitize with htmlspecialchars()
            $user_name = isset($_POST['user_name']) ? htmlspecialchars($_POST['user_name']) : 'Not entered';
            $message = isset($_POST['message']) ? htmlspecialchars($_POST['message']) : 'Not entered';
    
            echo "## Submission Complete";
            echo "<p>Your Name: {$user_name}</p>";
            echo "<p>Message:</p>";
            echo "<p>{$message}</p>";
            
            // In actual development, data saving to the database would follow
            
        } else {
            echo "<p>This is not a proper access from the form.</p>";
        }
    ?>
    

    Key Point: When submitting a form via the POST method, checking whether the request was truly made with the POST method using $_SERVER['REQUEST_METHOD'] is useful because it distinguishes it from direct URL access (the GET method).


    ⚖️ POST vs. GET “Proper Use”: When to Choose Which

    When unsure whether to use GET or POST in web development, the decision becomes clear by considering whether the operation involves “data retrieval/display” or “data registration/modification.”

    🛒 Cases Where You Should Choose the GET Method

    Used for operations that only involve data retrieval and display, and do not **change the content of data** on the server (i.e., have no side effects).

    • **Web Pages Viewing**: Displaying the homepage, list pages, or detail pages.
    • **Search**: Submitting search keywords and receiving a list of results.
    • **Filtering/Sorting**: Narrowing down or reordering data.

    📝 Cases Where You Should Choose the POST Method

    Used for operations that **change or create data content** on the server, or for submitting **highly confidential information**.

    • **Form Submission**: Contact inquiries, user registration, login, product purchase procedures.
    • **New Data Creation**: Posting to a bulletin board, publishing a blog article.
    • **Data Update/Deletion**: Editing a profile, deleting a comment (while **PUT/DELETE** may be used for these operations, POST is generally common for web forms).

    Important Principle (Idempotence): The GET method is ideally used for operations where “the result does not change no matter how many times it is executed (idempotence).” The POST method is used for operations that are not idempotent, as executing it repeatedly may create new data (e.g., duplicate registration in the database).


    Key Considerations When Using POST and GET in PHP: Security Measures are Mandatory

    We will explain the security points regarding the use of GET and POST that web development beginners should pay particular attention to.

    1. Thoroughly Sanitize Input Values

    User input data received from $_GET or $_POST **must** always be sanitized using functions like htmlspecialchars() before being displayed on the screen.

    • Purpose: This is to prevent an attack called XSS (Cross-Site Scripting). If malicious script (program code) is entered, displaying it without sanitization can potentially harm the viewer.

    2. Use POST and HTTPS for Confidential Information

    When submitting confidential information such as passwords, personal data, or payment details, you must adhere to the following two points.

    • **Use the POST Method**: Never use the GET method, which displays information in the URL.
    • **Use HTTPS communication**: Use **HTTPS** (https://), where communication is encrypted by TLS/SSL, instead of HTTP, to eliminate the risk of eavesdropping on the communication path.

    3. Implement CSRF Measures for POST Submissions

    Measures against an attack called CSRF (Cross-Site Request Forgery) are mandatory for form submissions using the POST method.

    • Attack Summary: This attack forces the server to execute operations unintended by the user (e.g., unauthorized product purchases or withdrawal procedures) via a malicious website.
    • Mitigation: Embed a **CSRF token** (a disposable, secret string issued with every form submission) into the form and have the server verify if the token is correct to block unauthorized requests.

    These security measures are the absolute fundamentals that a web developer must never neglect to ensure users can safely use the service.


    Summary

    In this article, we explained the basics of the GET and POST methods, which are the foundation of web communication, their differences, and their specific usage in PHP.

    Method**Role****Data Submission Location****Suitable Use****Security**
    **GET****Retrieval/Viewing** of Data**URL Query Parameters**Page display, search, filtering**Unsuitable** for confidential info
    **POST****Submission/Creation/Update** of Data**HTTP Request Body**Form submission, login, data registration**Suitable** for confidential info (HTTPS mandatory)

    Mastering the roles and proper use of these two methods is a major step in learning web development.