织梦自定义表单留言加验证码功能

原创 2021-03-4 本文共5977个字,预计耗時15分钟 844次阅读
文章摘要:利用织梦的自定义表单实现具有验证码功能的网站留言,防止被频繁提交被灌水留言,最近有需要加验证码的需求,验证码也有防止被灌水的危险...

织梦自定义表单留言加验证码功能

利用织梦的自定义表单(http://doc.ydqic.com/3635.html)实现网站留言功能之前有做过,最近有需要加验证码的需求,验证码也有防止被灌水的危险。百度相关教程,发现很严重的问题,一篇教程有错误,其他都是一样错的,都是一帮采集,一人写的教程全部采集,没有点分辨能力,真的不知道分明按照教程一步一步来,还是各种错误。下面这篇是经过实际验证,也改进了之前教程的一些问题。

第一步:

首先需要在后台-频道模型-自定义表单里新建一个表单,增加好表单所需要的字段,后增加一个验证码字段,在自定义表单HTML里填写如下:

<input name="validate" type="text" id="vdcode" style="text-transform:uppercase;" size="8"/>
<img id="vdimgck" align="absmiddle" onClick="this.src=this.src+'?'" style="cursor: pointer;" alt="看不清?点击更换" src="/include/vdimgck.php"/>
<a href="javascript:;" onClick="changeAuthCode();">看不清? </a>
注:网上教程最后那个a标签里href属性”javascript:vide(-1);”写法是错的,作用是让a标签取消默认跳转,如果照搬,后面会发现报错vide未定义,正确写法”javascript:;”或”javascript:void(0);”

增加验证码字段

第二步:

再在模板htm文件,需要的地方增加:

<input name="validate" type="text" id="vdcode" size="8" placeholder="Please enter the verification code"/>
<img id="vdimgck" align="absmiddle" onClick="this.src=this.src+'?'" style="cursor: pointer;" alt="看不清?点击更换" src="/include/vdimgck.php"/>
<a href="javascript:;" onClick="changeAuthCode();">看不清? </a>
注:这里的a标签跟上面一样其他教程也有”javascript:vide(-1);”错误写法,直接改正了;还有很多教程里都说要复制整个表单的结构,织梦默认的表单结构是table表格结构,其实只要其中的form元素,input,textarea等元素,其表格结构是不必要的,因为表格布局毕竟比较不常见也不美观。在拿到其默认的form结构,完全可以用div控制结构美化表单,如下面红线里是必须的表单元素,用div+css美化。

美化表单元素

还需要一个切换验证码刷新功能的js函数,另外为了表单的完整通常需要做内容的判断,如用户名是否填写,邮箱格式是否正确,下面的js基于jquery,可以写作模板htm文件的最后面

//验证码生成  绑定到"看不清按钮"点击事件上
function changeAuthCode() {
	let num = new Date().getTime();
	let rand = Math.round(Math.random() * 10000);
	num = num + rand;
	//$('#ver_code').css('visibility','visible');  这个没用的,不知道其他教程为啥有根本没有#ver_code这个元素
	if($("#vdimgck")[0]){
		$("#vdimgck")[0].src = "/include/vdimgck.php?tag=" + num;
	}
	return false;
}
//提交按钮 前判断数据 form元素需要绑定 onsubmit="return formcheck()"
function formcheck(){  
	let content = $(".content textarea").val();
	let name = $(".name input").val();
	let email = $(".email input").val();
	let re_email = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
	
	if(content == ''){
		alert("Please input the content");
		return false;
	}
	if(name == ''){
		alert("Please input the name");
		return false;
	}
	if(!re_email.test(email)){
		alert("Please input the mailbox");
		return false;
	}
}  
如果在发现前台验证码直接是vdimgck.php文件未找到错误,只是1,2步里的img元素的src路径问题,根据自身网站的情况,如果是动态的网上教程中路径”../include/vdimgck.php”是可行的,如果是静态的,然后你的留言板内容目录很深或者就可能出现错误,我在看网上的教程就是如此”../include/vdimgck.php”路径如果不是目录不对应会出错的,所以我直接改成路径”/include/vdimgck.php”。

第三步:

修改/plus/diy.php,使之判断是否验证码正确,在$fieldarr = explode(‘;’, $dede_fields);前,大概61行,增加函数:

//验证码
if(!empty($dede_fields)){
	$validate = empty($validate) ? '' : strtolower(trim($validate));            
	$svali = strtolower(GetCkVdValue());
	if(($validate=='' || $validate != $svali) && preg_match("/6/",$safe_gdopen)){
		ResetVdValue(); 
		ShowMsg('验证码不正确!','-1',$dede_add,0,1000);exit;
	} 
}
//end

修改后的效果:

判断验证码正确

最后贴上表单完整的HTML结构和CSS样式,最终呈现的自定义表单效果如下图:

织梦带验证码的自定义表单

织梦自定义表单HTML结构:

<div id="liuyanban">
	<div>
		<form action="/plus/diy.php" enctype="multipart/form-data" method="post" onsubmit="return formcheck()">
			<input type="hidden" name="action" value="post" />
			<input type="hidden" name="diyid" value="1" />
			<input type="hidden" name="do" value="2" />
			<div class="content">
				<span>*</span>
				<textarea name='ly_content' id='ly_content' placeholder="Please input message content."></textarea>
			</div>
			<div class="name">
				<input type='text' name='ly_name' id='ly_name' class='intxt' value='' placeholder="Please input your name."/>
			</div>
			
			<div class="email">
				<input type='text' name='ly_email' id='ly_email' class='intxt' value='' placeholder="Please input the mailbox." />
			</div>
			<div class="vdcode">
				<input name="validate" type="text" id="vdcode" size="8" placeholder="Please enter the verification code"/>
				<img id="vdimgck" align="absmiddle" onClick="this.src=this.src+'?'" style="cursor: pointer;" alt="看不清?点击更换" src="/include/vdimgck.php"/>
				<a href="javascript:;" onClick="changeAuthCode();">看不清? </a>
			</div>
			
			<div class="subbtn">
				<input type="submit" name="submit" value="Submission" class='coolbg' />
			</div>
			<input type="hidden" name="dede_fields" value="ly_content,multitext;ly_name,text;ly_email,text" />
			<input type="hidden" name="dede_fieldshash" value="88a381fdd6c488365b9cd67812e6643e" />
		</form>
	</div>
</div>

自定义表单CSS样式代码

#liuyanban{
	height: 590px;
	width: 664px;
	margin: 0 auto;
	text-align:center;
}
#liuyanban .content{
	height: 150px;
	width: 640px;
	color: #666;
	border-color: #000;
	margin-top: 0;
	line-height: 19px;
	font-size: 14px;
	text-align:center;
	display: inline-block;
	margin-top: 15px;
	margin-bottom: 15px;
}
#liuyanban .content span{
	display: inline-block;
	width: 5px;
	color:red;
	vertical-align: top;
	font-size: 16px;
	font-weight: 600;
}
#liuyanban .content textarea{
	width: 630px;
	display: inline-block;
	height: inherit;
	padding-left: 5px;
	padding-right: 5px;
	border: 1px solid rgba(240, 240, 240, 1);
	resize: none;
	padding-top: 5px;
}
#liuyanban .name{
	width: 630px;
	color: #666;
	border-color: rgba(228, 228, 228, 1);
	height: 40px;
	display: inline-block;
	vertical-align: middle;
	color: inherit;
	position: relative;
	font-size: 14px;
	margin-left:5px;
	margin-top: 15px;
	margin-bottom: 15px;
}
#liuyanban .name input{
	width: 100%;
	height: 100%;
	background: #F7F7F7;
	border: 1px solid #f0f0f0;
	padding-left: 5px;
}
#liuyanban .email{
	width: 630px;
	color: #666;
	border-color: rgba(228, 228, 228, 1);
	height: 40px;
	display: inline-block;
	vertical-align: middle;
	color: inherit;
	position: relative;
	font-size: 14px;
	margin-left:5px;
	margin-top: 15px;
	margin-bottom: 15px;
}
#liuyanban .email input{
	width: 100%;
	height: 100%;
	background: #F7F7F7;
	border: 1px solid #f0f0f0;
	padding-left: 5px;
}
#liuyanban .vdcode{
	width: 630px;
	color: #666;
	border-color: rgba(228, 228, 228, 1);
	height: 40px;
	display: inline-block;
	vertical-align: middle;
	color: inherit;
	position: relative;
	font-size: 14px;
	margin-left:5px;
	margin-top: 15px;
	margin-bottom: 15px;
	text-align: left;
}
#liuyanban .vdcode input{
	width: 40%;
	height: 100%;
	background: #F7F7F7;
	border: 1px solid #f0f0f0;
	padding-left: 5px;
}
#liuyanban .subbtn{
	width: 630px;
	color: #666;
	border-color: rgba(228, 228, 228, 1);
	height: 40px;
	display: inline-block;
	vertical-align: middle;
	color: inherit;
	position: relative;
	font-size: 14px;
	margin-left:5px;
	margin-top: 15px;
	margin-bottom: 15px;
}
#liuyanban .subbtn input{
	width: 100%;
	height: 100%;
	background: #0A91FA;
	border: 1px solid #0A91FA;
	padding-left: 5px;
	color:#fff;
	font-weight:600;
	font-size:16px
}

