hi every one I'm new and it really interesting what's happening here !!

Well I'm a beginner in PHP , the problem is that when i use print_r($message) or echo in the same page where I call header I got this message

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\xampp\crypter.php:13) in C:\xampp\htdocs\xampp\crypter.php on line 85

that is my code

<?php

$length = 5; // length of the string
$alphabet = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'; // possible character to use
$nb_characters = strlen($alphabet); // number of character

// the string that i want to randomize
$string = '';
for($i = 0; $i < $length; ++$i)
{
    $string .= $alphabet[mt_rand(0, $nb_characters-1)];
}
print($string); // here i want to printout my strin 
// we use the length of the string
$str_length = strlen($string);

// Creating the image zone whit the size specified 
$image = imagecreatetruecolor(30 * $str_length, 50);

// Creating the background 
for($x = 0; $x < imagesx($image); ++$x)
{
    for($y = 0; $y < imagesy($image); ++$y)
    {
        if (mt_rand(1,5) == 4)
        {
            $vred = mt_rand(100, 150);
            $vgreen = mt_rand(100, 150);
            $vblue = mt_rand(100, 150);
			
        }
        else
        {
			$vred = 255;
            $vgreen = 255;
            $vblue = 255;
        }

        // Allocating background color
        $color = imagecolorallocate($image, $vred, $vgreen, $vblue);

        // shohws the pixel
        imagesetpixel($image, $x, $y, $color);

        // delete the color
        imagecolordeallocate($image, $color);
    }
}

// Creating border
$vred = mt_rand(1, 80);
$vgreen = mt_rand(81, 160);
$vblue = mt_rand(161, 240);

// Allocating color
$color = imagecolorallocate($image, $vred, $vgreen, $vblue);

// Tracing the border
imagerectangle($image, 0, 0, imagesx($image)-1 , imagesy($image)-1, $color);

//deleting the color allocated
imagecolordeallocate($image, $color);

// Creating the text
for($i = 0; $i < $str_length; ++$i)
{
    $vred = mt_rand(1, 80);
    $vgreen = mt_rand(81, 160);
    $vblue = mt_rand(161, 240);
	$size = mt_rand(20, 30);
    $angle = mt_rand(-10, 10);
    $x = 13 + (20 * $i);
    $y = mt_rand(30, imagesy($image) - 10);
    $color = imagecolorallocate($image, $vred, $vgreen, $vblue);
    $font = 'AnkeCalligraph.TTF';

    // Drawing the text
    imagettftext($image, $size, $angle, $x, $y, $color, $font, $string[$i]);

    // deleting the color
    imagecolordeallocate($image, $color);
}

// creatin the image
header("Content-type: image/png");
imagepng($image);
?>

You always have to use "header()" before you generate any output. With the first output, PHP sends automatically the standard-header which cannot be manipulated afterwords.

And why do you output the generated text in an image? Remember, that the output of the php-script will be treated as an image! If you want to log each steps of your script for debugging purposes that you better open a file to write and log your stuff in there.

OK, Zero fast faster.

Hi,
HTTP headers (in PHP, headers are sent by header() function) are first things sent to client. Sending them after output (echo, print... - anything that is sending some data to the client) will produce an error and modified headers aren't sent. In your case, you must move line 13 after line 85, because you are outputting $string before modifying headers. So remember - send headers before outputting data.

:D -- I'm late

thanks all of you,

I really didn't know this . But the problem is that the image doesn't appear , what can I do ?

Make sure that the variable '$image' stored the correct data. More infos need and you need to post code including the function 'imagepng()'.

please could you show me how ? I'm still beginner , and I don't really know how to generate the code for including the function 'imagepng()' . about '$image' I think that stored the correct data .

Where is the 'imagepng($image)' function you used. What type of the data that the '$image' assigned. If the '$image' assigned the image path. You can easily echo the image tag with PHP. <img src="<?php echo $image; ?>" alt="" />

I got as image's data these one

$image = imagecreatetruecolor(30 * $str_length, 50);

.
.

imagecolorallocate($image, $vred, $vgreen, $vblue);

.
.

imagesetpixel($image, $x, $y, $color);

.
.

imagettftext($image, $size, $angle, $x, $y, $color, $font, $string[$i]);

if in line 75 : font = 'AnkeCalligraph.TTF';
the TTF file must be in the same directory.
do you have it.

header('Content-type: image/png');

It should be very top of the script after the opening tag of php '<?php'.

yes it works perfectly. but the problem is that once I use the function print() and even after the declaration header () the image does not appear!

Sometimes the warning is caused by a space before the opening php tag.

but there is no space before the opening php tag, I've just verified !

sometimes the included files have spaces before the opening or closing tags too.

another option could be if you include an HTML file.

e.g.

<?php include_once('file.html'); ?>

<?php header('x-test: 123'); ?>

<?php echo 123; ?>

can you pass it through a debugger ?

Actually I got no debugger , would you give me the name of some debugger ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.