Skip to content

Instantly share code, notes, and snippets.

@lav45
Last active June 7, 2019 11:58
Show Gist options
  • Save lav45/31f9744049c5bca13a46 to your computer and use it in GitHub Desktop.
Save lav45/31f9744049c5bca13a46 to your computer and use it in GitHub Desktop.
debug()
<?php
/**
* @link https://gist.github.com/lav45/31f9744049c5bca13a46
* @author Alexey Loban <lav451@gmail.com>
*
* Example:
* debug();
* debug($_POST);
* debug($_GET, $_POST, ...);
*
* Nginx:
* fastcgi_param PHP_VALUE "auto_prepend_file=/path_to/debug.php";
*
* Apache:
* php_value auto_prepend_file "/path_to/debug.php"
*
* Php CLI:
* mkdir -p ~/.php.d
* echo 'auto_prepend_file=/path_to/debug.php' >> ~/.php.d/php.ini
* echo 'export PHP_INI_SCAN_DIR=:~/.php.d' >> ~/.bashrc
*/
function debug()
{
// Install: https://chrome.google.com/webstore/detail/xdebug-helper/eadndfjplgieldjbigjakmdgkmoaaaoc
// if (empty($_COOKIE['XDEBUG_SESSION'])) return;
ob_get_level() && ob_clean();
$wrap = true;
switch (func_num_args()) {
case 0:
$res = (new \Exception())->getTraceAsString();
$wrap = false;
break;
case 1:
$res = func_get_arg(0);
break;
default:
$res = func_get_args();
break;
}
if ($wrap && !is_string($data)) {
$func = is_scalar($data) || null === $data ? 'var_export' : 'print_r';
$data = $func($data, true);
}
$isAjax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
$_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest' &&
$_SERVER['HTTP_CONTENT_TYPE'] === 'application/json';
$isHtml = isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'text/html') !== false;
if ($isHtml && !$isAjax) {
$res = '<pre>' . htmlspecialchars($res, ENT_IGNORE) . '</pre>';
}
exit($res);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment