SoFunction
Updated on 2025-03-09

PHP verification code implementation code

Generate a verification code picture, and also the variable $_SESSION[check_pic].
Copy the codeThe 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 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);
?>


The verification code image generated by the <img src=""> call
Copy the codeThe code is as follows:

<div class="bottomAds">
<fieldset class="bottomAds_quote"><legend>Leave a message</legend>
<div class="ads">
<form action="../utity/" 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=""><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); uses the int imagestring(int im, int font, int x, int y, string s, int col); function, which is used to draw horizontal strings.
This function draws horizontal horizontal strings on the picture. Parameter font is a glyph, and setting it to 1 to 5 means using the default glyph. Parameters x and y are the coordinates of the starting point of the string. The content of the string is placed on the parameter s. Parameter col represents the color of the string.

Compare $_POST[check] with $_SESSION[check_pic], and perform database insertion operation if it is equal. If not equal, go back to the previous page.
Copy the codeThe 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("");
$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 up 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('Submit Successfully'); (-1);</script>";
}
if (!$result)
{
die('Invalid query: ' . mysql_error());
}
}
else
{
echo "<script>alert('verification code error'); (-1);</script>";
}
}
?>