Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 为什么我加了验证码,别人还是可以刷票呢?

为什么我加了验证码,别人还是可以刷票呢?

Jun 23, 2016 pm 01:05 PM

为什么我加了验证码,别人还是可以刷票呢?下面是获取验证码的代码,是不是验证码太简单了,应该怎么弄复杂一点?

/**
 * 验证码图片类
*/

if (!defined('IN_FDYU'))
{
    die('Hacking attempt');
}

class captcha
{
    /**
     * 背景图片所在目录
     *
     * @var string  $folder
     */
 
    //var $folder = ROOT_PATH . 'includes/captcha/';

var $folder = '';

    /**
     * 图片的文件类型
     *
     * @var string  $img_type
     */
    var $img_type   = 'png';

    /*------------------------------------------------------ */
    //-- 存在session中的名称
    /*------------------------------------------------------ */
    var $session_word = 'captcha_word';

    /**
     * 背景图片以及背景颜色
     *
     * 0 => 背景图片的文件名
     * 1 => Red, 2 => Green, 3 => Blue
     * @var array   $themes
     */
    var $themes_jpg = array(
        1 => array('captcha_bg1.jpg', 255, 255, 255),
        2 => array('captcha_bg2.jpg', 0, 0, 0),
        3 => array('captcha_bg3.jpg', 0, 0, 0),
        4 => array('captcha_bg4.jpg', 255, 255, 255),
        5 => array('captcha_bg5.jpg', 255, 255, 255),
    );

    var $themes_gif = array(
        1 => array('captcha_bg1.gif', 255, 255, 255),
        2 => array('captcha_bg2.gif', 0, 0, 0),
        3 => array('captcha_bg3.gif', 0, 0, 0),
        4 => array('captcha_bg4.gif', 255, 255, 255),
        5 => array('captcha_bg5.gif', 255, 255, 255),
    );

    /**
     * 图片的宽度
     *
     * @var integer $width
     */
    var $width      = 38;

    /**
     * 图片的高度
     *
     * @var integer $height
     */
    var $height     = 16;

/**
     * 构造函数
     *
     * @access  public
     * @param
     *
     * @return void
     */
    function __construct($folder = '', $width = 38, $height = 16)
    {
        $this->captcha($folder, $width, $height);
    }

    /**
     * 构造函数
     *
     * @access  public
     * @param   string  $folder     背景图片所在目录
     * @param   integer $width      图片宽度
     * @param   integer $height     图片高度
     * @return  bool
     */
    function captcha($folder = '', $width = 38, $height = 16)
    {
$folder = ROOT_PATH . 'includes/captcha/';

        if (!empty($folder))
        {
            $this->folder = $folder;
        }

        $this->width    = $width;
        $this->height   = $height;

        /* 检查是否支持 GD */
        if (PHP_VERSION >= '4.3')
        {

            return (function_exists('imagecreatetruecolor') || function_exists('imagecreate'));
        }
        else
        {

            return (((imagetypes() & IMG_GIF) > 0) || ((imagetypes() & IMG_JPG)) > 0 );
        }
    }

    


    /**
     * 检查给出的验证码是否和session中的一致
     *
     * @access  public
     * @param   string  $word   验证码
     * @return  bool
     */
    function check_word($word)
    {
        $recorded = isset($_SESSION[$this->session_word]) ? base64_decode($_SESSION[$this->session_word]) : '';
        $given    = $this->encrypts_word(strtoupper($word));

        return (preg_match("/$given/", $recorded));
    }

    /**
     * 生成图片并输出到浏览器
     *
     * @access  public
     * @param   string  $word   验证码
     * @return  mix
     */
    function generate_image($word = false)
    {
        if (!$word)
        {
            $word = $this->generate_word();
        }

        /* 记录验证码到session */
        $this->record_word($word);

        /* 验证码长度 */
        $letters = strlen($word);

        /* 选择一个随机的方案 */
        mt_srand((double) microtime() * 1000000);

        if (function_exists('imagecreatefromjpeg') && ((imagetypes() & IMG_JPG) > 0))
        {
            $theme  = $this->themes_jpg[mt_rand(1, count($this->themes_jpg))];
        }
        else
        {
            $theme  = $this->themes_gif[mt_rand(1, count($this->themes_gif))];
        }

        if (!file_exists($this->folder . $theme[0]))
        {
            return false;
        }
        else
        {
            $img_bg    = (function_exists('imagecreatefromjpeg') && ((imagetypes() & IMG_JPG) > 0)) ?
                            imagecreatefromjpeg($this->folder . $theme[0]) : imagecreatefromgif($this->folder . $theme[0]);
            $bg_width  = imagesx($img_bg);
            $bg_height = imagesy($img_bg);

            $img_org   = ((function_exists('imagecreatetruecolor')) && PHP_VERSION >= '4.3') ?
                          imagecreatetruecolor($this->width, $this->height) : imagecreate($this->width, $this->height);

            /* 将背景图象复制原始图象并调整大小 */
            if (function_exists('imagecopyresampled') && PHP_VERSION >= '4.3') // GD 2.x
            {
                imagecopyresampled($img_org, $img_bg, 0, 0, 0, 0, $this->width, $this->height, $bg_width, $bg_height);
            }
            else // GD 1.x
            {
                imagecopyresized($img_org, $img_bg, 0, 0, 0, 0, $this->width, $this->height, $bg_width, $bg_height);
            }
            imagedestroy($img_bg);

            $clr = imagecolorallocate($img_org, $theme[1], $theme[2], $theme[3]);

            /* 绘制边框 */
            //imagerectangle($img_org, 0, 0, $this->width - 1, $this->height - 1, $clr);

            /* 获得验证码的高度和宽度 */
            $x = ($this->width - (imagefontwidth(5) * $letters)) / 2;
            $y = ($this->height - imagefontheight(5)) / 2;
            imagestring($img_org, 5, $x, $y, $word, $clr);

            header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');

            // HTTP/1.1
            header('Cache-Control: private, no-store, no-cache, must-revalidate');
            header('Cache-Control: post-check=0, pre-check=0, max-age=0', false);

            // HTTP/1.0
            header('Pragma: no-cache');
            if ($this->img_type == 'jpeg' && function_exists('imagecreatefromjpeg'))
            {
                header('Content-type: image/jpeg');
                imageinterlace($img_org, 1);
                imagejpeg($img_org, false, 95);
            }
            else
            {
                header('Content-type: image/png');
                imagepng($img_org);
            }

            imagedestroy($img_org);

            return true;
        }
    }

    /*------------------------------------------------------ */
    //-- PRIVATE METHODs
    /*------------------------------------------------------ */

    /**
     * 对需要记录的串进行加密
     *
     * @access  private
     * @param   string  $word   原始字符串
     * @return  string
     */
    function encrypts_word($word)
    {
        return substr(md5($word), 1, 10);
    }

    /**
     * 将验证码保存到session
     *
     * @access  private
     * @param   string  $word   原始字符串
     * @return  void
     */
    function record_word($word)
    {
        $_SESSION[$this->session_word] = base64_encode($this->encrypts_word($word));
    }

    /**
     * 生成随机的验证码
     *
     * @access  private
     * @param   integer $length     验证码长度
     * @return  string
     */
    function generate_word($length = 4)
    {
        $chars = '1234567890ABCDEFGHJKLMNPQRSTUVWXYZ';

        for ($i = 0, $count = strlen($chars); $i         {
            $arr[$i] = $chars[$i];
        }

        mt_srand((double) microtime() * 1000000);
        shuffle($arr);

        return substr(implode('', $arr), 5, $length);
    }
}

?>


回复讨论(解决方案)

只看到 check_word 的定义,没看到 check_word 的调用
也没看到 session_start

在提交页面里有调用的:
include_once('includes/cls_captcha.php');
$validator = new captcha();
if (!$validator->check_word($t_captcha))
{
$ajax['record']['all_tp'] = 9;
echo json_encode($ajax);
exit;
}

那 session_start(); 在哪里?

好像是没有,那怎么弄?

session_start(); 这个也有的,在另一个文件里

require(ROOT_PATH . 'includes/init.php');每个页面都有这个文件,session_start(); 就在这个文件里

你传入的 验证码在哪里?怎么没传给 $validator


                                          
                                          
                                          
                                          
