Skip to content
← Back to Snippets
Code

Avoid Request Shortcuts with Explicit GET and POST Intake

Shows separate explicit $_GET and $_POST intake branches without using $_REQUEST.

Purpose

Shows separate explicit $_GET and $_POST intake branches without using $_REQUEST.

Snippet details

ContextRequestLevelProductionCopy-and-paste statusMarked safe after review.

Categories

  • Forms and Validation

Code

<?php

/*
 * Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
 * Free for personal and internal use. Paid project use requires visible credit
 * to Jeffery L. Paris. Corporate use requires a paid license fee unless a
 * separate written license states otherwise.
 */

/**
 * Avoid Request Shortcuts with Explicit GET and POST Intake.
 *
 * Purpose:
 * Separates read-only query intake from state-changing submitted intake.
 *
 * @param string $key Request key to read.
 * @param string $default Default value.
 * @return array Explicit intake result.
 */
function ogSnippetAvoidRequestShortcutsExplicitGetAndPostIntake(string $key, string $default): array {
	$key = trim($key);
	$result = array(
		'source' => 'default',
		'value' => $default
	);

	if ($key === '') {
		return $result;
	}

	if (isset($_POST[$key]) === true) {
		$result['source'] = 'post';
		$result['value'] = trim((string) $_POST[$key]);
		return $result;
	}

	if (isset($_GET[$key]) === true) {
		$result['source'] = 'get';
		$result['value'] = trim((string) $_GET[$key]);
	}

	return $result;
}

$intake = ogSnippetAvoidRequestShortcutsExplicitGetAndPostIntake('mission', 'explore');

echo 'Request source: '.$intake['source'];