Re: Hi need help w/captcha code

Discussion in 'PHP' started by ataloss, Mar 31, 2016.

  1. #1
    I need help with this Captcha code. when I try it, all I get is code,
    starting with

    "captcha_varname='$captcha_var_name';" through "?>"

    It's like my Php wasn't working but I've confirmed it is. I'm hoping

    someone will tell me why. Following is my code COPIED FROM ONLINE:

    <?PHP
    // simple-captcha.php
    class FGSimpleCaptcha extends FG_CaptchaHandler;
    {
    var $error_str;
    var $captcha_varname;
    var $uniquekey;

    function FGSimpleCaptcha($captcha_var_name)
    {
    $this->captcha_varname='$captcha_var_name'; // ?

    $this->uniquekey='abc12fghijkl34mno;
    }
    /*Add more simple questions here.*/
    function GetSimpleCaptcha()
    {
    $arrQuestions = array(
    "Which of sock, library, cake or red is a color? "=>"red",
    "Enter the number thirteen thousand three hundred and sixty eight in

    digits: "=>"13,368");

    $question = array_rand($arrQuestions);
    $answer = $arrQuestions[$question];

    $_SESSION['FGCF_Captcha_Answer'] = $this->Md5CaptchaAnswer

    ($answer);

    return $question;
    }
    function SetFormKey($key)
    { $this->uniquekey = $key; }
    function GetKey()
    { return $this->uniquekey; }
    function Validate()
    {
    $ret=false;
    if(empty($_POST[$this->captcha_varname]))
    {
    $this->error_str = "Please answer the anti-spam question";
    $ret = false;
    }
    else
    {

    $scaptcha = trim($_POST[$this->captcha_varname]);

    $scaptcha = strtolower($scaptcha);

    $user_answer = $this->Md5CaptchaAnswer($scaptcha);

    if($user_answer != $_SESSION['FGCF_Captcha_Answer'])
    {
    $this->error_str = "Failed the anti-spam check!";
    $ret = false;
    }
    else
    { $ret = true; }
    }
    //else
    return $ret;
    }
    function Md5CaptchaAnswer($answer)
    { return md5($this->GetKey().$answer); }
    function GetError()
    { return $this->error_str; }
    }
    ?>
     
    ataloss, Mar 31, 2016 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    You're missing a single quote at the end of the content of the $this->uniquekey variable. It should be
    
    $this->uniquekey='abc12fghijkl34mno';
    
    PHP:
    Not
    
    $this->uniquekey='abc12fghijkl34mno;
    
    PHP:
    And for the future, when you paste code, be sure to put it inside [ code] or [ php] tags (remove the space inside), or use the Insert -> Code button in the editor.
     
    PoPSiCLe, Mar 31, 2016 IP
  3. ataloss

    ataloss Active Member

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    63
    #3
    Thanks for the response but $this->uniquekey='abc12fghijkl34mno'; doesn't change anything?
     
    ataloss, Mar 31, 2016 IP
  4. ataloss

    ataloss Active Member

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    63
    #4
     
    ataloss, Mar 31, 2016 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    Well, there is another error, I see now, at the first line - remove the ; from after FG_CaptchaHandler - it should go directly to the {

    However, that class / product seems to be quite bad, in many ways. I don't think I'd use it for anything important.
     
    PoPSiCLe, Apr 1, 2016 IP
  6. ataloss

    ataloss Active Member

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    63
    #6
    This is my current code-same results
    <?PHP
    // simple-captcha.php
    class FGSimpleCaptcha extends FG_CaptchaHandler
     {
      var $error_str;
      var $captcha_varname;
      var $uniquekey;
    
      function FGSimpleCaptcha($captcha_var_name)
     {
      $this->captcha_varname='$captcha_var_name'; // ?
    
      $this->uniquekey='abc12fghijkl34mno';
      }
      /*Add more simple questions here.*/
      function GetSimpleCaptcha()
     {
      $arrQuestions = array(
    "Which of sock, library, cake or red is a color? "=>"red",
    "Enter the number thirteen thousand three hundred and sixty eight in digits: "=>"13,368");   
       
      $question = array_rand($arrQuestions);
      $answer = $arrQuestions[$question];
    
      $_SESSION['FGCF_Captcha_Answer'] = $this->Md5CaptchaAnswer($answer);
    
      return $question;
     }
      function SetFormKey($key)
      { $this->uniquekey = $key; }
      function GetKey()
      { return $this->uniquekey; }
      function Validate()
     {
      $ret=false;
      if(empty($_POST[$this->captcha_varname]))
     {
      $this->error_str = "Please answer the anti-spam question";
      $ret = false;
     }
      else
     {
    
      $scaptcha = trim($_POST[$this->captcha_varname]);
    
      $scaptcha = strtolower($scaptcha);
    
      $user_answer = $this->Md5CaptchaAnswer($scaptcha);
    
      if($user_answer != $_SESSION['FGCF_Captcha_Answer'])
     {
      $this->error_str = "Failed the anti-spam check!";
      $ret = false;
     }
      else
      { $ret = true; }
     }
    //else
      return $ret;
     }
      function Md5CaptchaAnswer($answer)
     { return md5($this->GetKey().$answer); }
      function GetError()
      { return $this->error_str; }
     }
    ?>
    PHP:
     
    ataloss, Apr 1, 2016 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    If you're still getting PHP output to the browser, there is something else wrong, because that class in itself, as long as you remove the "extends other class"-bit, doesn't output anything. Only a blank page. Since you're not posting the rest of your code, it's hard to say where the error might be - it could be in another class, it might be in the page you're trying to use this in, etc.
     
    PoPSiCLe, Apr 1, 2016 IP