رفتن به مطلب

نیاز به دستور کپچا بدون افزونه


پست های پیشنهاد شده

این کلاس کپچا هست که خودم نوشتم و استفاده میکنم

<?php

/**
 * @author lord_viper
 * @copyright 2013
 */

class bn_captcha
{
    private $font_size    = 5;
    private $fontfilename = '';
    private $bg_color     = array(255,255,255);
    private $text_color   = array(0,0,0);
    private $line         = 0;
    private $noise        = 0;
    private $elipse       = 0;
    private $elfill       = false;
    private $text         = '';
    private $img;
    protected static $instance;

    function __construct()
    {
        if (session_status() == PHP_SESSION_NONE)
        //if (!isset($_SESSION))
        session_start();
    }

    public static function instance()
    {
        if(!isset(self::$instance))
        self::$instance = new self();

        return self::$instance;
    }
    
    private function RandomString($length = 10, $type = 'char',$repeat=2)
    {
        $Special = '!@#$%^&*()-_ []{}<>~+=,.;:/?|';
        $number  = '0123456789';
        $chars   = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        switch($type)
        {
            case 'all' :$str = $Special.$number.$chars;break;
            case 'char':$str = $chars;break;
            case 'num' :$str = $number;break;
            default    :$str = $chars.$number;
        }
        return substr(str_shuffle(str_repeat($str,$repeat)),0,$length);
    }
    
    public function line($count)
    {
        $this->line = $count;
        return $this;
    }
    
    public function noise($count)
    {
        $this->noise = $count;
        return $this;
    }
    
    public function font($fontname,$fontsize)
    {
        $this->fontfilename = $fontname;
        $this->font_size    = $fontsize;
        return $this;
    }
    
    public function ellipse($count,$fill=false)
    {
        $this->elipse = $count;
        $this->elfill = $fill;
        return $this;
    }
    
    public function text($text='')
    {
        $this->text = $text;
        return $this;
    }
    
    public function show($text_count=5,$text_type='all',$bgcolor='#000',$txtcolor='#FFFFFF',$echo=true)
    {
        if(empty($this->text)){
            $this->text    = self::RandomString($text_count,$text_type);
            $_SESSION['__captcha'] = strtolower($this->text);
            $this->text    = wordwrap($this->text,1,' ',true) ;
        }
        $size = self::get_img_size();
        $this->img = imagecreatetruecolor($size['width'],$size['height']);
        self::draw_ellipse($size);
        self::draw_line($size);
        imagefill($this->img,0,0,hexdec($bgcolor));
        if(empty($this->fontfilename))
            imagestring($this->img,5,10,rand(5,15),$this->text,hexdec($txtcolor));
        else
            imagettftext($this->img,$this->font_size,0,10,rand($size['height']-15,$size['height']-5),hexdec($txtcolor),$this->fontfilename,$this->text);
        self::draw_noise($size);
        ob_start();    
        ImagePng($this->img);
        $img = base64_encode(ob_get_clean());
        if($echo)
        echo "<img src='data:image/jpeg;base64,$img' />";
        else
        return "<img src='data:image/jpeg;base64,$img' />";
    }
    
    public function check_captcha($post)
    {
        return ($post==$_SESSION['__captcha']?true:false);
    }
//______________________________________________________________________________________________________________________________    
    private function get_img_size()
    {
        $ret = array();
        if(empty($this->fontfilename)){
            $w    = imagefontwidth($this->font_size);
            $h    = imagefontheight($this->font_size);
            $ret['width'] = ($w * strlen($this->text)) + 20;
            $ret['height'] = $h + 20;
        }else{
            $siz = imagettfbbox($this->font_size,0,$this->fontfilename,$this->text);
            $ret['width'] = $siz[4]+20;
            $ret['height'] = abs($siz[7])+20;                      
        }
        return $ret;
    }
    
