Forms: GET and POST

Содержание
Read Array
Read Array
Validate input
Escape output with htmlspecialchars

Read $_GET

Data in the query string lands in the $_GET array. Use it for search, filters, and pagination where the URL should stay shareable. See also How to add variable to url in PHP.

Always assume the key may be missing and provide a default.

$q = $_GET["q"] ?? ""; $page = (int)($_GET["page"] ?? 1); echo $q; echo $page;

Read $_POST

Form submissions with method post fill the $_POST array. Use it for logins, comments, and anything that changes data on the server.

Never place passwords or tokens into the query string when post is available.

$email = $_POST["email"] ?? ""; $message = $_POST["message"] ?? ""; echo $email; echo $message;

Validate input

Input from the browser is plain text and cannot be trusted. Check length, format, and allowed values before using it in queries or business rules.

Reject bad data early with a clear message instead of trying to repair it silently.

$email = trim($_POST["email"] ?? ""); if ($email === "" || strlen($email) > 255) { echo "Email is required"; } elseif (filter_var($email, FILTER_VALIDATE_EMAIL) === false) { echo "Email looks wrong"; } else { echo "Email is ok"; }

Escape output with htmlspecialchars

When printing user data back into HTML, escape it with htmlspecialchars. Without escaping, a value with angle brackets can break markup or inject scripts.

Escape at output time, not at input time, so the stored value stays clean and reusable.

$name = $_GET["name"] ?? "guest"; echo htmlspecialchars($name, ENT_QUOTES, "UTF-8");

Next: HTTP: headers and status codes

Автор статьи: Arthur Isaev

Похожие статьи
Программирование на PHP
PHP course: zero to hero in 50 lessons
What is PHP and where it runs
Install PHP 8.5 and run your first command
Your first PHP script
Comments in PHP
Variables and constants
Types in PHP
Strings in PHP
Numbers in PHP
Operators
if and else
switch and match
Ternary, null coalescing and nullsafe
Loops
Arrays
Associative arrays
Functions
Variable scope
include and require
Namespaces
Composer and autoloading
Everyday string functions
Dates and time
JSON in PHP
Reading and writing files
Errors: read, reproduce, fix
Classes and objects
Constructors and promotion
Inheritance
Interfaces and traits
Enums
Magic methods
Attributes
Exceptions
Strict types
Closures and arrow functions
Generators with yield
Forms: GET and POST
HTTP: headers and status codes
Cookies
Sessions
Login with sessions
JSON REST endpoint
Database with PDO
Uploads and .htaccess
Lint like a pro
Clean functions
Debugging
Modern PHP tour: 5 to 8.4
PHP 8.5 for busy developers
Hero roadmap: the whole course on one page
Изображение баннера

Поиск по сайту

Канал @aofeed Чат @aofeedchat

Контакты и сотрудничество:
Рекомендую наш хостинг beget.ru
Пишите на info@urn.su если Вы:
1. Хотите написать статью для нашего сайта или перевести статью на свой родной язык.
2. Хотите разместить на сайте рекламу, подходящую по тематике.
3. Реклама на моём сайте имеет максимальный уровень цензуры. Если Вы увидели рекламный блок недопустимый для просмотра детьми школьного возраста, вызывающий шок или вводящий в заблуждение - пожалуйста свяжитесь с нами по электронной почте
4. Нашли на сайте ошибку, неточности, баг и т.д. ... .......
5. Статьи можно расшарить в соцсетях, нажав на иконку сети: