我有一个nu_soap Web服务,我想创建它并用它发送Captcha代码和图像。
我正在使用getcaptcha
web服务将生成的Captcha代码和Captcha Id放入会话变量中。通过使用另一个名为validateCaptcha
的web服务,我正在检查它是否有效,
问题是,当我用Firefox Soa客户端插件检查我的web服务时,web服务工作正常,所有会话变量都工作正常。
但当我试图使用android手机进行检查时,会话变量没有定义或为空。
获取captcha代码:
<?php
session_start();
include_once('./Utility/DBM.php');
include_once('captcha.class.php');
class getCaptcha extends DBM
{
public function getcaptchacode($dump)
{
//creating and generating captcha image and code
$img = createcaptcha();
//Creating Captcha Id
$CaptchaId = (mt_rand(1,1000) . "1234");
imagepng($img[0],'Captcha/'.$CaptchaId.'.png');
imagedestroy($img[0]);
//encoding to base64 and getting file content
$base64 = base64_encode(file_get_contents('./Captcha/'.$CaptchaId.'.png'));
//setting up session for captcha
$_SESSION['Captcha_Code']=$img[0];
$_SESSION['Captcha_ID']=$CaptchaId;
$img[]='';
$captcha=$base64;
$captchaId = $CaptchaId;
$this->strResult = "";
$this->strResult.="<captcha>";
$this->strResult.="<captcha>$captcha</captcha>";
$this->strResult.="<captcha_Id>$captchaId</captcha_Id>";
$this->strResult.="</captcha>";
}
function __toString()
{
if($this->strResult!=""){
return $this->strResult;
}
return "";
}
}
?>
验证验证码:
<?php
session_start();
include_once('./Utility/DBM.php');
class validateCaptcha extends DBM
{
public function validatecaptcha($CaptchaCode,$Captcha_Id)
{
if(($CaptchaCode!=""&&$CaptchaCode==$_SESSION['Captcha_Code'])&&($Captcha_Id!=""&&$Captcha_Id==$_SESSION['Captcha_ID']))
{
$msg="Captcha is correct";
//generating a Token using md5 & salt
$hash = md5(mt_rand(1,1000000) . "123456");
$token=$_SESSION['CapToken'] = $hash;
//starting token session time
$_SESSION['Token_time'] = time();
//destroying captcha image
unlink('./Captcha/'.$Captcha_Id.'.png');
}
else
{
//destroying captcha image
unlink('./Captcha/'.$Captcha_Id.'.png');
//destroying all session
//session_destroy();
$msg="Wrong Captcha Entered";
$token="";
}
$this->strResult = "";
$this->strResult.="<captcha>";
$this->strResult.="<captcha>$msg</captcha>";
$this->strResult.="<token>$token</token>";
$this->strResult.="</captcha>";
}
function __toString()
{
if($this->strResult!=""){
return $this->strResult;
}
return "";
}
}
?>
我找到了这个问题的解决方案,我想把它发布在这里供其他人使用。
我没有使用会话变量,而是使用mysql数据库来存储captcha(id,code,token,expire_time),然后我使用php date()函数来检查captcha时间是否未过期并且有效。
验证代码:
class validateCaptcha extends DBM
{
public function validatecaptcha($CaptchaCode,$Captcha_Id)
{
$select_captcha = $this->select("captchaid,code,token,expire", "captcha", "code='$CaptchaCode' AND captchaid='$Captcha_Id'", 0, 0);
$captcha_check=mysql_num_rows($select_captcha);
$row = mysql_fetch_assoc($select_captcha);
if($captcha_check)
{
$msg="Captcha is correct";
$token=$row['token'];
//destroying captcha image
unlink('./Captcha/'.$Captcha_Id.'.png');
}
else
{
//destroying captcha image
unlink('./Captcha/'.$Captcha_Id.'.png');
$msg="Wrong Captcha Entered";
$token="";
}
$this->strResult = "";
$this->strResult.="<captcha>";
$this->strResult.="<captcha>$msg</captcha>";
$this->strResult.="<token>$token</token>";
$this->strResult.="</captcha>";
}
获取captcha代码:
class getCaptcha extends DBM
{
public function getcaptchacode($dump)
{
//creating and generating captcha image and code
$img = createcaptcha();
//Creating Captcha Id
$CaptchaId = (mt_rand(1,1000) . "1234");
imagepng($img[0],'Captcha/'.$CaptchaId.'.png');
imagedestroy($img[0]);
//encoding to base64 and getting file content
$base64 = base64_encode(file_get_contents('./Captcha/'.$CaptchaId.'.png'));
//generating a Token using md5 & salt
$hash = md5(mt_rand(1,1000000) . "123456");
$token=$_SESSION['CapToken'] = $hash;
//Getting time
$getime=date('Y-m-d H:i:s');
//inserting into captcha table
$this->RunQuery("INSERT INTO captcha (captchaid,code,token,expire) VALUES ('$CaptchaId','$img[1]','$token','$getime')", true, false);//TODO query
if (mysql_insert_id())
$Response = 'True';
else
$Response = 'False';
//deleting inactive captcha for 15 minutes
$this->RunQuery("DELETE FROM captcha WHERE expire < DATE_SUB(NOW(), INTERVAL 15 MINUTE)",true,false);
$img[]='';
$captcha=$base64;
$captchaId = $CaptchaId;
$this->strResult = "";
$this->strResult.="<captcha>";
$this->strResult.="<captcha>$captcha</captcha>";
$this->strResult.="<captcha_Id>$captchaId</captcha_Id>";
$this->strResult.="<Response>$Response</Response>";
$this->strResult.="</captcha>";
}