_php Example of user registration login function based on PHP

Source: Internet
Author: User
Tags character set diff strlen

This article describes the implementation of the user login based on PHP function, the project is divided into four parts: 1 front-end page production, 2 authentication code production, 3 to achieve registration landing, 4 function perfect. The specific situation can be looked down.

Verification Code making

First, the experiment introduction

This experiment will lead to the use of object-oriented ideas to encapsulate a validation code class. And in the registration and landing interface to display the use. Through the study of this experiment, you will understand the OOP ideas of PHP, as well as the use of GD Library, verification code generation.

1.1 Points of knowledge involved

    • Php
    • GD Library
    • OOP programming

1.2 Development tools

Sublime, a convenient and fast text editor. Click on the bottom left corner of the desktop: Application menu/Development/sublime

Second, the package Verification code class

2.1 Creating directories 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 fonts needed to make the verification code.

Create a new captcha.php file under Admin, which is the authentication code class file that we need to edit.

Current directory hierarchy:

Edit captcha.php File:

<?php 
/**
* Captcha class
/Class Captcha
{
  
  function __construct ()
  {
    # code ...
  }
}

To add a private property and a construction method for the class:

<?php 
/**
* Captcha class
/Class Captcha
{
  private $codeNum;  Verify the number of code digits
  private $width;  Verify code picture width
  private $height;  Verification code picture highly
  private $img;  Image resource handle
  private $lineFlag;  Whether to generate interference lines
  private $piexFlag;  Whether to generate interference points
  private $fontSize;  Font size
  private $code;  Verification code character
  private $string;  Generate the character set of the CAPTCHA code
  private $font;  Font
  function __construct ($codeNum = 4, $height = $width =, $fontSize =, $lineFlag = True, $piexFlag = True) 
   {
    $this->string = ' qwertyupmkjnhbgvfcdsxa123456789 ';  Remove some of the 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 by the following command:

$ wget Http://labfile.oss.aliyuncs.com/courses/587/consola.ttf

The next step is to start writing specific methods:

Create an image resource handle

Create Image resource public  
function CreateImage () {
    $this->img = imagecreate ($this->width, $this->height);  Create an Image resource
    imagecolorallocate ($this->img,mt_rand (0,100), Mt_rand (0,100), Mt_rand (0,100));  Fill the image background (using light-colored)
  }

Related functions to use

    • Imagecreate: Create a new palette-based image
    • Imagecolorallocate: Assigning color to an image
    • Mt_rand: Generate better random numbers

Creating a CAPTCHA string and outputting it to an image

Create Authenticode 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 character concatenation from 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 color for each character (using dark)
     $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 to use

    • Imagecreate: Create a new palette-based image
    • Imagecolorallocate: Assigning color to an image
    • Mt_rand: Generate better random numbers

Creating a CAPTCHA string and outputting it to an image

Create Authenticode 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 character concatenation from 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 color for each character (using dark)
     $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 to use:

    • Imagettftext: Writing text to an image with a TrueType font

Create a noise line

Create a disturbing 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-colored
      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 to use:

    • Imageline: Draw a line segment

Create a disturbance point

Create a disturbance point (default 100 point) public
function Createpiex () {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);
    }
  

Related functions to use:

    • Imagesetpixel: Draw a single pixel

External output Image:

 Public function Show ()
  {
    $this->createimage ();
    $this->createcode ();
    if ($this->lineflag) {  //whether to create interference lines
      $this->createlines ();
    }
    if ($this->piexflag) {  //whether the interference point is created
      $this->createpiex ();
    }
    Header (' content-type:image/png ');  The content of the request page is the PNG format image
    Imagepng ($this->img);  Output Image
    Imagedestroy ($this->img) in PNG format;  Clear image resource, free memory
  }

Related functions to use:

    • Imagepng: Output the image to a browser or file in PNG format
    • Imagedestroy: Destroying an image

External Verification Code:

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) {$thi
    s->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;
    The 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)); The Public Function Createcode () {$strlen = strlen ($this-&Gt;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]); The 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)); Imageline ($this->img,mt_rand (0, $this->width), Mt_rand (0, $this->height), Mt_rand (0, $this->width), Mt_ 
    Rand (0, $this->height), $color); The Public Function Createpiexs () {for ($i =0; $i < $i + +) {$color = Imagecolorallocate ($this->i MG,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);
    The 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);
  The Public Function GetCode () {return $this->code;

 }
}

That's all the code for the validation code class. It seems to be quite simple, but with a lot of image processing functions, the related functions I also do the necessary links and use instructions. These functions do not have to memorize, encountered unclear, at any time to check the official PHP documents, the most important is the Chinese document.

2.2 Using the Verification code

Now that the package is finished, you can start using it. Here for convenience, call the class directly below the Captcha class:

Session_Start (); Open session
$captcha = new Captcha ();  Instantiate the validation code class (customizable parameters)
$captcha->show ();  Call output

Third, front end display

The back end is ready to verify the code, the front-end interface can be shown, modify the registration and login form in the index.php part of the Verification code:

<div class= "Form-group" >
 <div class= "col-sm-12" > 
   <span>click to switch</span>
 </div>
</div>

The IMG tag adds the JS code for the Click event, which enables you to click on the Change verification code!

Effect Chart:


Four, improve

So far, our verification code module is basically complete. Learning here, you should have a further understanding of object-oriented programming. Also realized a trace of OOP thought. Three major features of OOP: encapsulation, inheritance, polymorphism. We've only got a little bit of encapsulation in mind here. We can continue to refine and improve this verification code class, design a more perfect class. This experiment also tells us that PHP has a lot of functions, do not rote, read more official documents.

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.