                                           为什么我加了验证码,别人还是可以刷票呢?
                                          
                                        



js:
function get_toupiao(tp_id,all_toupiao,hd_id,tp_star_time,tp_end_time)
{
if($.trim($("input[name=captcha]").val()) == '1')
{
if($.trim($("input[name=t_captcha]").val()) == '')
{
alert("验证码不能为空!");
$("input[name=t_captcha]").focus();
return false;
}
}
var t_captcha=$.trim($("input[name=t_captcha]").val());
var captcha=$.trim($("input[name=captcha]").val());
var originator=$.trim($("input[name=originator]").val());
$.get("/show.php?act=get_toupiao",{
tp_id:tp_id,
all_toupiao:all_toupiao,
hd_id:hd_id,
tp_star_time:tp_star_time,
tp_end_time:tp_end_time,
t_captcha:t_captcha,
captcha:captcha,
originator:originator,
async:false,
rand: Math.random()
},function(data){
if(data.record.all_tp=="9")
{
alert("验证码输入错误^_^!");
                return false;
}
/*if(data.record.all_tp=="8")
{
alert("请进行有效投票哦^_^!");
                return false;
}
if(data.record.all_tp=="7")
{
alert("同时投票的人太多,请稍候重试^_^!");
                return false;
}*/
if(data.record.all_tp=="5")
{
alert("今天您已经投票,请明天再投吧!");
                return false;
}
if(data.record.all_tp=="6")
{
alert("今天您已经投票,请明天再投吧!");
                return false;
}
if(data.record.all_tp=="3")
{
alert("投票时间已过,不能投票");
                return false;
}
if(data.record.all_tp=="4")
{
alert("投票时间还没到,不能投票");
                return false;
}
if(data.record.all_tp=="1")
{
alert("该活动您已投票,不能重复投票");
                return false;
}
if(data.record.all_tp=="2")
{
alert("该项目您已投票,不能重复投票");
                return false;
}
if(data.status.code=="1")
{
//成功
$("#tp_"+tp_id+"").empty();
$("#tp_"+tp_id+"").append(""+data.record.tp_count+"");
$("#captcha_"+tp_id+"").empty();
$("#captcha_"+tp_id+"").append(""+data.status.captcha+"");
alert("投票成功!");
                return false;
}
},"json");
}

PHP:
elseif($action == 'get_toupiao')
{
global $fdyu,$db;
$tp_id = intval($_GET['tp_id']);
$hd_id = intval($_GET['hd_id']);
$all_toupiao = $_GET['all_toupiao'];
$tp_star_time = $_GET['tp_star_time'];
$tp_end_time = $_GET['tp_end_time'];
$t_captcha = $_GET['t_captcha'];
$captcha = intval($_GET['captcha']);
$ip=getClientIP();
$ajax = array();

//是否需要验证码
if($captcha==1)
{
include_once('includes/cls_captcha.php');
$validator = new captcha();
if (!$validator->check_word($t_captcha))
{
$ajax['record']['all_tp'] = 9;
echo json_encode($ajax);
exit;
}
}

提供检查思路:
1.如果输入为空是否可以通过
2.把输入的验证码和session的验证码打印出来看看
3.验证通过后,需要把旧的验证码删除,否则用户可以不刷新页面,一直用这个验证码提交不同的数据

可以参考: http://blog.csdn.net/fdipzone/article/details/7295496

 以前刷Yii哥帖子时发现csdn就有楼上说的第3个bug,不过刷完转天他们就修复了
验证码防刷票是个很不靠谱的东西,如果投票数牵扯到利益,那用户完全可以去买付费的验证码识别
我觉得归根到底还是得IP去重

IP限了,但也没用,刷票软件可以刷不同的IP

10楼,我发现确实是像你说的第三点,因为我在数据库里记录了验证码,发现有重复的验证码!那怎么删除旧的验证码呢?

if($action == 'get_toupiao')
{
。。。。。。省略
if ($_SESSION['code_1']!=$t_captcha)
{
    $ajax['record']['all_tp']   = 9;
    echo json_encode($ajax);
    exit;
}
。。。。。省略
$ajax['status']['code'] = 1; //成功
echo json_encode($ajax);
exit;
}

是在$ajax['status']['code'] = 1; //成功这个上面加unset($_SESSION['code_1']);这个吗?

就是通过验证后,把session的验证码清空就可以了。
保证令牌只能用一次

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles