分類
网站

php+mysql留言系统(不显示留言)

  1. 表单的html代码
    1. css
    2. html
    3. js表单验证
  2. mysql数据库表的创建、连接和插入数据
    1. 创建表单
    2. PHP连接数据库和插入数据
  3. 加入验证码,防范恶意
  4. 加入频率控制,提高用户体验
1. 表单的html代码 css
<style type="text/css">
/*占位组件*/
.h20 { height: 1px }
.cleaner { 
	clear: both; 
	display: block; 
}
.no_margin_right { margin-right: 0 }

/*按钮位置*/
.float_l { float: left; margin-left:50px;}
.float_r { float:left; margin-left:200px;}

/*表单样式*/
#contact_form { 
	padding: 0; 
	margin: 0px 0px 0px 30px ; 
}
#contact_form form { 
	margin: 10px;
	padding: 10px; 
}
#contact_form form p { 
	float: left; 
	margin-right: 14px; 
}
#contact_form form .input_field { 
	width: 164px; 
	padding: 5px; 
	color: #222;  
	border: 1px solid gray; 
	margin-top: 2px;
}
/*标签样式*/
#contact_form form label { 
	display: block; 
	margin-right: 12px; 
	font-weight:bold;
}
/*文本域样式*/
#contact_form form textarea { 
	width: 450px; 
	height: 180px; 
	padding: 5px; 
	color: #222;
	border: 1px solid gray; 
	margin-top: 5px;
}
#contact_form form .submit_btn { 
	padding: 5px 14px; 
}
</style>
html
<div id="contact_form">
<!--点击提交时先执行return validate_form(this)函数,若通过验证则将数据提交至/php/message.php-->
<form method="post" id="contact" name="contact" action="/php/message.php"  onsubmit="return validate_form(this)">	
		<p>
		<label for="author">Auther:</label> 
		<input type="text" id="author" name="author" class="input_field" />
		</p><p>
		<label for="email">Email:</label> 
		<input type="text" id="email" name="email" class="input_field" />
		</p>
	<div class="cleaner h20"></div>
	<label for="title">Title:</label> 
	<input type="text" id="title" name="title" class="input_field" />
	<div class="cleaner h20"></div>
	<label for="message">Message:</label> 		
	<textarea id="message" name="message" ></textarea>		
	<div class="cleaner h20"></div>
	<p>
<!--此处为验证码图片和验证框,后面会细说-->
	<a href="#" onclick="document.getElementById('captcha').src = '/php/securimage/securimage_show.php?' + Math.random(); return false" title="Click to renew" ><img id="captcha" src="/php/securimage/securimage_show.php" alt="CAPTCHA Image" /></a>
	</p><p>   
	<input type="text" name="captcha_code" size="10" maxlength="6" class="input_field" style="width: 80px;" />
	</p>
	<div class="cleaner h20"></div>
	<input type="submit" value="Submit" id="submit" name="submit" class="submit_btn float_l"/>
	<input type="reset" value="Reset" id="reset" name="reset" class="submit_btn float_r"/>
	<div class="cleaner h20"></div>

</form>
</div>
js
<script type="text/javascript">
function validate_notnull(field,alerttxt){//验证是否为空
  with(field){
	  content=field.value;
	  if (content==null||content=="")
		{alert(alerttxt);return false}
	  else {return true}
  }
}

function validate_email(field,alerttxt){//验证是否为合法电邮地址
	with (field){
		apos=value.indexOf("@")
		dotpos=value.lastIndexOf(".")
		if (apos<1||dotpos-apos<2) 
		  {alert(alerttxt);return false}
		else {return true}
	}
}

function validate_form(thisform){
	with  (thisform){
		if (validate_notnull(author,"请输入您的名字。")==false)
		{return false;}
		if ((validate_email(email,"请以标准格式输入您的电子邮件。"))==false)
		{return false;}
		if (validate_notnull(message,"请输入留言正文。")==false)
		{return false;}
	}
}
</script>
2. mysql数据库表的创建、连接和插入数据 css数据表
  • id,int(5),not null,auto increment
  • date,timestamp,not null,on update CURRENT_TIMESTAMP
  • auther,varchar(30)
  • email,varchar(30),not null
  • title,varchar(100)
  • message,varchar(420),not null
数据均选择utf8_general_ci格式。   php
<?php
$author=$_POST["author"];$email=$_POST["email"];$title=$_POST["title"];$message=$_POST["message"];
$con=mysql_connect("yourhost","db","pw");
if(!$con){
	die('Could not connect: '.mysql_error());
	}
mysql_query("set names utf8");
mysql_select_db("db",$con);
mysql_query("INSERT INTO `table` (`id`, `date`, `author`, `email`, `title`, `message`) VALUES (NULL, CURRENT_TIMESTAMP, '$author', '$email', '$title', '$message')");
mysql_close($con);
echo "<script language="javascript" type="text/javascript">rn";
echo "alert('Congratulations!\nWe have got your message and may reply in a few days.');rn";
echo "history.back();rn</script>"; 
exit;
?>
3. 加入验证码,防范恶意 这里使用的是Securimage 3.0,官方网站:http://www.phpcaptcha.org/。里面有详细的英文说明文档。这是我的配置过程。 首先下载并解压Securimage 3.0到服务器上,我放在了/php/中。然后可以访问下测试页,如果运行良好,就可以往自己的页面加了。我的一开始就不能运行,升级了下php就好了,不需要另外更改配置。 html部分加入一个图片和一个输入框,代码上面已经有了。验证码图片的大小和识别难度都在securimage_show.php中进行设置,我设置了 $img->ttf_file = './ocraextended.ttf';//字体 $img->charset = 'ABCDEFGHJKLMNPRSTUVWXYZabcdefghkmnstwxy34579';//显示词库 $img->image_height = 50;//高度值px $img->image_width = $img->image_height * M_E;//适配宽度 $img->perturbation = 0.20;//字符扭曲程度 $img->num_lines = 2;//干扰线的数目 最后在message.php的最前面加入下面代码,验证码加好了。
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/php/securimage/securimage.php';
$securimage = new Securimage();
if ($securimage->check($_POST['captcha_code']) == false) {
echo "The security code entered was incorrect.<br /><br />";
echo "Please go <a href='javascript:history.go(-1)'>back</a> and try again.";
exit;
}

?>
4. 加入频率控制,提高用户体验 最早想用数据库记录ip控制一段时间内的表单提交次数,但是限于数据库诸多问题,决定用cookie和session进行限制。具体待定。

在〈php+mysql留言系统(不显示留言)〉中有 1 則留言

Do you mind if I quote a couple of your articles
as long as I provide credit and sources back to your site?
My website is in the very same niche as yours and my users would definitely
benefit from some of the information you present here.

Please let me know if this okay with you. Cheers!

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *