1、提交按钮置disabled
当用户提交后,立即把按钮置为不可用状态。这种用js来实现。
提交前
$("#submit").attr('disabled','true');
$("#submit").val("正在提交,请稍等");
....................................................................................
执行后,把按钮置为原来状态
$('#submit ').removeAttr('disabled');
$("#submit ").val("确定提交");
2、过期时间法
思路:当用户提交按钮后生成一个token(每次业务提交token 为唯一值)存入session,并设置过期时间。当用户再此提交时,检测token是否一致且是否过期,若一致且没有过期,则认为提交了二次。当程序执行出错的时候,则需要清除存入session的值。见下面程序
function checkRepeatSubmit($uniqueid = '', $expire = 30) {
$uniqueid = empty($uniqueid) ? Yii::app()->user->id . Yii::app()->user->name . Yii::app()->user->mihome : $uniqueid;
$token = md5("wms_check_repeat" . $uniqueid);
$time = time();
if (isset($_SESSION['token']) && !empty($_SESSION['token']) && $_SESSION['token'] == $token && ($time - $_SESSION['expire_time']
return false;
} else {
$_SESSION['token'] = $token;
$_SESSION['expire_time'] = $time;
//session写入的时候会等待整个页面加载完成,用此函数可以立即写入
session_write_close();
return true;
}
}
//删除存入的值
function cancelRepeatSubmit() {
unset($_SESSION['token']);
unset($_SESSION['expire_time']);
}
3、token销毁法
思路:当页面进行加装的时候生成token,存在session中,并写在表单里。表单提交的时候随表单提交给服务端,服务端通过session存入的token与token进行比较,若相等,则销毁seesion中存入的token,当页面遭到二次提交的时候,由于存入session中的token不存在而报错。下面是代码
/**
* 第二种方案
* 1、产生token,并存在session中
* 2、随页面生成
* 3、提交页面与session进行比对,成功后对session进行销毁
* 4、第二次提交则不存在这个值而报错
* @param type $uniqueid
* @return type
*/
function createToken($uniqueid) {
$uniqueid = empty($uniqueid) ? Yii::app()->user->id . Yii::app()->user->name . Yii::app()->user->mihome : $uniqueid;
$token = md5("wms_check2_repeat" . $uniqueid);
$_SESSION['form_token'] = $token;
session_write_close();
return $token;
}
function checkToken($token) {
if (!isset($_SESSION['form_token']) || empty($_SESSION['form_token']) || $_SESSION['form_token'] != $token) {
return false;
} else {
unset($_SESSION['form_token']);
return true;
}
}
上面总结了三种方法,个人感觉第一种跟第二种方法配合着用会达到更好的效果。第二种方法与第三种方法个人感觉第三种要有优势点。
第二种与第三种方法都是把token写在session中,这种方法好处是节省存储空间,但坏处是由于session是需要整个页面加载完毕才能写入,故当整个页面加载比较慢,且用户点击多次提交,可能由于session还没写入导致系统还认为是第一次输入。导致验证不起作用。好在php函数提供了一个牛逼的函数。 session_write_close(),可以立即把session写入,不用等待页面加载完成。同事对于session的存入也有很多种方法可以选择,可以存在redis,memcache或者数据库都可以的。

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Atom editor mac version download
The most popular open source editor
