Last active
February 24, 2023 10:17
-
-
Save Isa3v/1a17ea2439dccada4ae1d97ea76c35fa to your computer and use it in GitHub Desktop.
Защищаем веб-формы битрикса от примитивных ботов (Скрытая капча)
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
<? | |
\Bitrix\Main\EventManager::getInstance()->addEventHandler("main", "OnEndBufferContent", "OnEndBufferWebFormAntiSpam"); | |
function OnEndBufferWebFormAntiSpam(&$content) | |
{ | |
// Добавляем стили перед head | |
$xStyle = '<style>.xname{display:block;height:.1px;margin:0!important;overflow:hidden;padding:0!important;width:.1px;border:0;opacity:.01;}</style>'; // Стили для поля | |
$content = str_ireplace('</head>', $xStyle.'</head>', $content); | |
// Добавляем поля | |
$findField = '<input type="hidden" name="WEB_FORM_ID"'; // У всех веб-форм есть это поле (Должно быть) | |
$xField = '<input class="xname name" tabindex="-1" autocomplete="off" type="text" name="last_name" value="">'; // Максимально завлекающее ботов поля с утыканными name | |
$xField .= $findField; | |
// Добавляем к найденому полю вебформы наше поле | |
$content = str_ireplace($findField, $xField, $content); | |
return $content; | |
} | |
\Bitrix\Main\EventManager::getInstance()->addEventHandler('form', 'onBeforeResultAdd', 'onBeforeResultAddWebFormAntiSpam'); | |
function onBeforeResultAddWebFormAntiSpam($WEB_FORM_ID, &$arFields, &$arrVALUES){ | |
global $APPLICATION; | |
// Проверяем поле на заполненость | |
if(!empty($arrVALUES['last_name'])){ | |
$APPLICATION->ThrowException('Хм... ты точно не робот?'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment