【问题标题】:how to make captcha case sensitive in phpfox如何在phpfox中使验证码区分大小写
【发布时间】:2016-05-24 05:13:09
【问题描述】:

亲爱的朋友们,

我需要你的帮助来制作区分大小写的 phpfox,

我想让验证码在我的 phpfox 网站中不区分大小写。请帮我。 我在 phpfox 验证码模块中找不到更改区分大小写的代码,并且 phpfox 后端没有任何设置,所以请帮助我。 这是

的代码

captcha.class.php

<?php
/**
 * [PHPFOX_HEADER]
 */

defined('PHPFOX') or exit('NO DICE!');

/**
 * 
 * 
 * @copyright       [PHPFOX_COPYRIGHT]
 * @author          Raymond Benc
 * @package         Module_Captcha
 * @version         $Id: captcha.class.php 6005 2013-06-06 14:12:12Z Raymond_Benc $
 */
class Captcha_Service_Captcha extends Phpfox_Service 
{
    private $_oSession;

    /**
     * Class constructor
     */ 
    public function __construct()
    {       
        $this->_oSession = Phpfox::getService('log.session');
    }

    public function checkHash($sCode = null)
    {

        if (Phpfox::getParam('captcha.recaptcha'))
        {

            require_once(PHPFOX_DIR_LIB . 'recaptcha' . PHPFOX_DS . 'recaptchalib.php');        
            if (isset($_POST["recaptcha_response_field"])) 
            {           
                $oResp = recaptcha_check_answer(Phpfox::getParam('captcha.recaptcha_private_key'), $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);

                if ($oResp->is_valid){          
                    return true;
                }else{               
                    return false;
                }           
            }   

            return false;
        }       

        if (Phpfox::getParam('core.store_only_users_in_session'))
        {

            $oSession = Phpfox::getLib('session');

            $sSessionHash = $oSession->get('sessionhash');

            $aRow = $this->database()->select('*')
                ->from(Phpfox::getT('log_session'))
                ->where('session_hash = \'' . $this->database()->escape($sSessionHash) . '\'')
                ->execute('getSlaveRow');

            if (isset($aRow['session_hash']) && $this->_getHash(strtolower($sCode), $aRow['session_hash']) == $aRow['captcha_hash'])
            {
                return true;
            }
        }
        else
        {

            if ($this->_getHash(strtolower($sCode), $this->_oSession->getSessionId()) == $this->_oSession->get('captcha_hash'))
            {
                return true;
            }
        }

        return false;
    }

    public function setHash($sCode)
    {
        if (Phpfox::getParam('core.store_only_users_in_session'))
        {
            $oRequest = Phpfox_Request::instance();
            $oSession = Phpfox::getLib('session');

            $sSessionHash = $oSession->get('sessionhash');
            $bCreate = true;

            if (!empty($sSessionHash))
            {
                $bCreate = false;
                $aRow = $this->database()->select('*')
                    ->from(Phpfox::getT('log_session'))
                    ->where('session_hash = \'' . $this->database()->escape($sSessionHash) . '\'')
                    ->execute('getSlaveRow');

                if (isset($aRow['session_hash']))
                {
                    $this->database()->update(Phpfox::getT('log_session'), array('captcha_hash' => $this->_getHash($sCode, $sSessionHash)), "session_hash = '" . $sSessionHash . "'");
                }
                else
                {
                    $bCreate = true;
                }
            }

            if ($bCreate)
            {
                $sSessionHash = $oRequest->getSessionHash();
                $this->database()->insert(Phpfox::getT('log_session'), array(
                        'session_hash' => $sSessionHash,
                        'id_hash' => $oRequest->getIdHash(),
                        'captcha_hash' => $this->_getHash($sCode, $sSessionHash),
                        'user_id' => Phpfox::getUserId(),
                        'last_activity' => PHPFOX_TIME,
                        'location' => '',
                        'is_forum' => '0',
                        'forum_id' => 0,
                        'im_hide' => 0,
                        'ip_address' => '',
                        'user_agent' => ''
                    )
                );
                $oSession->set('sessionhash', $sSessionHash);
            }
        }
        else
        {
            $iId = $this->_oSession->getSessionId();

            $this->database()->update(Phpfox::getT('log_session'), array('captcha_hash' => $this->_getHash($sCode, $iId)), "session_hash = '" . $iId . "'");
        }
    }

    public function displayCaptcha($sText)
    {
        ((Phpfox::getParam('captcha.captcha_use_font') && function_exists('imagettftext')) ? $this->_writeFromFont($sText) : $this->_writeFromString($sText));

        ob_clean();

        header("X-Content-Encoded-By: phpFox " . PhpFox::getVersion());
        header("Pragma: no-cache");
        header('Cache-Control: no-store, no-cache, must-revalidate'); 
        header('Content-Type: image/jpeg');

        imagejpeg($this->_hImg);    
        imagedestroy($this->_hImg);     
    }   

