A nice PHP verification code class

Source: Internet
Author: User

A beautiful PHP Verification code class (share) font: [Increase decrease] Type: Reproduced below the small series for everyone to share a beautiful PHP code class. Need friends can come over to refer directly to the code:
Copy CodeThe code is as follows:
Verification Code Class
Class Validatecode {
Private $charset = ' abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789 ';//random factor
private $code;//Verification Code
Private $codelen = 4;//Verification Code length
Private $width = 130;//width
Private $height = 50;//height
private $img;//graphic resource handle
private $font;//The specified font
Private $fontsize = 20;//Specify font size
private $fontcolor;//Specify Font Color
Constructor method Initialization
Public Function __construct () {
$this->font = dirname (__file__). ' /font/elephant.ttf ';//Note that the font path should be written, otherwise the picture will not be displayed
}
Generate Random Code
Private Function Createcode () {
$_len = strlen ($this->charset)-1;
for ($i =0; $i < $this->codelen; $i +) {
$this->code. = $this->charset[mt_rand (0,$_len)];
}
}
Generate background
Private Function Createbg () {
$this->img = Imagecreatetruecolor ($this->width, $this->height);
$color = Imagecolorallocate ($this->img, Mt_rand (157,255), Mt_rand (157,255), Mt_rand (157,255));
Imagefilledrectangle ($this->img,0, $this->height, $this->width,0, $color);
}
Generate text
Private Function CreateFont () {
$_x = $this->width/$this->codelen;
for ($i =0; $i < $this->codelen; $i +) {
$this->fontcolor = imagecolorallocate ($this->img,mt_rand (0,156), Mt_rand (0,156), Mt_rand (0,156));
Imagettftext ($this->img, $this->fontsize,mt_rand ( -30,30), $_x* $i +mt_rand (1,5), $this->height/1.4, $this- >fontcolor, $this->font, $this->code[$i]);
}
}
Generate lines, snowflakes
Private Function Createline () {
Lines
for ($i =0; $i <6; $i + +) {
$color = Imagecolorallocate ($this->img,mt_rand (0,156), Mt_rand (0,156), Mt_rand (0,156));
Imageline ($this->img,mt_rand (0, $this->width), Mt_rand (0, $this->height), Mt_rand (0, $this->width), Mt_ Rand (0, $this->height), $color);
}
Snowflakes
for ($i =0; $i <100; $i + +) {
$color = Imagecolorallocate ($this->img,mt_rand (200,255), Mt_rand (200,255), Mt_rand (200,255));
Imagestring ($this->img,mt_rand (1,5), Mt_rand (0, $this->width), Mt_rand (0, $this->height), ' * ', $color);
}
}
Output
Private Function OutPut () {
Header (' content-type:image/png ');
Imagepng ($this->img);
Imagedestroy ($this->img);
}
External generation
Public Function doimg () {
$this-&GT;CREATEBG ();
$this->createcode ();
$this->createline ();
$this->createfont ();
$this->output ();
}
Get Verification Code
Public Function GetCode () {
Return Strtolower ($this->code);
}
}
Output instance:

How to use:
1, first save the verification code class as a file named ValidateCode.class.php;
2. Create a new file named captcha.php to call the class;
captcha.php

Copy CodeThe code is as follows:
Session_Start ();
Require './validatecode.class.php '; First, the class is included and the actual path is modified according to the actual situation.
$_VC = new Validatecode (); Instantiate an Object
$_vc->doimg ();
$_session[' authnum_session ' = $_vc->getcode ();//verification code saved to SESSION
3, refer to the page, the code is as follows:
Copy CodeThe code is as follows:
</img>
4, a complete verification page, the code is as follows:
Copy CodeThe code is as follows:
<?php
Session_Start ();
At the beginning of the page to open the session,
Error_reporting (2047);
Session_destroy ();
Remove the session to get a new session value each time;
With seesion effect is good, also very convenient
?>
<title>session example of photo verification </title>
<style type= "Text/css" >
#login p{
margin-top:15px;
line-height:20px;
font-size:14px;
Font-weight:bold;
}
#login img{
Cursor:pointer;
}
form{
margin-left:20px;
}
</style>
<body>

<form id= "Login" action= "method=" POST ">
<p> This example is the session validation instance </p>
<p>
<span> Verification Code:</span>
<input type= "text" name= "Validate" value= "size=10>
</img>
</p>
<p>
<input type= "Submit" >
</p>
</form>
<?php
Print the previous session;
echo "Last Session:<b>". $_session["Authnum_session"]. " </b><br> ";
$validate = "";
if (Isset ($_post["Validate")) {
$validate =$_post["Validate"];
echo "You have just entered:". $_post["Validate"]. " <br> Status: ";
if ($validate!=$_session["Authnum_session"]) {
Determine if the session value is consistent with the user input verification code;
echo "<font color=red> input error </font>";
}else{
echo "<font color=green> through verification </font>";
}
}
?>

A nice PHP verification code class

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.