Skip to content
← Back to Functions
Code

Environment Mismatch Detector

Detects mismatched host, protocol, config, database name, or debug mode.

Function signature

ogDetectEnvironmentMismatch(runtime = array(), expected = array())

Categories

  • Database Integrity

Parameters

runtimeRuntime environment values observed by caller-owned code.expectedExpected environment values configured for this deployment.

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

/**
 * Detects mismatched host, protocol, config, database name, or debug mode.
 *
 * Primary use case: Deployment audits.
 * Typical inputs: runtime environment, expected config.
 * Typical output: mismatch report.
 *
 * Implementation note: Redact secrets in reports.
 *
 * @param array $runtime Current runtime values.
 * @param array $expected Expected values.
 * @return array Environment mismatch report.
 */
function ogDetectEnvironmentMismatch($runtime = array(), $expected = array()) {
	$result = array(
		'success' => false,
		'message' => '',
		'data' => array()
	);

	if (!is_array($runtime)) {
		$result['message'] = 'Runtime values must be an array.';
		return $result;
	}
	if (!is_array($expected)) {
		$expected = array();
	}

	$mismatches = array();
	foreach ($expected as $key => $expected_value) {
		$key = (string)$key;
		if (preg_match('/password|secret|token|key/i', $key)) {
			continue;
		}
		$runtime_value = '';
		if (array_key_exists($key, $runtime)) {
			$runtime_value = $runtime[$key];
		}
		if ((string)$runtime_value !== (string)$expected_value) {
			$mismatches[] = array(
				'key' => $key,
				'expected' => (string)$expected_value,
				'actual' => (string)$runtime_value
			);
		}
	}

	$result['success'] = true;
	$result['message'] = 'Environment mismatch check completed.';
	$result['data'] = array('mismatches' => $mismatches, 'count' => count($mismatches));
	return $result;
}