配合之前的自定义表单中介绍的短时间防止重复提交,留言列表索引等功能(看这里:http://doc.ydqic.com/3635.html),一个比较完善的织梦留言功能即可完成,具有验证码,前台检验数据合理正确,短时间不可重复提交,留言搜索等功能。
版权免责申明
① 本站源码模板等资源SVIP用户永久不限量免费下载
② 所有资源来源于网络收集,如有侵权,请联系站长进行删除处理。
③ 分享目的仅供大家学习和交流,请不要用于商业用途,否则后果自负。
④ 如果你有源码需要出售,可以联系管理详谈。
⑤ 本站提供的源码、模板、插件等等资源,都不包含技术服务请大家谅解。
⑥ 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需。
⑦ 在您的能力范围内,为了大环境的良性发展,请尽可能的选择正版资源。
⑧ 网站资源绝不做任何二次加密或添加后门(原版加密除外)
常见问题F&Q
需要积分的资源怎么下载?
您可以注册后签到等活跃动作获得积分,积分可下载,也可充值升级等级免费下载。
源码模板等文件安全吗?有没有后门病毒吗?
站内资源标有“已测试”标签的资源源码,表示已经在本地安装测试调试过才分享出来的,可以保证一定的安全;若不放心可以自行下载模板资源后使用D盾等查杀工具扫一遍确认安全。
本站网站模板等源码提供安装服务吗?
本站资源收集于网络并分享出来共同学习,不提供免费安装服务,模板源码安装等需要有一定熟悉度,小白用户可以下载资源后雇人安装调试。