


The idea and implementation of php generating SessionID and image verification code
A verification code is required for backend login, and SessionID is required for user tracking in the frontend. Of course, the default PHP will have a SessionID after opening the Session, but I need my own and can be stored in the database, then I I gave it a try and constructed the following function.
/****** Generate Session ID ******/
The basic idea is to obtain the current microsecond time, then generate a random number, add the random number to the current time and encrypt it, and finally intercept the required length
/*
Function name: create_sess_id()
Function function: Generate a random session ID
Parameter: $len: The length of the session string is required, the default is 32 bits, not less than 16 bits
Return value: Return the session ID
Function heiyeluren
*/
function create_sess_id($len=32)
{
// Verify whether the submitted length is legal
if( !is_numeric($len) || ($len>32) || ($len< 16)) { return; }
// Get the microseconds of the current time
list($u, $s) = explode(' ', microtime());
$time = (float)$u (float)$s ;
// Generate a random number
$rand_num = rand(100000, 999999);
$rand_num = rand($rand_num, $time);
mt_srand($rand_num);
$rand_num = mt_rand();
// Generate SessionID
$sess_id = md5( md5($time). md5($rand_num) );
//Intercept the SessionID of the specified required length
$sess_id = substr($sess_id, 0, $len);
return $sess_id;
}
/****** Generate verification code ******/
Idea: This idea is relatively simple, because considering the uniqueness and randomness, our verification code only needs to intercept a string from the Session ID, because our SessionID is Uniquely thought out.
/*
Function name: create_check_code()
Function function: Generate a random check code
Parameter: $len: The length of the check code required, please not be longer than 16 digits, the default is 4 digits
Return value : Returns the check code of the specified length
function heiyeluren
*/
function create_check_code($len=4)
{
if ( !is_numeric($len) || ($len>6) || ($len<1) ) { return; }
$check_code = substr(create_sess_id(), 16, $len );
return strtoupper($check_code);
}
/****** Picture of generating verification code ******/
These are some simple ones PHP image programming stuff, I'm making images and it's simple.
/*
Function name: create_check_image()
Function function: Generate a check code image
Parameter: $check_code: Check code string, usually obtained by the create_check_code() function
Return value: Return the image
Function heiyeluren
*/
function create_check_image( $check_code )
{
// Generate an image
$im = imagecreate(65,22);
$black = ImageColorAllocate($im, 0,0,0); // Background Color
$white = ImageColorAllocate($im, 255,255,255); // Foreground color
$gray = ImageColorAllocate($im, 200,200,200);
imagefill($im,68,30,$gray);
// Change four digits The integer verification code is drawn into the image
imagestring($im, 5, 8, 3, $check_code, $white);
// Add interference pixels
for($i=0;$i<200;$i )
{
$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()p , rand()0 , $randcolor);
}
// Output image
Header("Content-type: image/PNG");
ImagePNG($im);
ImageDestroy($im);
}
Here we should note that when referencing the create_check_image() function, it must be in a In a separate file, because the output format when outputting the file header is an image format, mixed with other content, the image will not be displayed. In addition, you can change the image generation function. For example, if you want to change the color, then you change the generation positions of the foreground color and background color, then the colors will be different. At the same time, you also need to change the color of the check code. Change it, otherwise the background and check code will be black and will not be displayed. Please indicate the source when reprinting: The idea and implementation of php generating SessionID and image verification code

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
