PHP:无法解释的解析错误

时间:2014-03-30 09:54:22

标签: php apache syntax-error

嗯,我想把自己想象成一个非常有经验的php编码器,但我实际上不能为我的生活弄清楚我的代码有什么问题。 所以我写了这个小脚本来创造一个漂亮,丰富多彩的背景,也许偶尔扔进一个复活节彩蛋。没有什么太严肃,只是有点乐趣。无论哪种方式它工作正常,它只会引发相当随机的错误。

代码如下所示:http://pastebin.com/5ndL1E7J

<?
header("Content-Type: image/png");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
header("Cache-Control: no-store, no-cache, must-revalidate"); 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

$bgtime = file_get_contents('bgtime');

if( $bgtime <= time() ) {
    file_put_contents( 'bgtime', time() + 5 );
    $im = imagecreate( 1920, 955 );
    imagecolorallocate( $im, 255, 255, 255 );
    $red = imagecolorallocatealpha( $im, 136, 34, 34, 235);
    $green = imagecolorallocatealpha( $im, 34, 136, 34, 235);
    $blue = imagecolorallocatealpha( $im, 34, 34, 136, 235);

    for( $x=0; $x<88; $x++ ) {
        for( $y=0; $y<44; $y++ ) {      
            if( mt_rand( 0, 1 ) ) {
                $col = mt_rand( 0, 100 );
                if( $col < 90 ) {
                    imagefilledrectangle( $im, $x*22-10, $y*22-10, $x*22+10, $y*22+10, $green );
                } else if( $col < 98 ) {
                    imagefilledrectangle( $im, $x*22-10, $y*22-10, $x*22+10, $y*22+10, $blue );
                } else {
                    imagefilledrectangle( $im, $x*22-10, $y*22-10, $x*22+10, $y*22+10, $red );
                }
            }
        }
    }
    imagepng( $im, 'background.png' );
    imagepng( $im );
    imagedestroy( $im );

} else if( mt_rand( 0, 10 ) == 1 ) {
    $texts = [ "#<<_ Over#######>>> 9000", "#<<_ Game#######>>>_ Over" ];

    include '../fonts/pokebet.charray';

    $im = imagecreatefrompng( 'background.png' );
    $text = str_split( $texts[array_rand($texts)] );
    $red = imagecolorallocatealpha( $im, 136, 34, 34, 235);

    $offx = 0;
    $offy = 0;

    foreach( $text as $char ) {
        if( $char == '#' ) {
            $offx += 9;
            $offy = 0;
        } else if( $char == ">" ){
            $offx += 1;
        } else if( $char == "<" ) {
            $offx -= 1;
        } else if( $char == "_") {
            $offy += 5;
        } else if( $char == " ") {
            $offy += 1;
        } else {
            $offy -= $charray[$char][2];

            foreach( $charray[$char][0] as $pixel ) {
                $x = $pixel[0] + $offx;
                $y = $pixel[1] + $offy;

                imagefilledrectangle( $im, $x*22-10, $y*22-10, $x*22+10, $y*22+10, $red );
            }

            $offy += $charray[$char][1];
        }
    }
    imagepng( $im );
    imagepng( $im );
} else {
    include 'background.png';
}

?>

错误看起来像这样,声称的行和t_strings与每个错误差别很大:

PHP Parse error:  syntax error, unexpected '\xdeu\xdc\x81\xcb\xd7\xb59jb\xe5\xae\xfa' (T_STRING) in /var/www/inf1/images/background.png on line 31, referer: http://inf1.****.nl/

2 个答案:

答案 0 :(得分:1)

include 'background.png';将PNG图像粘贴到PHP代码中并尝试将其解析为PHP。当然,这是不可能的。你可能需要像fpassthru这样的东西:

$fp = fopen('background.php', 'rb');
fpassthru($fp);
fclose($fp);

答案 1 :(得分:1)

尝试将include 'background.png'更改为:

base64_encode(file_get_contents("background.png"));

echo将其发送到<img src= ...

像:

$imageData = base64_encode(file_get_contents("background.png"));

$src = 'data: '.mime_content_type($image).';base64,'.$imageData;

echo '<img src="',$src,'">';

或者可能是:

$file = 'background.png';

if (file_exists($file)) {
    header('Content-Type: image/png');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}

或者简单地说:

$file = 'background.png';
$type = 'image/png';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);

错误正在发生,因为图像数据中的特殊字符被读作PHP代码。