    private function draw_line($size)
    {
        if($this->line>0){
            $width  = $size['width'];
            $height = $size['height'];
            $minwidth = intval($size['width'] / 4);
            for ($i = 0; $i <= $this->line; $i++){
                imageline($this->img, rand(1, $minwidth), rand(1, $height), rand($width - $minwidth,$width),
                rand(1, $height), rand(1,16000000));
            }
        }
    }
    
    private function draw_noise($size)
    {
        if($this->noise>0){
            $width  = $size['width'];
            $height = $size['height'];
            for ($i = 0; $i <= $this->noise; $i++){
                imagesetpixel($this->img, rand(1, $width), rand(1, $height), rand(1,16000000));
            }
        }
    }
    
    private function draw_ellipse($size)
    {
        if($this->elipse>0){
            $width  = $size['width'];
            $height = $size['height'];
            for ($i = 0; $i < $this->elipse; $i++){
                $cx = (int)rand(15, $width - 5);
                $cy = (int)rand(15, $height - 5);
                $h  = rand(1, 30);
                $w  = rand(1, 30);
                if ($this->elfill)
                    imagefilledellipse($this->img, $cx, $cy, $w, $h,rand(1,16000000));
                else
                    imageellipse($this->img, $cx, $cy, $w, $h,rand(1,16000000));
            }             
        }       
    }
}
?>

برای استفاده به صورت زیر عمل کنید

 

$cap = new bn_captcha;

//check kardan captcha
//$_POST['captcha'] مقدار input کد امنیتی هست name=captcha
if($cap->check_captcha($_POST['captcha']))
echo 'captcha is true';
else
echo 'captcha is false';

//ایجاد کپچا فقط عدد 6 کاراکتر
echo $cap->show(6,'num');
//ایجاد کپچا عدد - حروف - سیمبل 6 کاراکتر
echo $cap->show(6,'all');
// ایجاد کپچت فقط حروف 6 کاراکتر پس زمینه مشکی رنگ حروف سفی
echo $cap->show(6,'char','#000','#FFF');

 

لینک به ارسال
  • 3 هفته بعد...
در در 8/8/2017 at 13:02، lord_viper گفته است :

این کلاس کپچا هست که خودم نوشتم و استفاده میکنم


<?php

/**
 * @author lord_viper
 * @copyright 2013
 */

class bn_captcha
{
    private $font_size    = 5;
    private $fontfilename = '';
    private $bg_color     = array(255,255,255);
    private $text_color   = array(0,0,0);
    private $line         = 0;
    private $noise        = 0;
    private $elipse       = 0;
    private $elfill       = false;
    private $text         = '';
    private $img;
    protected static $instance;

    function __construct()
    {
        if (session_status() == PHP_SESSION_NONE)
        //if (!isset($_SESSION))
        session_start();
    }

    public static function instance()
    {
        if(!isset(self::$instance))
        self::$instance = new self();

        return self::$instance;
    }
    
    private function RandomString($length = 10, $type = 'char',$repeat=2)
    {
        $Special = '!@#$%^&*()-_ []{}<>~+=,.;:/?|';
        $number  = '0123456789';
        $chars   = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        switch($type)
        {
            case 'all' :$str = $Special.$number.$chars;break;
            case 'char':$str = $chars;break;
            case 'num' :$str = $number;break;
            default    :$str = $chars.$number;
        }
        return substr(str_shuffle(str_repeat($str,$repeat)),0,$length);
    }
    
    public function line($count)
    {
        $this->line = $count;
        return $this;
    }
    
    public function noise($count)
    {
        $this->noise = $count;
        return $this;
    }
    
    public function font($fontname,$fontsize)
    {
        $this->fontfilename = $fontname;
        $this->font_size    = $fontsize;
        return $this;
    }
    
    public function ellipse($count,$fill=false)
    {
        $this->elipse = $count;
        $this->elfill = $fill;
        return $this;
    }
    
    public function text($text='')
    {
        $this->text = $text;
        return $this;
    }
    
