为了限制用户可以输入的文本区域中的字符,我在上面放了一个计数器。
// this is part of tinymce
setup: function (ed) {
ed.on('keyup', function (e) {
var maxchars = <?php echo $max_chars_allowed; ?>; // determined in settings
var countchars = CountCharacters();
var charsleft = maxchars - countchars;
和函数 CountCharacters():
// counting characters textarea
function CountCharacters() {
var body = tinymce.activeEditor.getBody();
var content = tinymce.trim(body.innerText || body.textContent);
return content.length;
};
现在我通过 ajax 将数据发送到 php 文件:
// count chars input
var countchars = CountCharacters();
$.ajax({
url: 'comment.php',
type: 'POST',
data: { countchars:countchars }
...
要检查comment.php 中的数据:
$countchars = $_POST['countchars']; // countchars read from ajax
$max_chars_allowed = 200;
if( $countchars > $max_chars_allowed ) {
echo 'Max allowed characters exceeded. Reduce characters and submit again';
......
这一切都很好,但我的主要问题是:这样做安全吗?也许有人可以通过浏览器检查器操作 js var countchars并将其设置为 100,而实际上他在 textarea 中键入了 300 个字符?然后提交后,ajax 将 100 个字符发送到 php 文件,而不是他在 textarea 中键入的 300 个字符,并且 php 中的安全循环将被绕过......
慕沐林林
相关分类