Experience and solutions to the problem of repeated submission of PHP forms_PHP Tutorial

WBOY
Release: 2016-07-15 13:27:31
Original
753 people have browsed it

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>
Copy after login

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>
Copy after login

The main principle of this technique is that the user is not allowed to resubmit after reverting, which means that resubmitting after reverting is not allowed. But the watering method of Ctrl-C/Ctrl-V still cannot be avoided. Backfilling of the number of downloads of downloaded files: When we download software, we often see the download statistics of the software, and these statistics make it convenient for webmasters to analyze the popularity of the software. The implementation principle is: store the unique identification and download number of the software in the back-end database. When the user downloads the software, the number of downloads of the corresponding software is first updated, that is, the number of downloads is increased by 1, and then the download is officially started on the download file. Please look at the implementation code below:

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>
Copy after login

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>
Copy after login

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>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446511.htmlTechArticleAfter a long period of development of PHP, many users know PHP very well. Here I will express my personal understanding and share it with you. Discuss discuss. When we are building websites, we often worry about some spam articles...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template