在片段外部设置的会话值无法在代码段中访问

时间:2012-08-21 10:14:44

标签: captcha modx modx-revolution

我正在尝试在已经构建并运行的MODx站点中使用验证码图像。我能够看到验证码图像,但遗憾的是验证码脚本无法设置可以在modx片段中访问的会话值,因此我无法将其与输入的值匹配。创建图像的脚本位于modx / assets / captcha / captcha.php中。 我花了将近3天来解决这个问题。我尝试了不同的方法来使它工作。但没有任何作用。有没有解决方案?有人来过这个问题吗?

2 个答案:

答案 0 :(得分:1)

将此内容放入您的脚本中,如下所示:

//Start the session so we can store what the security code actually is
session_start();
//Set the session to store the security code
$_SESSION["security_code"] = $security_code;

并在检查内容的片段中

//Continue the session
session_start();
//Check if the security code and the session value are not blank 
//and if the input text matches the stored text
if ((!empty($_REQUEST["txtCaptcha"]) && !empty($_SESSION["security_code"]))
     && ($_REQUEST["txtCaptcha"] == $_SESSION["security_code"]) ) {
  echo "<h1>Test successful!</h1>";
} else {
  echo "<h1>Test failed! Try again!</h1>";
}

但是如果你使用Modx Revolution更合适的解决方案是使用组件FormIt(http://rtfm.modx.com/display/ADDON/FormIt.Examples.Simple+Contact+Page),“Recaptcha”或“Blank NoSpam Field”

答案 1 :(得分:1)

Formit有一个验证码prehook - 你可以把那个片段分开吗?看他们是怎么做到的?事实上,为什么不只是使用它而不是自己滚动,它可能会减少相当多的工作。