    public function show($text_count=5,$text_type='all',$bgcolor='#000',$txtcolor='#FFFFFF',$echo=true)
    {
        if(empty($this->text)){
            $this->text    = self::RandomString($text_count,$text_type);
            $_SESSION['__captcha'] = strtolower($this->text);
            $this->text    = wordwrap($this->text,1,' ',true) ;
        }
        $size = self::get_img_size();
        $this->img = imagecreatetruecolor($size['width'],$size['height']);
        self::draw_ellipse($size);
        self::draw_line($size);
        imagefill($this->img,0,0,hexdec($bgcolor));
        if(empty($this->fontfilename))
            imagestring($this->img,5,10,rand(5,15),$this->text,hexdec($txtcolor));
        else
            imagettftext($this->img,$this->font_size,0,10,rand($size['height']-15,$size['height']-5),hexdec($txtcolor),$this->fontfilename,$this->text);
        self::draw_noise($size);
        ob_start();    
        ImagePng($this->img);
        $img = base64_encode(ob_get_clean());
        if($echo)
        echo "<img src='data:image/jpeg;base64,$img' />";
        else
        return "<img src='data:image/jpeg;base64,$img' />";
    }
    
    public function check_captcha($post)
    {
        return ($post==$_SESSION['__captcha']?true:false);
    }
//______________________________________________________________________________________________________________________________    
    private function get_img_size()
    {
        $ret = array();
        if(empty($this->fontfilename)){
            $w    = imagefontwidth($this->font_size);
            $h    = imagefontheight($this->font_size);
            $ret['width'] = ($w * strlen($this->text)) + 20;
            $ret['height'] = $h + 20;
        }else{
            $siz = imagettfbbox($this->font_size,0,$this->fontfilename,$this->text);
            $ret['width'] = $siz[4]+20;
            $ret['height'] = abs($siz[7])+20;                      
        }
        return $ret;
    }
    
    private function draw_line($size)
    {
        if($this->line>0){
            $width  = $size['width'];
            $height = $size['height'];
            $minwidth = intval($size['width'] / 4);
            for ($i = 0; $i <= $this->line; $i++){
                imageline($this->img, rand(1, $minwidth), rand(1, $height), rand($width - $minwidth,$width),
                rand(1, $height), rand(1,16000000));
            }
        }
    }
    
    private function draw_noise($size)
    {
        if($this->noise>0){
            $width  = $size['width'];
            $height = $size['height'];
            for ($i = 0; $i <= $this->noise; $i++){
                imagesetpixel($this->img, rand(1, $width), rand(1, $height), rand(1,16000000));
            }
        }
    }
    
    private function draw_ellipse($size)
    {
        if($this->elipse>0){
            $width  = $size['width'];
            $height = $size['height'];
            for ($i = 0; $i < $this->elipse; $i++){
                $cx = (int)rand(15, $width - 5);
                $cy = (int)rand(15, $height - 5);
                $h  = rand(1, 30);
                $w  = rand(1, 30);
                if ($this->elfill)
                    imagefilledellipse($this->img, $cx, $cy, $w, $h,rand(1,16000000));
                else
                    imageellipse($this->img, $cx, $cy, $w, $h,rand(1,16000000));
            }             
        }       
    }
}
?>

برای استفاده به صورت زیر عمل کنید

 


$cap = new bn_captcha;

//check kardan captcha
//$_POST['captcha'] مقدار input کد امنیتی هست name=captcha
if($cap->check_captcha($_POST['captcha']))
echo 'captcha is true';
else
echo 'captcha is false';

//ایجاد کپچا فقط عدد 6 کاراکتر
echo $cap->show(6,'num');
//ایجاد کپچا عدد - حروف - سیمبل 6 کاراکتر
echo $cap->show(6,'all');
// ایجاد کپچت فقط حروف 6 کاراکتر پس زمینه مشکی رنگ حروف سفی
echo $cap->show(6,'char','#000','#FFF');

 

سلام لرد عزیز مرسی که دانسته هاتو به اشتراک قرار دادی 

