为什么我的验证不起作用?

时间:2016-01-20 20:26:04

标签: php session captcha

我认为一切都是正确的但是当它推动我的提交。阅读代码中的评论部分:

<?php
session_start();
function create_image(){//this function make a image to validate form

$image=imagecreatetruecolor(200,50);

$background_color=imagecolorallocate($image,200,200,200);
imagefilledrectangle($image,0,0,200,50,$background_color);


$line_color=imagecolorallocate($image, 0,0,0);
for($i=0;$i<5;$i++){
    imageline($image, 0, rand()%50,200,rand()%50,$line_color);  
}
$pixel_color=imagecolorallocate($image,0,0,0);
for($i=0;$i<500;$i++){
    imagesetpixel($image, rand(0,200),rand(0,50),$pixel_color); 

    }
$text_color=imagecolorallocate($image,0,0,0);
$text_color2=imagecolorallocate($image,128,128,128);
$letters="ABDEFGQRMNZP23456789";
$len=strlen($letters);
$word='';
for($i=0;$i<6;$i++){    

    $letter=$letters[rand(0,$len-1)];
    //imagestring($image,6,5+($i*20),15,$letter,$text_color);
    imagettftext($image,30,3.5+($i*3),11.5+($i*30),35,$text_color2,'font/ad.ttf',$letter);
    imagettftext($image,30,2+($i*3),10+($i*30),35,$text_color,'font/ad.ttf',$letter);
    $word.=$letter;
}

imagepng($image,"images/image.png");
$_SESSION['captcha_string']=$word;

}
?>

在其他页面中,我使用该函数来验证我的表单机器人它不起作用:

<?php
include('inc/function.php');
create_image();


if($_SERVER['REQUEST_METHOD']=='POST'){

require_once('inc/mysqli_connect.php'); 
$er=array();
if($_POST['a']){// 'a' is the input

    if($_SESSION['captcha_string']==$_POST['a']){// the problem is here that dont work
    }else{
        $er[]='error';} 
}else{
    $er[]='error';}
if($_POST['Name']){
    $name=trim($_POST['Name']);}
else{
    $er[]="error.";}
if($_POST['Email']||$_POST['Phone']){
    $mail=trim($_POST['Email']);
    $phone=trim($_POST['Phone']);}
else{
    $er[]="error";}
if($_POST['Text']){
    $txt=$_POST['Text'];}
else{
    $er[]="error";} 
if(empty($er)){ 
    $q="INSERT INTO contact values(Null, '$name', '$mail' , '$phone' , '$txt');";
    $result=mysqli_query($db,$q);
    $err='error';           
    }else{
        foreach($er as $msg){
            echo "$msg <br>\n";}
    }
}

&GT;

为什么我的比较短语不起作用?

0 个答案:

没有答案