如何防止PHP联系人表单中的垃圾邮件URL [英] How to prevent spam URLs in a PHP contact form

查看:48
本文介绍了如何防止PHP联系人表单中的垃圾邮件URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created a form where a user can add a review. Perhaps this is very obvious but how can I avoid a user inserting a url in the text area. Is there a way to check this? I have put in a captcha to check for humans.

<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()或两者结合来尝试两种方法.下面将清除文本区域,并对其进行转义以供数据库使用.

Using PHP you can try two things using preg_match() and strip_tags() or a combination of them both. below will clean the textarea, escape it as well for database.

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

Once the form is submitted try this.

$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
}

这篇关于如何防止PHP联系人表单中的垃圾邮件URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