PHP Build Validator

Source: Internet
Author: User
Tags getcolor rand

<?php session_start ();?>
<?php
{
$authcode = new Authcode ();
if ($authcode->geturifilename () = = "authcode.php")
{
$authcode->outputimg ();
}
}
function Chkauthcode ($Authcode)
{
if ($_session[' authcode '] = = $Authcode)
{
$rtn = true;
}
Else
{
$rtn = false;
}
$_session[' authcode '] = rand (0, 999999);
return $RTN;
}
Class Authcode
{
/* Public Variables * *
* Private Variables * *
var $image;
#
var $sBgcolor;
var $nWidth;
var $nHeight;
var $nLen;
var $bNoise;
var $nNoise;
var $bBorder;
var $aFontlist;
* Constractor * *
function Authcode ()
{
$this->sbgcolor = "#FFCC00";
$this->nwidth = 70;
$this->nheight = 25;
$this->nleftmargin = 5;
$this->nrightmargin = 5;
$this->ntopmargin = 3;
$this->nbottommargin = 2;
$this->nlen = 4;
$this->bnoise = true;
$this->nnoisepoint = 50;
$this->nnoiseline = 5;
$this->bborder = true;

$this->afontlist[0] = "./fonts/arial.ttf";
$this->afontlist[1] = "./fonts/comic.ttf";
$this->afontlist[2] = "./fonts/raavi.ttf";
$this->afontlist[3] = "./fonts/verdanai.ttf";
$this->afontlist[4] = "./fonts/tahoma.ttf";
$this->afontlist[5] = "./fonts/shruti.ttf";
$this->afontlist[6] = "./fonts/bkant. TTF ";
$this->afontlist[7] = "./fonts/comicbd.ttf";
$this->afontlist[8] = "./fonts/courbi.ttf";
$this->afontlist[9] = "./fonts/times.ttf";
}

function outputimg ()
{
$this->image = "";
$this->image = imagecreate ($this->nwidth, $this->nheight);
$back = $this->getcolor ($this->sbgcolor);
Imagefilledrectangle ($this->image, 0, 0, $this->nwidth, $this->nheight, $back);
$size = ($this->nwidth-$this->nleftmargin-$this->nrightmargin)/$this->nlen;
if ($size > ($this->nheight-$this->ntopmargin-$this->nbottommargin))
$size = $this->nheight-$this->ntopmargin-$this->nbottommargin;

$left = ($this->nwidth-$this->nlen* ($size + $size/10))/2 + $this->nleftmargin;
$code = "";
For ($i =0 $i < $this->nlen; $i + +)
{
$randtext = rand (0, 9);
$code. = $randtext;
$textColor = Imagecolorallocate ($this->image, rand (0), rand (0, MB), rand (0, 100));
$font = $this->afontlist[rand (0,9)];//rand (1,4). ". TTF ";
$randsize = rand ($size-$size/10, $size + $size/10);
$location = $left + ($i * $size + $size/10);
Imagettftext ($this->image, $randsize, Rand ( -18,18), $location, Rand ($size, $size + $size/5) + $this->ntopmargin, $ TextColor, $font, $randtext);
}
if ($this->bnoise = = True) $this->setnoise ();
$_session[' authcode ' = $code;
$bordercolor = $this->getcolor ("");
if ($border ==true) imagerectangle ($this->image, 0, 0, $this->nwidth-1, $this->nheight-1, $bordercolor);
Header ("Expires:mon, June June 1997 05:00:00 GMT"); Date in the past
Header ("last-modified:"). Gmdate ("D, D M Y h:i:s"). "GMT"); Always modified
Header ("Cache-control:no-store, No-cache, must-revalidate"); http/1.1
Header ("Cache-control:post-check=0, pre-check=0", false);
Header ("Cache-control:private");
Header ("Pragma:no-cache"); http/1.0
Header ("Content-type:image/png");
Imagepng ($this->image);
Imagedestroy ($this->image);

return $sAuthcode;
}
function Chkauthcode ($Authcode)
{
if ($this->getauthcode () = = $Authcode)
{
$rtn = true;
}
Else
{
$rtn = false;
}
$_session[' authcode '] = rand (0, 999999);
return $RTN;
}
function Getauthcode ()
{
$x _authcode = $_session[' Authcode '];
$_session[' authcode '] = rand (0, 999999);
return $x _authcode;
}

* Private Functions * *
function Geturifilename ()
{
return substr ($_server[' script_name '), Strrpos ($_server[' script_name '], '/') + 1, strlen ($_server[' script_name '])- Strrpos ($_server[' script_name '], '/');
}
function Setnoise ()
{
For ($i =0 $i < $this->nnoiseline; $i + +) {
$randColor = Imagecolorallocate ($this->image, rand (0, 255), rand (0, 255), rand (0, 255));
Imageline ($this->image, rand (0, $this->nwidth), rand (0, $this->nheight), rand (0, $this->nwidth), rand (0, $ this->nheight), $randColor);
}

For ($i =0 $i < $this->nnoisepoint; $i + +) {
$randColor = Imagecolorallocate ($this->image, rand (0, 255), rand (0, 255), rand (0, 255));
Imagesetpixel ($this->image, rand (0, $this->nwidth), rand (0, $this->nheight), $randColor);
}
}
function GetColor ($color)
{
$color = Eregi_replace ("^#", "", $color);
$r = $color [0]. $color [1];
$r = Hexdec ($r);
$b = $color [2]. $color [3];
$b = Hexdec ($b);
$g = $color [4]. $color [5];
$g = Hexdec ($g);
$color = Imagecolorallocate ($this->image, $r, $b, $g);
return $color;
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.