500 Internal Server Error
Author
Discussion

evenflow

Original Poster:

8,846 posts

305 months

Wednesday 3rd March 2010
quotequote all
Just released a PHP/HTML website onto a live environment. The code runs perfectly on my hosting company's server.
However, on the live env, most of the files work, but one doesn't.
I can see index.php and other pages, but if I try to look at contact.php I get a 500 Internal Server Error.
If it was permissions or something then surely nothing would work? Any ideas?!

jamieboy

5,921 posts

252 months

Wednesday 3rd March 2010
quotequote all
Does the contact.php page reference anything that might be a different version or not installed at all on the live server?

plover

363 posts

234 months

Wednesday 3rd March 2010
quotequote all
evenflow said:
Just released a PHP/HTML website onto a live environment. The code runs perfectly on my hosting company's server.
However, on the live env, most of the files work, but one doesn't.
I can see index.php and other pages, but if I try to look at contact.php I get a 500 Internal Server Error.
If it was permissions or something then surely nothing would work? Any ideas?!
What does the servers log file say ? If I had to guess, has someone updated the contact email address from a test to real email and mucked it up; missed a quote or something ?

evenflow

Original Poster:

8,846 posts

305 months

Wednesday 3rd March 2010
quotequote all
I've stripped all the PHP out of the file (everything between <?php ?> tags) and the HTML now shows in the browser?!

evenflow

Original Poster:

8,846 posts

305 months

Wednesday 3rd March 2010
quotequote all
Think it may be something to do with register_globals in php.ini

The first statement in the file is:

<?php
$urlID = "";
$urlID = $_GET['urlid'];
?>


If I replace that code with a simple print 'hello'; it runs...

Edited by evenflow on Wednesday 3rd March 11:34

marshalla

15,902 posts

224 months

Wednesday 3rd March 2010
quotequote all
Are you really sure the production server is running the same version of PHP ?

evenflow

Original Poster:

8,846 posts

305 months

Friday 5th March 2010
quotequote all
Well, I resolved it, but I'm not sure how.

The code at the very top of the file was originally:

<?php
$urlID = "";
$urlID = $_GET['urlid'];
?>


I replaced this with:

<?php
$urlID = "";
if (!empty($_GET['urlid']))
{
$urlID = $_GET['urlid'];
}
?>

and it runs. I didn't think you had to check for empty before trying to assign a variable like this?




marshalla

15,902 posts

224 months

Friday 5th March 2010
quotequote all
evenflow said:
Well, I resolved it, but I'm not sure how.

The code at the very top of the file was originally:

<?php
$urlID = "";
$urlID = $_GET['urlid'];
?>


I replaced this with:

<?php
$urlID = "";
if (!empty($_GET['urlid']))
{
$urlID = $_GET['urlid'];
}
?>

and it runs. I didn't think you had to check for empty before trying to assign a variable like this?
D'oh! That's a classic script error. What happens is that the $_GET['urlid'] returns an empty string so the assignment becomes

$urlID =

which is incomplete.

In other languages, the solution is to wrap the potentially empty result in a set of quotes which still allows it to be parsed and substituted by the interpreter - usually that means double quotes - so that if the result is an empty string, it will be a quoted empty string, which is still valid for the assignment.

So - does

$urlID="$_GET['urlid']" ;

work instead of your solution ? (according to the PHP docs it should, but I don't have time to verify it here today).


evenflow

Original Poster:

8,846 posts

305 months

Friday 5th March 2010
quotequote all
Thanks. I always thought PHP handled empty variables assignments like that without throwing an error?
Anyway, as you say probably better to do the check.

jamieboy

5,921 posts

252 months

Friday 5th March 2010
quotequote all
marshalla said:
evenflow said:
Well, I resolved it, but I'm not sure how.
D'oh! That's a classic script error. What happens is that the $_GET['urlid'] returns an empty string
PHP isn't something I know much about, I work with ASP.

But now that we've found the cause of the problem, what would cause evenflow's code to work on the dev server but fail on the live one?

evenflow

Original Poster:

8,846 posts

305 months

Friday 5th March 2010
quotequote all
jamieboy said:
But now that we've found the cause of the problem, what would cause evenflow's code to work on the dev server but fail on the live one?
Yes, exactly that. That's why I thought PHP handled empty variable assignments as no error on the dev server/test environment.

Maybe to do with the level of script error reporting set in the php.ini file?

American iv

468 posts

219 months

Saturday 6th March 2010
quotequote all
marshalla said:
evenflow said:
Well, I resolved it, but I'm not sure how.

The code at the very top of the file was originally:

<?php
$urlID = "";
$urlID = $_GET['urlid'];
?>


I replaced this with:

<?php
$urlID = "";
if (!empty($_GET['urlid']))
{
$urlID = $_GET['urlid'];
}
?>

and it runs. I didn't think you had to check for empty before trying to assign a variable like this?
D'oh! That's a classic script error. What happens is that the $_GET['urlid'] returns an empty string so the assignment becomes

$urlID =

which is incomplete.

In other languages, the solution is to wrap the potentially empty result in a set of quotes which still allows it to be parsed and substituted by the interpreter - usually that means double quotes - so that if the result is an empty string, it will be a quoted empty string, which is still valid for the assignment.

So - does

$urlID="$_GET['urlid']" ;

work instead of your solution ? (according to the PHP docs it should, but I don't have time to verify it here today).
what? The way you've written that suggests the flaw is because the script is first written to the page and then executed like JavaScript. Putting the $_GET within quotes will have no affect on this error and you'll still get an internal 500 error whilst using PHP.

The issue here is that PHP is throwing an error as it can't find the key within the $_GET array. The difference between production and dev boxes is that Production servers don't allow continuation after an error has been thrown whereas the dev boxes do.

The correct way to fix it has already been done (check if the variable is set), but you could always set the php ini settings within the script. Incidently, you could always replace your fix with the shorthand:

<?php
$urlID = (isset($_GET['urlid'])) ? $_GET['urlid'] : '';
?>

This defaults the string to '' if urlid isn't present within your query string.

As an aside, if you're using the urlid within a SQL query, make sure you have escaped it first (use mysql_real_escape_string) which will help stop SQL injection attacks.