【问题标题】:Captcha php ... I dont know what is errorCaptcha php ...我不知道什么是错误
【发布时间】:2016-02-08 12:42:47
【问题描述】:

我是 php 新手,我想做 capthca ...我编写了代码、验证等,结果我得到的只是一张小图片...上面什么都没有...而且我的程序没有告诉我哪里出错了

这是我在 captcha.php 中的代码:

<?php
session_start();`enter code here`


  // build the base captcha image 

$img = imagecreatetruecolor(80,30);

$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
$grey = imagecolorallocate($img,150,150,150);
$red = imagecolorallocate($img, 255, 0, 0);
$pink = imagecolorallocate($img, 200, 0, 150);

// build the base captcha image - END

// building randomString 
function randomString($length){
    $chars = "abcdefghijkmnopqrstuvwxyz023456789";
    srand((double)microtime()*1000000);
    $str = "";
    $i = 0;

        while($i <= $length){
            $num = rand() % 33;
            $tmp = substr($chars, $num, 1);
            $str = $str . $tmp;
            $i++;
        }
    return $str;
}

for($i=1;$i<=rand(1,5);$i++){
    $color = (rand(1,2) == 1) ? $pink : $red;
    imageline($img,rand(5,70),rand(5,20), rand(5,70)+5,rand(5,20)+5, $color);
}
// building randomString - END

// fill  image with a white rectangle
imagefill($img, 0, 0, $white);

$string = randomString(rand(7,10));
$_SESSION['string'] = $string;

imagettftext($img, 11, 0, 10, 20, $black, "calibri.ttf", $string);
// fill  image with a white rectangle - END

//  type of image
header("Content-type: image/png");
imagepng($img);


?>

这是我在 contact.php 中的代码:

<?php

session_start();

if(isset($_POST['submit'])) {

    if(!empty($_POST['ime']) && !empty($_POST['email']) && !empty($_POST['poruka']) && !empty($_POST['code'])) {

        if($_POST['code'] == $_SESSION['rand_code']) {

            // send email
            $accept = "Poruka uspesno poslata.";

        } else {

            $error = "Pogresan kod.";
              }

    } else {

        $error = "Molimo Vas popunite sva polja.";    
    }
 }

?> 

<!DOCTYPE html >
<html >
<head>
<title>Kontaktirajte nas</title>

</head>

<body>

<?php if(!empty($error)) echo '<div class="error">'.$error.'</div>'; ?>
<?php if(!empty($accept)) echo '<div class="accept">'.$accept.'</div>'; ?>

<form  method="post">
    <p>Ime<input type="text" name="ime" /></p>
    <p>Email<input type="text" name="email" /></p>
    <p>Poruka<textarea name="poruka" rows=”25” cols=”40”></textarea></p>
        <img src="captcha.php"/>
    <p>Posalji<input type="text" name="code" /></p>
    <p><input type="submit" name="submit" value="Posalji" class="button" /></p>
    <p>"IPO" MASARIKOVA br.5, BEOGRAD</p>
</form>

</body>
</html>

【问题讨论】:

  • 你期待什么结果?你打开了error_reporting 吗?您是否尝试过访问 captcha.php 没有 header changed-type 以查看它是否打印了一些错误?
  • 我没有……但我会尝试……谢谢……
  • 我希望得到图像应该在哪里写一些随机字母和数字的组合......但我只得到破碎图片的图像......如果你知道我的意思......
  • 不知道该告诉你什么。我测试了你的代码,我得到一个随机字符串,其中有随机线穿过它......我确实必须找到一个calibri.ttf 文件并将其放在与captcha.php 相同的目录中。此外,必须将 captcha.php 中的 $_SESSION['string'] 更改为 $_SESSION['rand_code']
  • 好的...我也将.ttf文件放在与captcha.php相同的文件夹中...我不需要包含命令的额外行吗??

标签: javascript php jquery html css


【解决方案1】:

由于您将标题设置为header("Content-type: image/png");,因此页面不会呈现其他内容。

只需将您的captcha 和表单分开即可。

例如,您的 captcha.php 将是:

<?php
session_start();
// build the base captcha image

$img = imagecreatetruecolor(80, 30);

$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
$grey = imagecolorallocate($img, 150, 150, 150);
$red = imagecolorallocate($img, 255, 0, 0);
$pink = imagecolorallocate($img, 200, 0, 150);

// build the base captcha image - END

// building randomString
function randomString($length)
{
    $chars = "abcdefghijkmnopqrstuvwxyz023456789";
    srand((double)microtime() * 1000000);
    $str = "";
    $i = 0;

    while ($i <= $length) {
        $num = rand() % 33;
        $tmp = substr($chars, $num, 1);
        $str = $str . $tmp;
        $i++;
    }
    return $str;
}

for ($i = 1; $i <= rand(1, 5); $i++) {
    $color = (rand(1, 2) == 1) ? $pink : $red;
    imageline($img, rand(5, 70), rand(5, 20), rand(5, 70) + 5, rand(5, 20) + 5, $color);
}
// building randomString - END

// fill  image with a white rectangle
imagefill($img, 0, 0, $white);

$string = randomString(rand(7, 10));
$_SESSION['string'] = $string;

imagettftext($img, 11, 0, 10, 20, $black, "Gotham-Bold.otf", $string);
// fill  image with a white rectangle - END

//  type of image
header("Content-type: image/png");
imagepng($img);


?>

【讨论】:

  • @Igorbeginner,如果可以,请接受...干杯!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多