Last active
June 7, 2019 11:58
-
-
Save lav45/31f9744049c5bca13a46 to your computer and use it in GitHub Desktop.
debug()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @link https://gist.github.com/lav45/31f9744049c5bca13a46 | |
* @author Alexey Loban <[email protected]> | |
* | |
* 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