    public function generateCode($sCharacters) 
    {   
        $sPossible = Phpfox::getParam('captcha.captcha_code');
        $sCode = '';
        $i = 0;
        while ($i < $sCharacters) 
        { 
            $sCode .= substr($sPossible, mt_rand(0, strlen($sPossible)-1), 1);
            $i++;
        }      
        return strtolower($sCode);
    }       

    /**
     * If a call is made to an unknown method attempt to connect
     * it to a specific plug-in with the same name thus allowing 
     * plug-in developers the ability to extend classes.
     *
     * @param string $sMethod is the name of the method
     * @param array $aArguments is the array of arguments of being passed
     */
    public function __call($sMethod, $aArguments)
    {
        /**
         * Check if such a plug-in exists and if it does call it.
         */
        if ($sPlugin = Phpfox_Plugin::get('captcha.service_captcha__call'))
        {
            return eval($sPlugin);
        }

        /**
         * No method or plug-in found we must throw a error.
         */
        Phpfox_Error::trigger('Call to undefined method ' . __CLASS__ . '::' . $sMethod . '()', E_USER_ERROR);
    }       

    private function _getHash($sCode, $sSalt)
    {
        return md5(md5($sCode) . $sSalt);
    }

    private function _writeFromFont($sText)
    {
        $iString = strlen($sText);
        $iWidth = (($iString + 5) * 10 * 2);
        $iHeight = 45;
        $iTextSize = 20;
        $sFont = Phpfox::getParam('core.dir_static') . 'image/font/' . Phpfox::getParam('captcha.captcha_font');

        if (!file_exists($sFont))
        {
            return $this->_writeFromString($sText);
        }

        $this->_imageCreate($iWidth, $iHeight);

        $nBgColor  = imageColorAllocate($this->_hImg, 255, 255, 255);
        $nTxtColor = imageColorAllocate($this->_hImg, 0, 0, 0);     

        if (!($aBox = @imagettfbbox($iTextSize, 0, $sFont, $sText)))
        {
            return $this->_writeFromString($sText);
        }

        //Find out the width and height of the text box
        $iTextW = $aBox[2] - $aBox[0];
        $iTextH = $aBox[5] - $aBox[3];        

        if (function_exists('imagefilledellipse'))
        {
            $nNoiseColor = imagecolorallocate($this->_hImg, 207, 181, 181);
            for ($i = 0; $i < ($iWidth*$iHeight) / 3; $i++)
            {
                imagefilledellipse($this->_hImg, mt_rand(0, $iWidth), mt_rand(0, $iHeight), 1, 1, $nNoiseColor);
            }
        }

        $iImageLineColor = imagecolorallocate($this->_hImg, 207, 181, 181);
        for ($i = 0; $i < ($iWidth*$iHeight) / 150; $i++)
        {
            imageline($this->_hImg, mt_rand(0, $iWidth), mt_rand(0, $iHeight), mt_rand(0, $iWidth), mt_rand(0, $iHeight), $iImageLineColor);
        }       

        // Calculate the positions
        $positionLeft = (($iWidth - $iTextW) / 2) - (20 + $iString);
        $positionTop = (($iHeight - $iTextH) / 2);

        for ($i = 0; $i < $iString; $i++) 
        { 
            if (!@imagettftext($this->_hImg, $iTextSize, 0, $positionLeft, 30, $nTxtColor, $sFont, $sText[$i]))
            {
                return $this->_writeFromString($sText);
            }

            $positionLeft += 20;
        }
    }

    private function _writeFromString($sText)
    {
        $iString = strlen($sText);
        $iWidth = (($iString + 5) * 6.4 * 2);
        $iHeight = 40;

        $this->_imageCreate($iWidth, $iHeight);

        $nBgColor  = imageColorAllocate($this->_hImg, 255, 255, 255);
        $nTxtColor = imageColorAllocate($this->_hImg, 0, 0, 0);        

        $positionLeft = 20;

        for ($i = 0; $i < $iString; $i++) 
        { 
            imagestring($this->_hImg, 5, $positionLeft, 12,  $sText[$i], $nTxtColor);

            $positionLeft += 15;
        }        
    }

    private function _imageCreate($iWidth, $iHeight)
    {
        $this->_hImg = imageCreate($iWidth, $iHeight);
    }   
}

?>

