This course through the use of PHP and Web front-end technology to achieve a website registration login portal page, learning and practice PHP programming, interested students can refer to.
This article introduces the implementation of user registration login function based on PHP, this project is divided into four parts: 1 front page production, 2 verification code production, 3 registration login, 4 perfect function. The specific situation can be looked down.
Verification Code Production
First, the experimental introduction
This experiment will lead you to encapsulate a validation code class using object-oriented thinking. And in the registration and login interface display use. Through the study of this experiment, you will understand the OOP idea of PHP, as well as the use of the GD library, verification code generation.
1.1 Points of knowledge involved
Php
GD Library
OOP programming
1.2 Development tools
Sublime, a quick and easy text editor. Click on the lower left corner of the desktop: Application menu/Development/sublime
Second, package Verification code class
2.1 Creating a directory and preparing fonts
In the Web directory to create an admin directory as our background directory, store the background code files. Create a fonts directory under Admin to store the font you need to make the verification code.
Create a new captcha.php file under Admin, this is the verification code class file that we need to edit.
Current directory hierarchy:
Edit captcha.php File:
<?php/*** Captcha class*/class captcha{ function __construct () { # code ... }}
Add private properties and construction methods for this class:
<?php/*** Captcha class*/class captcha{ private $codeNum; Verify the number of code bits private $width; Verification code picture width private $height; Verification code Picture Height private $img; Image resource handle private $lineFlag; Whether to generate interference lines private $piexFlag; Whether to generate the interference point private $fontSize; Font size private $code; The verification code character is private $string; The character set for generating the CAPTCHA is private $font; Font function __construct ($codeNum = 4, $height = $width = $fontSize, $lineFlag = True, $piexFlag = True)
{ $this->string = ' qwertyupmkjnhbgvfcdsxa123456789 '; Remove some similar characters $this->codenum = $codeNum; $this->height = $height; $this->width = $width; $this->lineflag = $lineFlag; $this->piexflag = $piexFlag; $this->font = dirname (__file__). ' /fonts/consola.ttf '; $this->fontsize = $fontSize; }}
Font files can be downloaded to the fonts directory using the following command:
$ wget Http://labfile.oss.aliyuncs.com/courses/587/consola.ttf
The next step is to write a specific method:
Create an image resource handle
Create Image resource public function CreateImage () { $this->img = imagecreate ($this->width, $this->height); Create Image Resource imagecolorallocate ($this->img,mt_rand (0,100), Mt_rand (0,100), Mt_rand (0,100)); Fill image background (using light) }
Related functions used
Imagecreate: Create a new palette-based image
Imagecolorallocate: Assigning colors to an image
Mt_rand: Generate a better random number
Create Captcha string and output to image
Create the Captcha public function Createcode () { $strlen = strlen ($this->string)-1; for ($i =0; $i < $this->codenum; $i + +) { $this->code. = $this->string[mt_rand (0, $strlen)]; Randomly remove four characters from a character set } $_session[' code ' = $this->code; Join session //calculate each character spacing $diff = $this->width/$this->codenum; for ($i =0; $i < $this->codenum; $i + +) { //generate colors for each character (using dark color) $txtColor = imagecolorallocate ($this->img , Mt_rand (100,255), Mt_rand (100,255), Mt_rand (100,255)); Write Image Imagettftext ($this->img, $this->fontsize, Mt_rand ( -30,30), $diff * $i +mt_rand (3,8), Mt_rand (20,$ THIS->HEIGHT-10), $txtColor, $this->font, $this->code[$i]); } }
Related functions used
Imagecreate: Create a new palette-based image
Imagecolorallocate: Assigning colors to an image
Mt_rand: Generate a better random number
Create Captcha string and output to image
Create the Captcha public function Createcode () { $strlen = strlen ($this->string)-1; for ($i =0; $i < $this->codenum; $i + +) { $this->code. = $this->string[mt_rand (0, $strlen)]; Randomly remove four characters from a character set } $_session[' code ' = $this->code; Join session //calculate each character spacing $diff = $this->width/$this->codenum; for ($i =0; $i < $this->codenum; $i + +) { //generate colors for each character (using dark color) $txtColor = imagecolorallocate ($this->img , Mt_rand (100,255), Mt_rand (100,255), Mt_rand (100,255)); Write Image Imagettftext ($this->img, $this->fontsize, Mt_rand ( -30,30), $diff * $i +mt_rand (3,8), Mt_rand (20,$ THIS->HEIGHT-10), $txtColor, $this->font, $this->code[$i]); } }
Related functions used:
Create interference lines
Create a disturbance line (default four) public function Createlines () {for ($i =0; $i < 4; $i + +) { $color = imagecolorallocate ($this- >img,mt_rand (0,155), Mt_rand (0,155), Mt_rand (0,155)); Use light imageline ($this->img,mt_rand (0, $this->width), Mt_rand (0, $this->height), Mt_rand (0, $this, width), mt_rand (0, $this->height), $color); } }
Related functions used:
Create a disturbance point
Create a disturbance point (default 100 points) Public Function Createpiex () {for ($i =0; $i < $i + +) { $color = imagecolorallocate ($this-& Gt;img,mt_rand (0,255), Mt_rand (0,255), Mt_rand (0,255)); Imagesetpixel ($this->img,mt_rand (0, $this->width), Mt_rand (0, $this->height), $color); } }
Related functions used:
External output Image:
Public function Show () { $this->createimage (); $this->createcode (); if ($this->lineflag) { //whether to create a disturbance line $this->createlines (); } if ($this->piexflag) { //whether to create a disturbance point $this->createpiex (); } Header (' content-type:image/png '); The content of the requested page is an image Imagepng ($this->img) in PNG format; Output Image Imagedestroy ($this->img) in PNG format; Clear image resource, free memory }
Related functions used:
Provide verification code for external:
Public Function GetCode () {return $this->code; The complete code is as follows: <?php/*** Captcha class*/class captcha{private $codeNum; Private $width; Private $height; Private $img; Private $lineFlag; Private $piexFlag; Private $fontSize; Private $code; Private $string; Private $font; function __construct ($codeNum = 4, $height =, $width = $fontSize, $lineFlag = True, $piexFlag = True) {$this- >string = ' qwertyupmkjnhbgvfcdsxa123456789 '; $this->codenum = $codeNum; $this->height = $height; $this->width = $width; $this->lineflag = $lineFlag; $this->piexflag = $piexFlag; $this->font = dirname (__file__). ' /fonts/consola.ttf '; $this->fontsize = $fontSize; Public Function CreateImage () {$this->img = imagecreate ($this->width, $this->height); Imagecolorallocate ($this->img,mt_rand (0,100), Mt_rand (0,100), Mt_rand (0,100)); Public Function Createcode () {$strlen = strlen ($this->string)-1; for ($i =0; $i < $this->codenum; $i + +) {$this->code. = $this->string[mt_rand (0, $strlen)]; } $_session[' Code ' = $this->code; $diff = $this->width/$this->codenum; for ($i =0; $i < $this->codenum; $i + +) {$txtColor = Imagecolorallocate ($this->img,mt_rand (100,255), Mt_rand ( 100,255), Mt_rand (100,255)); Imagettftext ($this->img, $this->fontsize, Mt_rand ( -30,30), $diff * $i +mt_rand (3,8), Mt_rand ($this, HEIGHT-10), $txtColor, $this->font, $this->code[$i]); }} Public Function Createlines () {for ($i =0; $i < 4; $i + +) {$color = Imagecolorallocate ($this->img,mt_r and (0,155), Mt_rand (0,155), Mt_rand (0,155)); Imageline ($this->img,mt_rand (0, $this->width), Mt_rand (0, $this->height), Mt_rand (0, $this->width), Mt_ Rand (0, $this->height), $color); }} Public Function Createpiexs () {for ($i =0; $i < $i + +) {$color = Imagecolorallocate ($this->img,mt _rand (0,255), Mt_rand (0,255), Mt_rand (0,255)); ImagesEtpixel ($this->img,mt_rand (0, $this->width), Mt_rand (0, $this->height), $color); }} public Function Show () {$this->createimage (); $this->createcode (); if ($this->lineflag) {$this->createlines (); } if ($this->piexflag) {$this->createpiexs (); } header (' Content-type:image/png '); Imagepng ($this->img); Imagedestroy ($this->img); } public Function GetCode () {return $this->code; }}
The above is the entire code of the Verification Code class. It seems quite simple, but with more image processing functions, the above related functions I have also made the necessary links and usage instructions. These functions also do not have to memorize, encountered unclear, always consult the official PHP documentation, most importantly, there are Chinese documents.
2.2 Using a verification code
Now that you have packaged it, you can start using it. For convenience, this class is called directly below the Captcha class:
Session_Start (); Open Session$captcha = new Captcha (); Instantiate the validation code class (customizable parameters) $captcha->show (); Call output
Third, the front-end display
The backend is ready with the verification code, the front-end interface can be displayed, modify the registration and login form in the index.php part of the Verification code:
<p class= "Form-group" > <p class= "col-sm-12" > <span>click to switch</span> </p></p>
The IMG tag adds the JS code of the Click event, so that you can implement the click-to-replace verification code function!
:
Iv. Perfect
So far, our verification code module has basically been completed. Learning here, you should have a further understanding of object-oriented programming. I also realized the idea of OOP. Three major features of OOP: encapsulation, inheritance, polymorphism. We only use a little of the idea of encapsulation here. We can continue to improve and improve this code class, design a more perfect class. This experiment also tells us that PHP has a lot of functions, do not memorize, read more official documents.
Summary: The above is the entire content of this article, I hope to be able to help you learn.