[英]Prevent links to be sent in HTML/PHP input forms
我在html文件中有以下輸入形式
<form action="contact.php" method="post" autocomplete="on">
<input type="text" name="Name">
<input type="text" name="Email">
<textarea name="Message"></textarea>
和以下contact.php文件發送消息:
if (preg_match('(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*', $_POST['Message'])) {
// it's SPAM
die();
}
if(isset($_POST['Email'])){...
我正在尋找的結果是防止鏈接插入textarea。
如果在文本區域中插入了鏈接,我仍然會收到電子郵件,並且我不知道我的代碼有什么問題。
只需使用正則表達式或xpath查找定位標記。 它將更加容易,您可以向用戶發出有關錨標記的警報,也可以使表單無法提交表單。
正則表達式:-
\<a.*(\s*)href+\=[\"\'].*[\"\'][.\s\w]*>[.\s\w\d]*\<\/a\>
xpath:-
var anchorCount = document.evaluate( 'count(//a)', document, null, XPathResult.ANY_TYPE, null );
alert( 'This document contains ' + anchorCount.numberValue + ' anchor elements' );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.