captcha在本地工作,但不在服务器上!

时间:2011-02-16 11:21:07

标签: php session post captcha unset

我有一个非常讨厌的问题。我有一个验证码系统,可以使用xammp在我的本地网络设置上工作,但它在我的远程linux盒子上不起作用。我有一个teori,captcha.php文件以某种方式重新加载,但我尝试删除我的谷歌分析JavaScript,但它仍然无法正常工作。

这是我的HTML:

<div class="box">
                    <h2>Captcha</h2>
                    <div class="block">
                        <p>Are you human? Type in the text bellow to prove it.</p>  
                        <table><form action="homepage.php" method="post">

                        <tr>
                            <td><img src="captcha.php?width=120&height=40&characters=5" /></td>
                            <td>
                                <label for="security_code">Security Code:</label><input id="security_code" name="security_code" type="text" />
                            </td>
                        </tr>
                        <tr>
                            <td>&nbsp;</td>

                            <td><input type="submit" name="human" value="Submit" /></td>
                        </tr>
                        </form></table>

                    </div>
                </div

captcha.php

<?php
session_start();

/*
* File: CaptchaSecurityImages.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 03/08/06
* Updated: 07/02/07
* Requirements: PHP 4/5 with GD and FreeType libraries
* Link: http://www.white-hat-web-design.co.uk/articles/php-captcha.php
* 
* This program is free software; you can redistribute it and/or 
* modify it under the terms of the GNU General Public License 
* as published by the Free Software Foundation; either version 2 
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful, 
* but WITHOUT ANY WARRANTY; without even the implied warranty of 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
* GNU General Public License for more details: 
* http://www.gnu.org/licenses/gpl.html
*
*/

class CaptchaSecurityImages {

    var $font = 'fonts/monofont.ttf';

    function generateCode($characters) {
        /* list all possible characters, similar looking characters and vowels have been removed */
        $possible = '23456789bcdfghjkmnpqrstvwxyz';
        $code = '';
        $i = 0;
        while ($i < $characters) { 
            $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
            $i++;
        }
        return $code;
    }

    function CaptchaSecurityImages($width='120',$height='40',$characters='6') {
        $code = $this->generateCode($characters);
        /* font size will be 75% of the image height */
        $font_size = $height * 0.75;
        $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
        /* set the colours */
        $background_color = imagecolorallocate($image, 255, 255, 255);
        $text_color = imagecolorallocate($image, 20, 40, 100);
        $noise_color = imagecolorallocate($image, 100, 120, 180);
        /* generate random dots in background */
        for( $i=0; $i<($width*$height)/3; $i++ ) {
            imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
        }
        /* generate random lines in background */
        for( $i=0; $i<($width*$height)/150; $i++ ) {
            imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
        }
        /* create textbox and add text */
        $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
        $x = ($width - $textbox[4])/2;
        $y = ($height - $textbox[5])/2;
        imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
        /* output captcha image to browser */
        header('Content-Type: image/jpeg');
        imagejpeg($image);
        imagedestroy($image);
        $_SESSION['security_code'] = $code;
    }

}

$width = isset($_GET['width']) ? $_GET['width'] : '120';
$height = isset($_GET['height']) ? $_GET['height'] : '40';
$characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '6';

$captcha = new CaptchaSecurityImages($width,$height,$characters);

?>

检查功能的一部分:

if( $_SESSION['security_code'] != $code_input ){

    unset($_SESSION['security_code']);
    return 1; 
}

编辑:

好吧,我把它添加到了html:

<input type="submit" name="test" value="test" />

这到php:

if( isset($_POST["test"]) ){

    echo $_SESSION['security_code']; 

}

当我点击测试时,我会在本地显示图像中的代码,由captcha.php生成。然而,在服务器上,它显示一个随机数..这怎么可能?如果我发现更多内容,我会更新我的帖子

4 个答案:

答案 0 :(得分:1)

检查主机上是否安装了GD。

<?php
phpinfo();
?>

phpinfo()会告诉你。

检查标题为“GD”的部分,如果不存在则无法创建图像。

答案 1 :(得分:1)

我修复了它:从未理解错误,但意识到加载了captcha.php文件并以某种方式更改了会话变量。所以我把它添加到验证码

    if( isset($_SESSION['security_code']) ){
        return $_SESSION['security_code']; 
    }

我删除了所有

unset($_SESSION['security_code'])

现在,如果设置了会话代码,图像代码和会话代码保持不变。这可能是一种安全风险,因为用户可以强制使用验证码,但我认为用户不会那么做以破坏安全性。

答案 2 :(得分:0)

嘿,我有同样的问题,但我已经解决了我的问题。我的代码不同但有同样的问题。我将验证码生成代码放在验证码文件夹中,当时验证码不匹配。但是,当我从该文件夹中删除我的整个代码并将所有代码放在根文件夹中时,它工作正常。可能这会对你有所帮助......

答案 3 :(得分:0)

这个问题真的很糟糕,幸运的是解决方案很简单。转到:

/wp-config.php

找到一行(39左右)并注释掉这个函数调用:

// Turn register_globals off.
//wp_unregister_GLOBALS();

Worpress将不再杀死你的$ _SESSION变种。顺便说一句。如果你的服务器上有register_globals关闭,Worpress会让你的$ _SESSION也活着......奇怪:))

WP支持论坛http://wordpress.org/support/topic/wp-blog-headerphp-killing-sessions

的致谢