Forum Moderators: coopster

Message Too Old, No Replies

500 internal server error

Fixing a 500 internal server error

         

MartinWeb

3:24 am on Nov 7, 2009 (gmt 0)

10+ Year Member



Hello. I have a page that is written entirely in PHP. Whenever I attempt to view the page, I get a 500 internal server error. After contacting my hosting server they have told me that it is a problem in my scripting, and that the PHP generates no bugs in the error logs. What should I do? Any help would be greatly appreciated.

TheMadScientist

3:53 am on Nov 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I haven't received a 500 Error from a PHP script before...
They're much more common from an .htaccess file.

Are you using an .htaccess file?

Do you have any redirects or rewrites?

Is there anyway the PHP script is initiating a redirect (or something), which creates an infinite loop between the .htaccess and the script or the script and the script itself? (Maybe it's possible to generate a 500 with an inefficient script, or loop, or something, but it's not something I've had happen.)

StoutFiles

4:25 am on Nov 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Post the PHP file.

rsutherland75

2:45 pm on Nov 7, 2009 (gmt 0)

10+ Year Member



I have had the error if your page times out, you can change your settings in your php.ini file or use ini_set() via max_execution_time.

It is possible that your loop takes longer then you have set, i beleive if you do not have a max execution time set it defaults to 30 seconds, so i would assume that could be your problem.

MartinWeb

3:22 pm on Nov 7, 2009 (gmt 0)

10+ Year Member



Thank you for your help. I have gone over my code, and I have no include()s or headers(). I also tried set_time_limit(0); as rsutherland75 said, but that didn't work either. Does anyone have any suggestions? I am sorry that I can't post the script, but it is 15 hundred lines long.

StoutFiles

4:57 pm on Nov 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's probably timing out.

rocknbil

4:57 pm on Nov 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Possibly, but a simple timeout will still usually return a "blank page" to the browser in most PHP configurations (that I've seen :-) )

There is only one place to look first - your error logs. A 500 will be recorded there, no matter where it comes from. I find it odd that you say it doesn't.

Basically any request to a server that does not respond back to the browser - i.e., if the process dies or is killed before returning a response, or outputs to STDOUT before generating appropriate content-type headers - will generate a 500, regardless of the technology.

The only time it won't (that I'm aware of) is a process that is not an http request, such as a cron job. Is it possible this script is calling an external process, and that's what's causing it to die?

If you can't isolate the cause, do this:

<?php
header("content-type:text/html");
echo "HERE";
exit 0;
// rest of your code
?>

The exit is important, you want to know at what point it's dying so don't want to let the script continue. Do that, run it, move it down in your script 50-100 lines (or before, then after, the next function call.) Re run it, keep moving it down through your script until you zero in on the specific area that's causing the 500.

MartinWeb

5:59 pm on Nov 7, 2009 (gmt 0)

10+ Year Member



Thank you so much. I did what you suggested, and I found that my error exists here.

$q2="SELECT * FROM chess_play where id='$gameId' LIMIT 1";
$result2=mysql_query($q2) or die("Could not execute query : $q2." . mysql_error());
while($row=mysql_fetch_array($result2)){

Does anyone spot any problems? Thank you again.

rocknbil

2:27 am on Nov 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looks like standard stuff, and just fine. (Even if id is an integer field, you should get identical results, quoted or un quoted. What about inside the while - did you delete/comment all the while contents, like so?

while($row=mysql_fetch_array($result2)){
// delete all this
}

The use of "2" ($q, $q2, $result, $result2) variables leads me to think you might have one loop inside another, is it possible you have one misnamed, or something else that's causing it to crash?

MartinWeb

4:06 am on Nov 8, 2009 (gmt 0)

10+ Year Member



Thank you. I was only using "2" because I wanted to make sure that I wasn't calling the variables later and creating and error. I've now eliminated the error to this line-
while($row=mysql_fetch_array($result2)){

Here is all of the include variables-
$q2="SELECT * FROM chess_play where id='$gameId' LIMIT 1";
$result2=mysql_query($q2) or die("Could not execute query : $q2." . mysql_error());
while($row=mysql_fetch_array($result2)){

Does that line look correct? Thank you for all of your help.

rocknbil

8:06 pm on Nov 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've now eliminated the error to this line-
while($row=mysql_fetch_array($result2)){.....

Sorry, I don't see an error. Looks fine. Are you sure it's connected to the database? You didn't possibly close the connection somewhere before this code? (Should get an error if it's closed, but still . . . )

An aside, PHP executes inline (generally) so variables will overwrite previous values and even set the data type. Recycling is good. :-)

var $foo = Array ('one','two');
$foo = 3; // re-cast as integer
$foo = 'four'; // now it's a string
echo $foo; // s/b "four"

I hate to say this, but on large scripts (1500+ lines) I've had late nights where I had a syntax error and it still runs without error until it encounters the chunk of code that's causing it. An example is

function some_function () {
echo "blah";

function some_other_function () {
echo "blah too";
}
}

May cause an error as posted (or be valid code) but you get the drift . . . a function nested inside a function when I thought I had two. What I do is create a copy, begin stripping it out until I locate the errant chunk, then eliminate that chunk and rebuild it. Generally this is followed by a <facepalm>, but hopefully that is not the case for you.

rocknbil

8:35 pm on Nov 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



**Flash** Just hit me, my own words:

...if the process dies or is killed before returning a response, or outputs to STDOUT before generating appropriate content-type headers - will generate a 500 ...

You might also give this a stab.

header("content-type:text/html");
echo "entering loop <br>\n";
while($row=mysql_fetch_array($result2)){
// leave your code as-is
}
echo "exiting loop <br>\n";
exit;

Spread out the test to surround the loop, you might be dying and it may output a clue right there. But if you move the three-line echo/exit before and after, you'll never see it.

(Sorry for the exit 0, Perl and PHP are so similar I often mix them up . . . )

rocknbil

7:54 pm on Nov 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Silence for a day or two usually means it's solved . . . for posterity, what was the problem?

MartinWeb

2:44 am on Nov 12, 2009 (gmt 0)

10+ Year Member



Oh, sorry. The problem still persists. I am totally stumped. I attempted rocknbil's code, and I realized that the problem seems to occur in different parts of the script depending on where it ends. For example, I have the error appear when I run an empty MySQL get, but it also appears in the later code. Does anyone have any other ideas? Thanks.

TheMadScientist

2:54 am on Nov 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Could you please be specific about the appears later in the code portion too?

I still suspect something outside of PHP could be effecting the results in some way, but without the code, we're probably all really stumped even more than you are...

What does your server error log say regarding the 500 error?
(The error log should not be confused with the server log file.)

Does the page redirect in anyway?

Does the page include any files at all?
Maybe a 'connection.php' file for DB connections?

If so is it included internally /the/path/to/the/file.php or externally http://www.example.com/?

What exactly is supposed to happen when the page appearing to cause (or causing) the error is loaded?

Does it just 'echo out' the results, or is it included by another page, or...

The list of things we don't know about the situation, file and code just keeps going, so the best answer is probably: Check the Server Error Log file which should tell you more about the 500 error, and then post the error log entries (or a portion to let us have a look) plus the relevant code if you can't resolve the issue based on the error log.

TheMadScientist

3:09 am on Nov 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just a new little situation I ran into:

I have now managed to generate a 500 based solely on PHP, but mine had to do with a COOKIE check for login and redirect, where if the COOKIE was not present, the script served the login page rather than the content.

The login page was served to the requested location and the requested location was redirected back to from the cookie setting script after the login was complete.

There was an order of operations error for a certain login string, which, when you logged in as a certain (generic) user, made you attempt to keep logging in even though the COOKIE was set. The login script was also checking for the cookie and redirecting to the correct location if it was set...

The end result was: location requested, login page served, login set, redirected to the originally requested location, login page served, redirected to the originally requested location, login page served, redirected to the originally requested location, and on and on...

rocknbil

8:11 pm on Nov 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah the OP says nothing is showing the the error logs.

It's down to this, suggested above:

Create a copy, begin stripping it out until I locate the errant chunk, then eliminate that chunk and rebuild it.

MartinWeb

2:58 am on Nov 30, 2009 (gmt 0)

10+ Year Member



I finally figured out the cause of the 500 internal server error. My hosting server (FatCow) generates them on endless loops. At least I'll no how to fix them now...