PHP GET and POST Methods
# Chapter 13: PHP GET and POST Methods
1. Introduction
Welcome to Chapter 13! In the previous chapter, we usedmethod="POST" to send our contact form data securely to the server. But forms can also use method="GET". These two HTTP methods dictate how data is packaged and sent from the browser to your PHP application. Choosing the wrong method can result in massive security flaws or broken website functionality. In this chapter, we will master the difference between GET and POST, learn how to use URL parameters, and build a functioning search system.
2. Learning Objectives
By the end of this chapter, you will be able to:- Explain the technical differences between GET and POST requests.
-
Retrieve data from the URL using the
$_GETsuperglobal array.
- Understand when it is appropriate to use GET (Searching/Filtering).
- Understand when it is mandatory to use POST (Logins, Databases).
- Pass variables between pages using URL Query Strings.
3. The POST Method (Review)
When a form usesmethod="POST", the browser packages the data into the HTTP request "body".
- The data is invisible to the user.
- It does not appear in the URL.
- There is no size limit (you can upload 50MB video files).
- Used for sensitive data (passwords) and actions that change data on the server (creating an account, updating a profile).
4. The GET Method
When a form usesmethod="GET", the browser takes the form data and appends it directly to the URL in the address bar. This is called a Query String.
If the user types laptop and clicks submit, the browser redirects to:
search.php?query=laptop
5. Capturing GET Data
Insidesearch.php, you use the $_GET array to capture the data from the URL.
6. URL Parameters (Query Strings)
You don't even need a form to use GET data! You can create standard HTML links that pass data to the next page. A query string starts with a?. Multiple variables are separated by an &.
profile.php?user_id=45&theme=dark
7. Real-World Examples
When to use GET:-
Search bars (Users can bookmark the search results page:
amazon.com/search?q=shoes).
-
Filtering products (
shop.php?category=shirts&size=L).
-
Pagination (
blog.php?page=3).
When to use POST:
-
Login forms (You NEVER want
login.php?password=mySecretshowing in browser history!).
- Checkout systems (Processing credit cards).
- Uploading profile pictures.
8. Output Explanations
When clicking a link like<a href="delete.php?id=10">Delete</a>, the browser requests delete.php. PHP sees ?id=10 in the URL, creates $GET['id'] with the value 10. However, using a GET link to *delete* data is a terrible practice. A web crawler (like Googlebot) following links on your site might accidentally click all your delete links! Deletions should always be POST requests.
9. Common Mistakes
-
Passwords in GET: Creating a login form with
method="GET". The user's password will be saved in plain text in their browser history and server logs.
-
Assuming GET data is safe: Data in the URL can be edited by ANYONE manually typing in the address bar. Always validate and
htmlspecialchars()GET data just like POST data.
-
Undefined Array Keys: Echoing
$GET['id']when the user visited the page normally without clicking a link (so?id=is not in the URL). Always useisset($_GET['id'])first.
10. Best Practices
- Rule of Thumb: If the request just *retrieves* information (search, read), use GET. If the request *changes* information (create, update, delete), use POST.
- If using GET for database queries, you must use Prepared Statements to prevent SQL injection (we will learn this later).
11. Exercises
-
1.
Create a link
<a href="welcome.php?name=YourName">Click Me</a>.
-
2.
On
welcome.php, write a PHP script that checks ifnameis set in the URL, and if so, echoes a welcome message.
12. Mini Project: Search Form System
Task: Build a single-page fake product database. Let the user search for a product using a GET form, and display the result based on the URL query.13. Coding Challenges
Challenge 1: Create an HTML list of three categories: Electronics, Clothing, Books. Make each list item a hyperlink pointing tocategory.php?cat=Name. On category.php, securely echo "You are viewing the [Name] category."
14. MCQs with Answers
1. Which HTTP method appends form data to the URL? A) POST B) GET C) PUSH D) APPEND *Answer: B*2. Why must sensitive data (like passwords) be sent via POST? A) POST is encrypted by default. B) GET is slower than POST. C) GET appends data to the URL, meaning passwords would be visible on screen, in browser history, and server logs. D) PHP cannot read passwords sent via GET. *Answer: C*
3. What symbol starts a query string in a URL?
A) &
B) #
C) ?
D) =
*Answer: C*
15. Interview Questions
Q: Explain the primary differences between GET and POST. *A:* GET sends data in the URL, has a size limit, is bookmarkable, and is meant for retrieving data without changing server state. POST sends data in the invisible HTTP body, has no size limit, cannot be bookmarked, and is meant for sensitive data or actions that modify server state (like database updates).Q: Why shouldn't you use GET requests to delete data?
*A:* Because GET requests are meant to be "safe" and idempotent (meaning calling them multiple times doesn't change the result). If you have a link <a href="delete.php?id=5">, search engine crawlers or browser pre-fetching tools might automatically "visit" that link, accidentally deleting data from your database. Deletions must use POST requests requiring active user submission.