954,242 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

I Get Message "Server Error" while trying to run a php script with GD

I am getting the 500 Server Error message when I try to run my captcha script. The script is as follows:

<?php
//Start the session so we can store what the security code actually is
session_start();

//Send a generated image to the browser
create_image();
exit(); 

function create_image()
{
    /*//Let's generate a totally random string using md5
    $md5_hash = md5(rand(0,999));
    //We don't need a 32 character long string so we trim it down to 5
    $security_code = substr($md5_hash, 15, 5); */
	
	$codelength = 6;
	while($newcode_length < $codelength) {
	$x=1;
	$y=3;
	$part = rand($x,$y);
	if($part==1){$a=48;$b=57;}  // Numbers
	if($part==2){$a=65;$b=90;}  // UpperCase
	if($part==3){$a=97;$b=122;} // LowerCase
	$code_part=chr(rand($a,$b));
	$newcode_length = $newcode_length + 1;
	$newcode = $newcode.$code_part;
	}
	$security_code = $newcode;

    //Set the session to store the security code
    $_SESSION["security_code"] = $security_code;

    //Set the image width and height
    $width = 100;
    $height = 22;  

    //Create the image resource
    $image = ImageCreate($width, $height);  

    //We are making three colors, white, black and gray
    $white = ImageColorAllocate($image, 255, 255, 255);
    $black = ImageColorAllocate($image, 0, 0, 0);
    $grey = ImageColorAllocate($image, 204, 204, 204); 
	$red = ImageColorAllocate($image, 255, 0, 0); 

    //Make the background black
    ImageFill($image, 0, 0, $white); 

    //Add randomly generated string in white to the image
    ImageString($image, 5, 26, 3, $security_code, $black); 

    //Throw in some lines to make it a little bit harder for any bots to break
    ImageRectangle($image,0,0,$width-1,$height-1,$grey);
    imageline($image, 0, $height/2, $width, $height/2, $red);
    imageline($image, $width/2, 0, $width/2, $height, $red);
    //imageline($image, 0, 0, $width, $height, $grey);
    //imageline($image, $width, 0, 0, $height, $red); 

    //Tell the browser what kind of file is come in
    header("Content-Type: image/jpeg"); 

    //Output the newly created image in jpeg format
    ImageJpeg($image); 

    //Free up resources
    ImageDestroy($image);
}
?>


I am using a Mandriva "Mandrake" Linux server with Apache2 and PHP5 both installed. I have GD installed as well so I have no idea what the problem could be.

sun-tzu
Light Poster
40 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

what error you are getting in apache and php error logs???

---
Manoj

manojsamtani
Light Poster
Banned
37 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
Infraction Points: 5
 

Dear,

If you have root access to the server then first login to the server as root.


Then type :

tail -f /usr/local/apache/log/error_log

for displaying the apache error log.

the syntax : tail -f /log/error_log

jino
Junior Poster
117 posts since Feb 2008
Reputation Points: 10
Solved Threads: 10
 

I'm not a coder so I don't understand your PHP code. However, I admin Apache and know that some scripts which use GD also use true type fonts. If that's the case, then both GD and TTF need to be compiled into Apache. Checking your logs is a good idea, too.

vectro
Junior Poster in Training
64 posts since Oct 2008
Reputation Points: 10
Solved Threads: 1
 

The code above works on my PC. Creates a beautiful white rectangle with a slim red cross inside. Plus the random 6 byte character string. Cool.

Now, HTML code 500. Usually a trivial syntax error:

session_start(;


rather than

session_start(;


throws a 500.

Cool,too

linuxaomi
Newbie Poster
17 posts since Jul 2011
Reputation Points: 10
Solved Threads: 1
 

should read:

session_start(;


rather than

session_start();


throws a 500.

Not my day, sorry.

linuxaomi
Newbie Poster
17 posts since Jul 2011
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: