Maison > Article > développement back-end > 经验之谈PHP表单重复提交问题解决方案_PHP教程
PHP经过长时间的发展,很多用户都很了解PHP了,这里我发表一下个人理解,和大家讨论讨论。我们在做网站时,常常为一些灌水文章而烦恼。有时,由于网络状况等原因用户不知道提交是否成功,也会再次提交同一份表单,这就造成了PHP表单的重复提交不起。
有一个简单的方法可以避免同一表单的重复提交。首先,先定义一个session变量用来保存一个PHP表单的提交序列号。这里我定义为“$userLastAction”。然后在表单里加入一个hidden 变量,把值设为
<ol class="dp-xml"> <li class="alt"><span><span>$userLastAction+1: </span></span></li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">input</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">type</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">Hidden</font></span><span> </span><span class="attribute"><font color="#ff0000">name</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">lastAction</font></span><span> </span><span class="attribute"><font color="#ff0000">value</font></span><span>=</span><span class="tag"><strong><font color="#006699"></font></strong></span><span> =$userLastAction+1 </span><strong><font color="#006699"><span class="tag">?></span><span class="tag">></span></font></strong><span> </span> </li> </ol>
最后,在处理提交之前判断PHP表单是否已被提交过。
<ol class="dp-xml"> <li class="alt"><span><span>if($lastAction</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>$userLastAction){ </span></span></li> <li class=""><span>$userLastAction++; // 序列号加1 </span></li> <li class="alt"><span>// 处理表单数据 </span></li> <li class=""><span>} </span></li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
先建一数据表download (后端用MySql)
<ol class="dp-xml"> <li class="alt"><span><span>download表中含有下列两个字段: </span></span></li> <li class=""><span>id下载的文件的唯一标识 </span></li> <li class="alt"><span>downnum 下载次数,默认值为0 </span></li> </ol>
实现
假设已经从数据库取出:描述文件名 五笔字型 ;下载文件名:wbzx.zip ;标识号id值为2处理后生成的超级链接代码为:
<ol class="dp-xml"><li class="alt"><span><strong><font color="#006699"><span class="tag"><span class="tag-name">a</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">href</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"download.php?filename=wbzx.zip&id=2"</font></span><span class="tag"><strong><font color="#006699">></font></strong></span><span>五笔字型</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">a</span><span class="tag">></span></font></strong><span> </span></span></li></ol>
下面的代码download.php处理下载次数累计及根据传递的文件名定位下载文件。
<ol class="dp-xml"> <li class="alt"><span><span>//连接Mysql数据库代码略 </span></span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">res</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">mysql_query</font></span><span>("update download set </span><span class="attribute"><font color="#ff0000">downnum</font></span><span>=(downnum+1) where </span><span class="attribute"><font color="#ff0000">id</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">'$id'</font></span><span>" ,$db); </span> </li> <li class="alt"><span>header("location:$filename"); </span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>