Skip to content
← Back to Functions
Code

Admin Flash Message Queue

Stores one-time admin flash messages by type and scope.

Function signature

ogQueueAdminFlashMessage(message, type = 'info', scope = 'admin')

Categories

  • Sessions and Authentication

Parameters

messageHuman-facing admin message text after the caller removes secrets and raw traces.typeFlash message type such as info, success, warning, or error.scopeFlash message scope used by the caller to separate admin/UI contexts.

Return value

Public-safe status string returned by the function for explicit controller branching or logging.

  • success
  • message
  • data

Compatibility

Existing function name, slug, path, and call order preserved; advertised metadata corrected to the actual source behavior.

Minimum PHP version: 7.4

Security notes

Use caller-owned allowlists and context-specific escaping; validate admin actions, export fields, privacy plans, cache keys, templates, settings, routes, and ecommerce policies before production use.

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.
 */

/**
 * Builds a one-time admin flash message payload for post-redirect-get flows.
 *
 * The message is returned as data for the caller to store in $_SESSION or another
 * approved flash-message store. Output escaping must happen when rendering.
 *
 * @param string $message Message text to queue.
 * @param string $type Message type such as success, warning, error, or info.
 * @param string $scope Optional scope key for the destination page.
 * @return array Flash message payload.
 */
function ogQueueAdminFlashMessage($message, $type = 'info', $scope = 'admin') {
	$result = array(
		'success' => false,
		'message' => '',
		'data' => array()
	);

	$message = trim((string)$message);
	$type = strtolower(trim((string)$type));
	$scope = trim((string)$scope);

	$allowed_types = array('success', 'warning', 'error', 'info');
	if (!in_array($type, $allowed_types, true)) {
		$type = 'info';
	}

	if (empty($scope)) {
		$scope = 'admin';
	}

	if (empty($message)) {
		$result['message'] = 'Flash message text is required.';
		return $result;
	}

	if (strlen($message) > 500) {
		$message = substr($message, 0, 500);
	}

	$flash = array(
		'id' => bin2hex(random_bytes(8)),
		'type' => $type,
		'scope' => $scope,
		'message' => $message,
		'created_at' => time()
	);

	$result['success'] = true;
	$result['message'] = 'Flash message prepared.';
	$result['data'] = array('flash_message' => $flash);

	return $result;
}