Created
September 24, 2015 20:48
-
-
Save aanton/59f55b3801317daeb9d5 to your computer and use it in GitHub Desktop.
PHP example to debug requests done in ISO-8859-1 & UTF-8 pages
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 | |
$debug = []; | |
// Configure charset | |
$charsets = ['utf-8', 'iso-8859-1']; | |
$charset = array_key_exists('charset', $_REQUEST) && | |
in_array(mb_strtolower($_REQUEST['charset']), $charsets) ? mb_strtolower($_REQUEST['charset']) : 'utf-8'; | |
ini_set('default_charset', $charset); | |
mb_internal_encoding(ini_get('default_charset')); | |
// Request parameters | |
$isAjax = array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER) && | |
(strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'); | |
$requestType = !empty($_GET) ? 'GET' : (!empty($_POST) ? 'POST' : 'N/A'); | |
$text = $_REQUEST['text']; | |
if ($charset === 'iso-8859-1' && $isAjax) { | |
// Using trial & error (to get the same response as non-ajax requests) | |
// In Manual URLs, only works when using encodeURIComponent (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) | |
$text = mb_convert_encoding($text, 'HTML-ENTITIES', 'UTF-8'); | |
$text = html_entity_decode($text, ENT_COMPAT, 'CP1252'); | |
} | |
if (!empty($text)) { | |
$debug[] = 'charset: ' . $charset . ' request: ' . $requestType . ' ajax: ' . ($isAjax ? 'true' : 'false'); | |
$debug[] = 'text: ' . $text; | |
$debug[] = 'mb_strlen: ' . mb_strlen($text) . ' strlen: ' . strlen($text) . PHP_EOL; | |
} | |
// Ajax request | |
if ($isAjax) { | |
echo join(PHP_EOL, $debug); | |
die(); | |
} | |
// Uncomment if necessary | |
// $textHtmlized = htmlspecialchars($text, ENT_COMPAT, $charset); | |
$textHtmlized = $text; | |
?> | |
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="<?php echo $charset ?>"> | |
<title>Test INPUT</title> | |
</head> | |
<body> | |
<h1>TEST INPUT (charset: <?php echo $charset; ?>, request: <?php echo $requestType; ?>)</h1> | |
<div> | |
<a href="debug-charsets-requests.php?charset=utf-8">Reset UTF-8</a> | | |
<a href="debug-charsets-requests.php?charset=iso-8859-1">Reset ISO-8859-1</a> | |
</div> | |
<h2>Example data</h2> | |
<!-- space 1-ñ-€-Ⓜ-東-🐵-𣇵-😮-📷-2 a&b end! --> | |
<div><span class="ui-example">space 1-ñ-€-Ⓜ-東-🐵-𣇵-😮-📷-2 a&b end!</span> <a href="#" class="ui-copy">copy</a></div> | |
<h2>Test GET/POST</h2> | |
<form action="debug-charsets-requests.php" method="get"> | |
<input type="text" name="text" value="<?php echo $textHtmlized; ?>" size="50" class="ui-input-text" /> | |
<input type="hidden" name="charset" value="<?php echo $charset; ?>" /> | |
<br /> | |
<input type="button" value="Submit GET" class="ui-request-get ui-request-form" /> | |
<input type="button" value="Submit POST" class="ui-request-post ui-request-form" /> | |
<br /> | |
<input type="button" value="Ajax GET" class="ui-request-get ui-request-ajax" /> | |
<input type="button" value="Ajax POST" class="ui-request-post ui-request-ajax" /> | |
<br /> | |
<input type="button" value="Ajax URL (manual) WRONG" class="ui-request-get ui-request-url ui-url-manual" /> | |
<input type="button" value="Ajax URL (escape) WRONG" class="ui-request-get ui-request-url ui-url-escape" /> | |
<input type="button" value="Ajax URL (encodeURIComponent) OK" class="ui-request-get ui-request-url ui-url-encode" /> | |
</form> | |
<h2>DEBUG</h2> | |
<textarea rows="8" cols="120" class="ui-debug" disabled> | |
<?php echo join(PHP_EOL, $debug); ?> | |
</textarea> | |
<pre class="ui-debug-pre"> | |
<?php echo str_replace(['&', '<', '>'], ['&', '<', '>'], join(PHP_EOL, $debug)); ?> | |
</pre> | |
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.3.min.js"></script> | |
<script type="text/javascript"> | |
$(function() { | |
var $form = $('form'); | |
var $input = $form.find('input[name=text]'); | |
$('.ui-copy').on('click', function(e) { | |
e.preventDefault(); | |
$input.val($('.ui-example').text()); | |
}); | |
$('input[type=button]').on('click', function(e) { | |
var $button = $(this); | |
var charset = $form.find('input[name=charset]').val(); | |
var isAjax = $button.is('.ui-request-ajax') || $button.is('.ui-request-url'); | |
var isGet = $button.is('.ui-request-get'); | |
var isAjaxUrl = $button.is('.ui-request-url'); | |
var text = $input.val(); | |
if (!isAjax) { | |
$form.attr('method', isGet ? 'GET' : 'POST'); | |
$form.submit(); | |
} else { | |
var url = 'debug-charsets-requests.php'; | |
var method = isGet ? 'GET' : 'POST'; | |
if ($button.is('.ui-url-escape')) { | |
text = escape(text); | |
} else if ($button.is('.ui-url-encode')) { | |
text = encodeURIComponent(text); | |
} | |
var data = {'text': text, 'charset': charset}; | |
if (isAjaxUrl) { | |
url += '?text=' + text + '&charset=' + charset; | |
data = {}; | |
} | |
debug(JSON.stringify({'url': url, 'method': method, 'data': data}), true); | |
$.ajax({ | |
url: url, | |
type: method, | |
data: data, | |
success: function (data, status) | |
{ | |
debug(data, false); | |
}, | |
error: function (xhr, desc, err) | |
{ | |
alert(xhr); | |
alert('Details: ' + desc + '\nError:' + err); | |
} | |
}); | |
} | |
}); | |
function debug(data, isRequest) { | |
var $debug = $('.ui-debug'); | |
if (isRequest) { | |
$debug.val('REQUEST:\n' + data + '\n'); | |
} else { | |
$debug.val($debug.val() + 'RESPONSE:\n' + data + '\n'); | |
} | |
$('.ui-debug-pre').html($debug.val().replace(/&/g, '&').replace(/\</g, '<').replace(/\>/g, '>')); | |
} | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment