Why is this session value wrong?

Source: Internet
Author: User
Keywords Php
Tags setcookie
The code of a verification code from the Internet I want to print the session value in the Yzm1.php page to see why
Is the value of the session different from the picture? Each time the yzm1.php page is refreshed, the string that is printed is the same as the string on the picture before the page refreshes, i.e. the string that appears on the picture, such as the value of Echo $_session["AAA" after refreshing once. Why are they not the same? So how do I do the verification?

yzm1.php


  
           

yzm2.php

!--? phpif (!isset ($_session)) {session_start ();} function Random ($len) {$srcstr = "1a2s3d4f5g6hj8k9qwertyupzxcvbnm"; Mt_srand (); $strs = ""; for ($i = 0; $i $len; $i + +) {$strs. = $srcstr [Mt_rand (0, 30)]; } return $strs;} Randomly generated string $str = random (4); The width of the captcha picture $width = 50; The height of the captcha picture $height = 25; Declare the picture format of the layer you want to create @ header ("Content-type:image/png"); Create a layer $im = Imagecreate ($width, $height); Background color $back = Imagecolorallocate ($im, 0xFF, 0xFF, 0xFF); Blur point Color $pix = Imagecolorallocate ($im, 187, 230, 247); Font Color $font = imagecolorallocate ($im, 41, 163, 238); The point Mt_srand () of the Blur function; for ($i = 0; $i $i + +) {Imagesetpixel ($im, Mt_rand (0, $width), Mt_rand (0, $height), $pix);} Output character imagestring ($im, 5, 7, 5, $STR, $font); Output Rectangle Imagerectangle ($im, 0, 0, $width-1, $height-1, $font); Output Image imagepng ($im); Imagedestroy ($im); Select Cookie//setcookie ("Verification", $str, Time () + 7200, "/"); Select session$_session["AAA"] = $STR;? -->

Reply content:

The code of a verification code from the Internet I want to print the session value in the Yzm1.php page to see why
Is the value of the session different from the picture? Each time the yzm1.php page is refreshed, the string that is printed is the same as the string on the picture before the page refreshes, i.e. the string that appears on the picture, such as the value of Echo $_session["AAA" after refreshing once. Why are they not the same? So how do I do the verification?

yzm1.php


  
           

yzm2.php

!--? phpif (!isset ($_session)) {session_start ();} function Random ($len) {$srcstr = "1a2s3d4f5g6hj8k9qwertyupzxcvbnm"; Mt_srand (); $strs = ""; for ($i = 0; $i $len; $i + +) {$strs. = $srcstr [Mt_rand (0, 30)]; } return $strs;} Randomly generated string $str = random (4); The width of the captcha picture $width = 50; The height of the captcha picture $height = 25; Declare the picture format of the layer you want to create @ header ("Content-type:image/png"); Create a layer $im = Imagecreate ($width, $height); Background color $back = Imagecolorallocate ($im, 0xFF, 0xFF, 0xFF); Blur point Color $pix = Imagecolorallocate ($im, 187, 230, 247); Font Color $font = imagecolorallocate ($im, 41, 163, 238); The point Mt_srand () of the Blur function; for ($i = 0; $i $i + +) {Imagesetpixel ($im, Mt_rand (0, $width), Mt_rand (0, $height), $pix);} Output character imagestring ($im, 5, 7, 5, $STR, $font); Output Rectangle Imagerectangle ($im, 0, 0, $width-1, $height-1, $font); Output Image imagepng ($im); Imagedestroy ($im); Select Cookie//setcookie ("Verification", $str, Time () + 7200, "/"); Select session$_session["AAA"] = $STR;? -->

Don't say logic, just say code
First of all

function random($len) {    $srcstr = "1a2s3d4f5g6hj8k9qwertyupzxcvbnm";    mt_srand();    $strs = "";    for ($i = 0; $i < $len; $i++) {        $strs .= $srcstr[mt_rand(0, 30)];    }    

The value of $STR is that way, so it doesn't have to be the same as long as you use YZM2 to get the verification code.

$str = md5($str); //选择 cookie//SetCookie("verification", $str, time() + 7200, "/"); //选择 Session$_SESSION["str"] = $str;

This is the time to write the value in the session, write $STR before the $str MD5 once, so you see the value is not generated $STR

  • 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.