search
HomeBackend DevelopmentPHP Tutorial 分享DEDECMS 验证码的一个细节有关问题。附提问一个疑惑,有关SESSION的

分享DEDECMS 验证码的一个细节问题。附提问一个疑惑,有关SESSION的

本帖最后由 default7 于 2012-12-01 15:54:51 编辑
问题:重复提交问题。表单提交成功了,发现页面单击右键点击返回上一页,浏览器提示“此页面不可用”,按下F5重新刷新,结果又提交成功了。导致“如果不停地刷新”就可以不停地提交成功!验证码的问题。
代码:
<br />
if($dopost == 'add'){<br />
  $svali = GetCkVdValue();<br />
  if(strtolower($vdcode)!=$svali || $svali=='')<br />
  {<br />
      ResetVdValue();<br />
      ShowMsg('验证码错误!', '-1');<br />
      exit();<br />
  }<br />
}<br />

解决: 一开始以为是ResetVdValue();这个函数的问题,最后才发现是执行顺序的问题,把   ResetVdValue(); 放到判断“提交的验证码是否正确”的代码之前就可以了。
<br />
if($dopost == 'add'){<br />
  $svali = GetCkVdValue();<br />
  ResetVdValue(); //位置移前<br />
  if(strtolower($vdcode)!=$svali || $svali=='')<br />
  {<br />
      ShowMsg('验证码错误!', '-1');<br />
      exit();<br />
  }<br />
}<br />




后来看了验证码方面的获取和销毁的函数,有一个疑问的地方:
1.未改动前的文件:/include/common.func.php,函数GetCkVdValue()和ResetVdValue()
<br />
/**<br />
 *  获取验证码的session值<br />
 *<br />
 * @return    string<br />
 */<br />
function GetCkVdValue()<br />
{<br />
    @session_id($_COOKIE['PHPSESSID']);<br />
    @session_start();<br />
    return isset($_SESSION['securimage_code_value']) ? $_SESSION['securimage_code_value'] : '';<br />
}<br />
<br />
/**<br />
 *  PHP某些版本有Bug,不能在同一作用域中同时读session并改注销它,因此调用后需执行本函数<br />
 *<br />
 * @return    void<br />
 */<br />
function ResetVdValue()<br />
{<br />
    @session_start();<br />
    $_SESSION['securimage_code_value'] = '';<br />
}<br />


2.未改动前的验证码生成程序:/include/vdimgck.php 开头的地方有写
<br />
$sessSavePath = DEDEDATA."/sessions/";<br />
<br />
// Session保存路径<br />
if(is_writeable($sessSavePath) && is_readable($sessSavePath)){ session_save_path($sessSavePath); }<br />
if(!empty($cfg_domain_cookie)) session_set_cookie_params(0,'/',$cfg_domain_cookie);<br />
<br />
session_start();<br />



个人将代码修改成如下,文件/include/common.func.php
<br>
<br>
/**<br>
 *  获取验证码的session值<br>
 *<br>
 * @return    string<br>
 */<br>
function GetCkVdValue()<br>
{<br>
    $sessSavePath = DEDEDATA."/sessions/";<br>
    if(is_writeable($sessSavePath) && is_readable($sessSavePath)){ session_save_path($sessSavePath); }<br>
    if(!empty($GLOBALS['cfg_domain_cookie'])) session_set_cookie_params(0,'/',$GLOBALS['cfg_domain_cookie']);<br>
<br>
    @session_id($_COOKIE['PHPSESSID']);<br>
    @session_start();<br>
    return isset($_SESSION['securimage_code_value']) ? $_SESSION['securimage_code_value'] : '';<br>
}<br>
<br>
/**<br>
 *  PHP某些版本有Bug,不能在同一作用域中同时读session并改注销它,因此调用后需执行本函数<br>
 *<br>
 * @return    void<br>
 */<br>
function ResetVdValue()<br>
{<br>
    $sessSavePath = DEDEDATA."/sessions/";<br>
    if(is_writeable($sessSavePath) && is_readable($sessSavePath)){ session_save_path($sessSavePath); } <div class="clear">
                 
              
              
        
            </div>
Statement
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
What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

How do you optimize PHP applications for performance?How do you optimize PHP applications for performance?May 08, 2025 am 12:08 AM

TooptimizePHPapplicationsforperformance,usecaching,databaseoptimization,opcodecaching,andserverconfiguration.1)ImplementcachingwithAPCutoreducedatafetchtimes.2)Optimizedatabasesbyindexing,balancingreadandwriteoperations.3)EnableOPcachetoavoidrecompil

What is dependency injection in PHP?What is dependency injection in PHP?May 07, 2025 pm 03:09 PM

DependencyinjectioninPHPisadesignpatternthatenhancesflexibility,testability,andmaintainabilitybyprovidingexternaldependenciestoclasses.Itallowsforloosecoupling,easiertestingthroughmocking,andmodulardesign,butrequirescarefulstructuringtoavoidover-inje

Best PHP Performance Optimization TechniquesBest PHP Performance Optimization TechniquesMay 07, 2025 pm 03:05 PM

PHP performance optimization can be achieved through the following steps: 1) use require_once or include_once on the top of the script to reduce the number of file loads; 2) use preprocessing statements and batch processing to reduce the number of database queries; 3) configure OPcache for opcode cache; 4) enable and configure PHP-FPM optimization process management; 5) use CDN to distribute static resources; 6) use Xdebug or Blackfire for code performance analysis; 7) select efficient data structures such as arrays; 8) write modular code for optimization execution.

PHP Performance Optimization: Using Opcode CachingPHP Performance Optimization: Using Opcode CachingMay 07, 2025 pm 02:49 PM

OpcodecachingsignificantlyimprovesPHPperformancebycachingcompiledcode,reducingserverloadandresponsetimes.1)ItstorescompiledPHPcodeinmemory,bypassingparsingandcompiling.2)UseOPcachebysettingparametersinphp.ini,likememoryconsumptionandscriptlimits.3)Ad

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.