Home Backend Development PHP Tutorial Implementation code of PHP verification code_PHP tutorial

Implementation code of PHP verification code_PHP tutorial

Jul 21, 2016 pm 03:26 PM
php session code variable picture accomplish generate of code verify

checkcode.php generates a verification code image, as well as the variable $_SESSION[check_pic].

Copy code The code is as follows:

<?
session_start();
for($i= 0; $i<4; $i++){
$rand.= dechex(rand(1,15));
}
$_SESSION[check_pic]=$rand;
//echo $_SESSION[check_pic];
//Set the image size
$im = imagecreatetruecolor(100,30);
//Set the color
$bg=imagecolorallocate($im,0,0,0 );
$te=imagecolorallocate($im,255,255,255);
//Write the string in the upper left corner of the image
imagestring($im,rand(5,6),rand(25,30) ,5,$rand,$te);
// Output image
header("Content-type:image/jpeg");
imagejpeg($im);
?>

form.php
Call the generated verification code image through <img src="checkcode.php">
Copy code The code is as follows:

<div class="bottomAds">
<fieldset class="bottomAds_quote"><legend>Message</legend>
<div class="ads">
<form action="../utity/post.php" method="post" onsubmit="return chkinput(this)">
<input name="name" type="text" /> your name
<input name="email" type="text" /> your email
<input name="website" type ="text" /> Your website
<textarea name="content" style="width:340; height:150;">
</textarea><br />
<img src="checkcode.php"><input type="text" name="check"><br />
<input type="submit" value="Submit " />
</form>
</div>
<br clear="both" />
</fieldset>

imagestring($im,rand(5,6),rand(25,30),5,$rand,$te); used int imagestring(int im, int font, int x, int y, string s, int col); function, this function is used to draw horizontal strings.
This function draws a horizontal string on the image. The parameter font is the font, and setting it from 1 to 5 means using the default font. The parameters x and y are the starting point coordinates of the string. The contents of the string are placed on parameter s. Parameter col represents the color of the string.
post.php
Compare $_POST[check] and $_SESSION[check_pic]. If they are equal, perform database insertion. If not equal, return to the previous page.
Copy code The code is as follows:

<?php
session_start();
if(isset( $_POST[check]))
{
if($_POST[check] == $_SESSION[check_pic])
{
// echo "Verification code is correct".$_SESSION[check_pic] ;
require("dbinfo.php");
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST[ 'website'];
$content = $_POST['content'];
$date = date("Y-m-d h:m:s");
// Connect to MySQL server
$ connection = mysql_connect ($host, $username, $password);
if (!$connection)
{
die('Not connected : ' . mysql_error());
}
//Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected)
{
die ('Can't use db : ' . mysql_error ());
}
//Insert data into the database
$query = "insert into table (nowamagic_name, nowamagic_email, nowamagic_website, nowamagic_content, nowamagic_date) values ​​('$name','$email', '$website','$content','$date')";
$result = mysql_query($query);
if($result)
{
echo "<script> alert('Submission successful'); history.go(-1);</script>";
}
if (!$result)
{
die('Invalid query: ' . mysql_error());
}
}
else
{
echo "<script>alert('Verification code error'); history.go(-1);< /script>";
}
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323975.htmlTechArticlecheckcode.php generates a verification code image and the variable $_SESSION[check_pic]. Copy the code as follows: ? session_start(); for($i=0; $i4; $i++){ $rand.= dechex(rand(1,15)); } $_SESSIO...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles