After a long period of development of PHP, many users know PHP very well. Here I will express my personal understanding and discuss it with everyone. When we are building websites, we often worry about some spamming articles. Sometimes, due to network conditions and other reasons, users do not know whether the submission is successful and will submit the same form again, which causes repeated submission of PHP forms.
There is an easy way to avoid duplicate submissions of the same form. First, define a session variable to save the submission sequence number of a PHP form. Here I define it as "$userLastAction". Then add a hidden variable to the form and set the value to
<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><SPAN class=tag-name>input</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>
Finally, determine whether the PHP form has been submitted before processing the submission.
<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>
First create a data table download (use MySql for the backend)
<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>
Implementation
Assume that it has been taken out from the database: description file name Wubi font; Download file name: wbzx.zip; The identification number id value is 2. The generated hyperlink code after processing is:
<ol class="dp-xml"><li class="alt"> <span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>a</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>></span></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> </li></ol>
The following code download.php processes the cumulative number of downloads and passes the The filename locates the downloaded file.
<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>