Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何防止PHP联系人表单中的垃圾邮件URL_Php_Forms - Fatal编程技术网

如何防止PHP联系人表单中的垃圾邮件URL

如何防止PHP联系人表单中的垃圾邮件URL,php,forms,Php,Forms,我已经创建了一个表单,用户可以在其中添加评论。也许这是很明显的,但是我如何避免用户在文本区域插入url呢。有办法检查这个吗? 我已经输入了验证码来检查人类 <form action="" method="POST" name="form"> some other input fields <span id="sprytextarea1"> <textarea name="Comments" cols="50"

我已经创建了一个表单,用户可以在其中添加评论。也许这是很明显的,但是我如何避免用户在文本区域插入url呢。有办法检查这个吗? 我已经输入了验证码来检查人类

<form action="" method="POST" name="form">
  some other input fields

             <span id="sprytextarea1">
             <textarea name="Comments" cols="50" rows="3" ></textarea>
             <span class="textareaRequiredMsg">A value is required.</span></span></p>

  <img id="captcha" src="/securimage/securimage_show.php" alt="CAPTCHA Image" /><input type="text" name="captcha_code" size="10" maxlength="6" />

<a href="#" onclick="document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random(); return false">[ Different Image ]</a>

  <input name="Submit" type="submit" value="Submit your form ">
           <input type="hidden" name="MM_insert" value="form">
           </form>

其他一些输入字段
需要一个值


欢迎任何建议

使用PHP,您可以使用preg_match()和strip_tags()或两者的组合尝试两种方法。下面将清理文本区域,并将其转义到数据库

提交表单后,请尝试此操作

$post = array();

foreach($_POST as $k => $v){

// strip all HTML and escape values for database

$post[$k] = strip_tags(mysql_real_escape_string($v));
}

// check of we have URL in text area

if(preg_match('/www\.|http:|https:/'i,$post['Comments']){

// prevent form from saving code goes here
echo 'error please remove URLs';

}else{
// let form be saved
}

简单地说,没有任何自动的方法来检查输入文本中的URL。您只需对用户输入进行人工检查

例如:假设您尝试对URL应用正则表达式检查,而我想欺骗它,我可能能够编写显示为URL的无限字符串:

  • http:example.com
  • h ttp://ecxampleDOTcom
  • ht tp://ecample。Com

因此,任何评论系统为了实现对垃圾邮件的终极保护,都会应用主持人审查,由一个人检查内容。

只需在插入db之前输入if条件即可

if(preg_match("/\b(?:(?:https?|ftp|http):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$_POST['comment'])){

echo 'error please remove URLs';

}else
{....

这就是验证码的目的——最大限度地减少机器人/垃圾邮件(尽管它很容易被工资差异打败)。现在,可以删除所有URL或类似URL的文本,但这将击败非垃圾邮件链接,也不会阻止智能表示。或者,要求验证并信任花时间注册的人不会发送垃圾邮件。如何验证inputUse prepared语句以将不安全的内容放入数据库中。PDO和MySQLi确实为它们提供了方法。甚至,mysql_*函数也被弃用。不完全确定Captcha的含义不是为了阻止用户直接在表单中插入URL,它们不是这样做的。OP询问是否有方法检查表单并防止用户插入URL。上面的代码应该可以做到这一点。验证码似乎实现了一个目的,那就是防止来自机器人或非人类的垃圾邮件评论。我没有提到验证码。在大多数情况下,这个答案中的正则表达式足以检查主题中是否有URL。这个答案中的数据库内容应该避免像瘟疫一样(不推荐使用,不安全等)。谢谢Leon,你的答案是正确的。我只需要更改一些位,结果就是这样的:if(preg\u match('/www.\http:'https:\/\/\/[a-z0-9]+([\-\.]{1}[a-z\u 0-9]+)*\.[u a-z]{2,5}.”((:[0-9]{1,5})\/.$/I',$u POST['Comments'])){这不会阻止所有URL,例如,它不会阻止
aahttps://stackoverflow.com/questions/20254482/how-to-prevent-spam-urls-in-a-php-contact-form