【问题讨论】:

    标签: captcha phpfox


    【解决方案1】:

    你需要删除所有使用strtolower()的代码。

    这里是固定代码:

    <?php
    /**
     * [PHPFOX_HEADER]
     */
    
    defined('PHPFOX') or exit('NO DICE!');
    
    /**
     * 
     * 
     * @copyright       [PHPFOX_COPYRIGHT]
     * @author          Raymond Benc
     * @package         Module_Captcha
     * @version         $Id: captcha.class.php 6005 2013-06-06 14:12:12Z Raymond_Benc $
     */
    class Captcha_Service_Captcha extends Phpfox_Service 
    {
        private $_oSession;
    
        /**
         * Class constructor
         */ 
        public function __construct()
        {       
            $this->_oSession = Phpfox::getService('log.session');
        }
    
        public function checkHash($sCode = null)
        {
    
            if (Phpfox::getParam('captcha.recaptcha'))
            {
    
                require_once(PHPFOX_DIR_LIB . 'recaptcha' . PHPFOX_DS . 'recaptchalib.php');        
                if (isset($_POST["recaptcha_response_field"])) 
                {           
                    $oResp = recaptcha_check_answer(Phpfox::getParam('captcha.recaptcha_private_key'), $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
    
                    if ($oResp->is_valid){          
                        return true;
                    }else{               
                        return false;
                    }           
                }   
    
                return false;
            }       
    
            if (Phpfox::getParam('core.store_only_users_in_session'))
            {
    
                $oSession = Phpfox::getLib('session');
    
                $sSessionHash = $oSession->get('sessionhash');
    
                $aRow = $this->database()->select('*')
                    ->from(Phpfox::getT('log_session'))
                    ->where('session_hash = \'' . $this->database()->escape($sSessionHash) . '\'')
                    ->execute('getSlaveRow');
    
                if (isset($aRow['session_hash']) && $this->_getHash($sCode, $aRow['session_hash']) == $aRow['captcha_hash'])
                {
                    return true;
                }
            }
            else
            {
    
                if ($this->_getHash($sCode, $this->_oSession->getSessionId()) == $this->_oSession->get('captcha_hash'))
                {
                    return true;
                }
            }
    
            return false;
        }
    
        public function setHash($sCode)
        {
            if (Phpfox::getParam('core.store_only_users_in_session'))
            {
                $oRequest = Phpfox_Request::instance();
                $oSession = Phpfox::getLib('session');
    
                $sSessionHash = $oSession->get('sessionhash');
                $bCreate = true;
    
                if (!empty($sSessionHash))
                {
                    $bCreate = false;
                    $aRow = $this->database()->select('*')
                        ->from(Phpfox::getT('log_session'))
                        ->where('session_hash = \'' . $this->database()->escape($sSessionHash) . '\'')
                        ->execute('getSlaveRow');
    
                    if (isset($aRow['session_hash']))
                    {
                        $this->database()->update(Phpfox::getT('log_session'), array('captcha_hash' => $this->_getHash($sCode, $sSessionHash)), "session_hash = '" . $sSessionHash . "'");
                    }
                    else
                    {
                        $bCreate = true;
                    }
                }
    
                if ($bCreate)
                {
                    $sSessionHash = $oRequest->getSessionHash();
                    $this->database()->insert(Phpfox::getT('log_session'), array(
                            'session_hash' => $sSessionHash,
                            'id_hash' => $oRequest->getIdHash(),
                            'captcha_hash' => $this->_getHash($sCode, $sSessionHash),
                            'user_id' => Phpfox::getUserId(),
                            'last_activity' => PHPFOX_TIME,
                            'location' => '',
                            'is_forum' => '0',
                            'forum_id' => 0,
                            'im_hide' => 0,
                            'ip_address' => '',
                            'user_agent' => ''
                        )
                    );
                    $oSession->set('sessionhash', $sSessionHash);
                }
            }
            else
            {
                $iId = $this->_oSession->getSessionId();
    
                $this->database()->update(Phpfox::getT('log_session'), array('captcha_hash' => $this->_getHash($sCode, $iId)), "session_hash = '" . $iId . "'");
            }
        }
    
        public function displayCaptcha($sText)
        {
            ((Phpfox::getParam('captcha.captcha_use_font') && function_exists('imagettftext')) ? $this->_writeFromFont($sText) : $this->_writeFromString($sText));
    
            ob_clean();
    
            header("X-Content-Encoded-By: phpFox " . PhpFox::getVersion());
            header("Pragma: no-cache");
            header('Cache-Control: no-store, no-cache, must-revalidate'); 
            header('Content-Type: image/jpeg');
    
            imagejpeg($this->_hImg);    
            imagedestroy($this->_hImg);     
        }   
    
        public function generateCode($sCharacters) 
        {   
            $sPossible = Phpfox::getParam('captcha.captcha_code');
            $sCode = '';
            $i = 0;
            while ($i < $sCharacters) 
            { 
                $sCode .= substr($sPossible, mt_rand(0, strlen($sPossible)-1), 1);
                $i++;
            }      
            return $sCode;
        }       
    
        /**
         * If a call is made to an unknown method attempt to connect
         * it to a specific plug-in with the same name thus allowing 
         * plug-in developers the ability to extend classes.
         *
         * @param string $sMethod is the name of the method
         * @param array $aArguments is the array of arguments of being passed
         */
        public function __call($sMethod, $aArguments)
        {
            /**
             * Check if such a plug-in exists and if it does call it.
             */
            if ($sPlugin = Phpfox_Plugin::get('captcha.service_captcha__call'))
            {
                return eval($sPlugin);
            }
    
            /**
             * No method or plug-in found we must throw a error.
             */
            Phpfox_Error::trigger('Call to undefined method ' . __CLASS__ . '::' . $sMethod . '()', E_USER_ERROR);
        }       
    
        private function _getHash($sCode, $sSalt)
        {
            return md5(md5($sCode) . $sSalt);
        }
    
        private function _writeFromFont($sText)
        {
            $iString = strlen($sText);
            $iWidth = (($iString + 5) * 10 * 2);
            $iHeight = 45;
            $iTextSize = 20;
            $sFont = Phpfox::getParam('core.dir_static') . 'image/font/' . Phpfox::getParam('captcha.captcha_font');
    
            if (!file_exists($sFont))
            {
                return $this->_writeFromString($sText);
            }
    
            $this->_imageCreate($iWidth, $iHeight);
    
            $nBgColor  = imageColorAllocate($this->_hImg, 255, 255, 255);
            $nTxtColor = imageColorAllocate($this->_hImg, 0, 0, 0);     
    
            if (!($aBox = @imagettfbbox($iTextSize, 0, $sFont, $sText)))
            {
                return $this->_writeFromString($sText);
            }
    
            //Find out the width and height of the text box
            $iTextW = $aBox[2] - $aBox[0];
            $iTextH = $aBox[5] - $aBox[3];        
    
            if (function_exists('imagefilledellipse'))
            {
                $nNoiseColor = imagecolorallocate($this->_hImg, 207, 181, 181);
                for ($i = 0; $i < ($iWidth*$iHeight) / 3; $i++)
                {
                    imagefilledellipse($this->_hImg, mt_rand(0, $iWidth), mt_rand(0, $iHeight), 1, 1, $nNoiseColor);
                }
            }
    
            $iImageLineColor = imagecolorallocate($this->_hImg, 207, 181, 181);
            for ($i = 0; $i < ($iWidth*$iHeight) / 150; $i++)
            {
                imageline($this->_hImg, mt_rand(0, $iWidth), mt_rand(0, $iHeight), mt_rand(0, $iWidth), mt_rand(0, $iHeight), $iImageLineColor);
            }       
    
            // Calculate the positions
            $positionLeft = (($iWidth - $iTextW) / 2) - (20 + $iString);
            $positionTop = (($iHeight - $iTextH) / 2);
    
            for ($i = 0; $i < $iString; $i++) 
            { 
                if (!@imagettftext($this->_hImg, $iTextSize, 0, $positionLeft, 30, $nTxtColor, $sFont, $sText[$i]))
                {
                    return $this->_writeFromString($sText);
                }
    
                $positionLeft += 20;
            }
        }
    
        private function _writeFromString($sText)
        {
            $iString = strlen($sText);
            $iWidth = (($iString + 5) * 6.4 * 2);
            $iHeight = 40;
    
            $this->_imageCreate($iWidth, $iHeight);
    
            $nBgColor  = imageColorAllocate($this->_hImg, 255, 255, 255);
            $nTxtColor = imageColorAllocate($this->_hImg, 0, 0, 0);        
    
            $positionLeft = 20;
    
            for ($i = 0; $i < $iString; $i++) 
            { 
                imagestring($this->_hImg, 5, $positionLeft, 12,  $sText[$i], $nTxtColor);
    
                $positionLeft += 15;
            }        
        }
    
        private function _imageCreate($iWidth, $iHeight)
        {
            $this->_hImg = imageCreate($iWidth, $iHeight);
        }   
    }
    
    ?>
    

    【讨论】:

      【解决方案2】:

      您可能会遇到问题,也可能是您的解决方案。请记住,您的数据库上的排序规则也需要考虑在内。如果你使用 utf8_general_ci 那么ci 表示它是case insensitive 所以无论你在代码方面做什么,数据库都不关心它的大小写。在您的搜索栏上进行测试,查找一些字符串,假设是用户 John Smith,然后搜索与 john smith 相同的字符串...如果搜索产生相同的结果,那么您的数据库不关心区分大小写的搜索。我不知道您的验证码将其答案保存在哪里,但如果它确实将它们保存在数据库中,并且它与您在其中的问题相匹配,那么它可能不在乎它是大写还是小写。

      只是一个提醒。您需要在自己的环境中进行测试。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-24
        • 2015-12-26
        • 2020-08-02
        • 2011-05-12
        相关资源
        最近更新 更多