فقط ببخشید این قضیه استفادشو یه توضیح کوچولو بدی ممنون میشم چون یکم گیج کنندس

لینک به ارسال
در در 8/8/2017 at 01:32، lord_viper گفته است :

این کلاس کپچا هست که خودم نوشتم و استفاده میکنم


<?php

/**
 * @author lord_viper
 * @copyright 2013
 */

class bn_captcha
{
    private $font_size    = 5;
    private $fontfilename = '';
    private $bg_color     = array(255,255,255);
    private $text_color   = array(0,0,0);
    private $line         = 0;
    private $noise        = 0;
    private $elipse       = 0;
    private $elfill       = false;
    private $text         = '';
    private $img;
    protected static $instance;

    function __construct()
    {
        if (session_status() == PHP_SESSION_NONE)
        //if (!isset($_SESSION))
        session_start();
    }

    public static function instance()
    {
        if(!isset(self::$instance))
        self::$instance = new self();

        return self::$instance;
    }
    
    private function RandomString($length = 10, $type = 'char',$repeat=2)
    {
        $Special = '!@#$%^&*()-_ []{}<>~+=,.;:/?|';
        $number  = '0123456789';
        $chars   = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        switch($type)
        {
            case 'all' :$str = $Special.$number.$chars;break;
            case 'char':$str = $chars;break;
            case 'num' :$str = $number;break;
            default    :$str = $chars.$number;
        }
        return substr(str_shuffle(str_repeat($str,$repeat)),0,$length);
    }
    
    public function line($count)
    {
        $this->line = $count;
        return $this;
    }
    
    public function noise($count)
    {
        $this->noise = $count;
        return $this;
    }
    
    public function font($fontname,$fontsize)
    {
        $this->fontfilename = $fontname;
        $this->font_size    = $fontsize;
        return $this;
    }
    
    public function ellipse($count,$fill=false)
    {
        $this->elipse = $count;
        $this->elfill = $fill;
        return $this;
    }
    
    public function text($text='')
    {
        $this->text = $text;
        return $this;
    }
    
    public function show($text_count=5,$text_type='all',$bgcolor='#000',$txtcolor='#FFFFFF',$echo=true)
    {
        if(empty($this->text)){
            $this->text    = self::RandomString($text_count,$text_type);
            $_SESSION['__captcha'] = strtolower($this->text);
            $this->text    = wordwrap($this->text,1,' ',true) ;
        }
        $size = self::get_img_size();
        $this->img = imagecreatetruecolor($size['width'],$size['height']);
        self::draw_ellipse($size);
        self::draw_line($size);
        imagefill($this->img,0,0,hexdec($bgcolor));
        if(empty($this->fontfilename))
            imagestring($this->img,5,10,rand(5,15),$this->text,hexdec($txtcolor));
        else
            imagettftext($this->img,$this->font_size,0,10,rand($size['height']-15,$size['height']-5),hexdec($txtcolor),$this->fontfilename,$this->text);
        self::draw_noise($size);
        ob_start();    
        ImagePng($this->img);
        $img = base64_encode(ob_get_clean());
        if($echo)
        echo "<img src='data:image/jpeg;base64,$img' />";
        else
        return "<img src='data:image/jpeg;base64,$img' />";
    }
    
    public function check_captcha($post)
    {
        return ($post==$_SESSION['__captcha']?true:false);
    }
//______________________________________________________________________________________________________________________________    
    private function get_img_size()
    {
        $ret = array();
        if(empty($this->fontfilename)){
            $w    = imagefontwidth($this->font_size);
            $h    = imagefontheight($this->font_size);
            $ret['width'] = ($w * strlen($this->text)) + 20;
            $ret['height'] = $h + 20;
        }else{
            $siz = imagettfbbox($this->font_size,0,$this->fontfilename,$this->text);
            $ret['width'] = $siz[4]+20;
            $ret['height'] = abs($siz[7])+20;                      
        }
        return $ret;
    }
    
