Safe Debug Context Redactor
Redacts secrets, tokens, passwords, cookies, and personal identifiers from debug context arrays.
Purpose
Redacts secrets, tokens, passwords, cookies, and personal identifiers from debug context arrays.
Snippet details
ContextSecurityLevelAdvancedCopy-and-paste statusMarked safe after review.Categories
- Security
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.
*/
/**
* Safe Debug Context Redactor.
*
* Purpose:
* Redacts secrets, tokens, passwords, cookies, and personal identifiers from debug context arrays.
*
* @param array $context Debug context array.
* @return array Redacted context.
*/
function ogSnippetSafeDebugContextRedactor(array $context): array {
$redacted = array();
$sensitive_words = array('password', 'token', 'secret', 'cookie', 'authorization', 'email');
foreach ($context as $key => $value) {
$key_text = strtolower((string) $key);
$should_redact = false;
foreach ($sensitive_words as $sensitive_word) {
if (strpos($key_text, $sensitive_word) !== false) {
$should_redact = true;
}
}
if ($should_redact === true) {
$redacted[$key] = '[redacted]';
} elseif (is_array($value)) {
$redacted[$key] = ogSnippetSafeDebugContextRedactor($value);
} else {
$redacted[$key] = $value;
}
}
return $redacted;
}
$debug_context = array('pilot' => 'Starbuck', 'session_token' => 'abc123', 'nested' => array('email' => 'pilot@example.com'));
$clean_debug = ogSnippetSafeDebugContextRedactor($debug_context);
echo $clean_debug['session_token'];