I have a nu_soap Web service which I want to create and send Captcha code and image with it.
I'm using getcaptcha
web service to put generated Captcha code into a session variable and also Captcha Id, And by using another Web service named : validateCaptcha
I'm checking if it's valid or not,
Problem is when I check my web services with Firefox Soa client addons web services are working ok and all session variables are working fine.
but when I'm trying to use android phone to check it session variables are not defined or are empty.
get captcha code :
<?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 "";
}
}
?>
validate captcha code :
<?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 "";
}
}
?>
I Found a solution for this problem, I thought to post it here for others to use.
Instead of using session variables I used mysql database to store captcha (id,code,token,expire_time) then I used php date() function to check if captcha time is not expired and it's valid and In validation web service I used mysql to select from db to check if captcha is valid or not.
validation code :
get captcha code :