    private function draw_line($size)
    {
        if($this->line>0){
            $width  = $size['width'];
            $height = $size['height'];
            $minwidth = intval($size['width'] / 4);
            for ($i = 0; $i <= $this->line; $i++){
                imageline($this->img, rand(1, $minwidth), rand(1, $height), rand($width - $minwidth,$width),
                rand(1, $height), rand(1,16000000));
            }
        }
    }
    
    private function draw_noise($size)
    {
        if($this->noise>0){
            $width  = $size['width'];
            $height = $size['height'];
            for ($i = 0; $i <= $this->noise; $i++){
                imagesetpixel($this->img, rand(1, $width), rand(1, $height), rand(1,16000000));
            }
        }
    }
    
    private function draw_ellipse($size)
    {
        if($this->elipse>0){
            $width  = $size['width'];
            $height = $size['height'];
            for ($i = 0; $i < $this->elipse; $i++){
                $cx = (int)rand(15, $width - 5);
                $cy = (int)rand(15, $height - 5);
                $h  = rand(1, 30);
                $w  = rand(1, 30);
                if ($this->elfill)
                    imagefilledellipse($this->img, $cx, $cy, $w, $h,rand(1,16000000));
                else
                    imageellipse($this->img, $cx, $cy, $w, $h,rand(1,16000000));
            }             
        }       
    }
}
?>

برای استفاده به صورت زیر عمل کنید

 


$cap = new bn_captcha;

//check kardan captcha
//$_POST['captcha'] مقدار input کد امنیتی هست name=captcha
if($cap->check_captcha($_POST['captcha']))
echo 'captcha is true';
else
echo 'captcha is false';

//ایجاد کپچا فقط عدد 6 کاراکتر
echo $cap->show(6,'num');
//ایجاد کپچا عدد - حروف - سیمبل 6 کاراکتر
echo $cap->show(6,'all');
// ایجاد کپچت فقط حروف 6 کاراکتر پس زمینه مشکی رنگ حروف سفی
echo $cap->show(6,'char','#000','#FFF');

 

کد اول در function.php قرار دهید .

و کد دوم در هر صفحه ی که می خواهید کد کپچا نمایش دهد قرار دهید.

به عنوان مثال برای نمایش در صفحه  دلخواه فقط کافیه :

$cap = new bn_captcha;
echo $cap->show();

به همین سادگی  کد کپچا نمایش داده خواهد شد.

حال اگر با جزئیات بیشتر مد نظر هست می تونید از کد پایین استفاده کنید.

$cap = new bn_captcha;

//check kardan captcha
//$_POST['captcha'] مقدار input کد امنیتی هست name=captcha
if($cap->check_captcha($_POST['captcha']))
echo 'captcha is true';
else
echo 'captcha is false';

//ایجاد کپچا فقط عدد 6 کاراکتر
echo $cap->show(6,'num');
//ایجاد کپچا عدد - حروف - سیمبل 6 کاراکتر
echo $cap->show(6,'all');
// ایجاد کپچت فقط حروف 6 کاراکتر پس زمینه مشکی رنگ حروف سفی
echo $cap->show(6,'char','#000','#FFF');

 

ویرایش شده توسط Mirrajabi
لینک به ارسال

به گفتگو بپیوندید

هم اکنون می توانید مطلب خود را ارسال نمایید و بعداً ثبت نام کنید. اگر حساب کاربری دارید، برای ارسال با حساب کاربری خود اکنون وارد شوید .

مهمان
ارسال پاسخ به این موضوع ...

×   شما در حال چسباندن محتوایی با قالب بندی هستید.   حذف قالب بندی

  تنها استفاده از 75 اموجی مجاز می باشد.

×   لینک شما به صورت اتوماتیک جای گذاری شد.   نمایش به صورت لینک

×   محتوای قبلی شما بازگردانی شد.   پاک کردن محتوای ویرایشگر

×   شما مستقیما نمی توانید تصویر خود را قرار دهید. یا آن را اینجا بارگذاری کنید یا از یک URL قرار دهید.

×
×
  • اضافه کردن...