Browser is displaying PHP code instead of design. What is cause of this?

I have created a CMS image gallery which I am having one problem with. Sometimes when in the admin side of the site, after uploading an image or modifying various areas the page is loading up as code instead of the actual design. When this happens, this code is before the page code:

//
HTTP/1.1 200 OK
Date: Sun, 20 Jul 2008 15:36:46 GMT
Server: Apache/2
X-Powered-By: PHP/4.4.8
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Keep-Alive: timeout=15, max=93
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html

134f
//

and there is always a 0 at the bottom of the page, following the </html> command. The URL of the page will always be the name of the php file followed by the specific area that wants to be viewed, for example ‘?page=album-detail&alId=7.’ Does anyone know what causes this to happen?

Thanks
Russ

This is happening because your webserver isn’t configured to use PHP for processing the file, that you’re requesting. What is the name of the file, you’re requesting?

One of the pages where this happens is:

http://www.thedaffodilsociety.com/gallery/index.php

But it also happens on various other pages. How can I configure my webserver to process this file?

Russ

Come to think of it, this problem could also occur, if you use short-tags, and you haven’t configured PHP to use this. Can you show a link to a page, where the problem occurs?

Works fine for me :shifty:

Hey, have you tried looking at the link in my last post. It has definately been happening in this page. Sometimes it works and sometimes it doesn’t. And the same happens with a few other pages but they’re part of my admin system so don’t really want to give links to these.

But, yeah, if you go to http://www.thedaffodilsociety.com//gallery/index.php and keep clicking on the pictures and going back and so on, eventually you’ll get the error screen.

Russ

OK, that is seriously weird. For some reason, the HTTP response is malformed. I’m not sure what’s going wrong, but the entire HTTP response is treated as the body of the response, making it invalid HTML. The numbers (including the trailing zero) are part of the chunked-response protocol of HTTP1.1, and probably not related to the root-problem. I suspect, that that’s probably Geek to you, but it might help you solve the issue. Basically, I think that there may be a bug in the version of Apache, that you’re using, or perhaps in one of the loaded extensions (modules). The easiest thing to try, would be to upgrade/downgrade to a different version of Apache, if you have that option?

I’ll have your thread moved to the Apache Configuration forum, since this is a web server issue, and most likely not related directly to PHP.

kyber,

What is seriously weird is that the double // between the domain and gallery directory was processed, too (although it gave the same page as the single / version).

In my experience, you’ve gone through all the likely suspects:

  1. PHP not configured (properly - I still think that this may be a problem as slaterino may be trying to use PHP5 functions?)

  2. A part of that PHP configure “problem” may be the short tags - but that’s normally set on the server. Since he didn’t confirm (or deny), that’s still a possibility.

  3. Also likely, his Apache may be having a problem and needs to be restarted. If it’s his (test?) server, easy, otherwise he’ll have to report the problem to his host (which would have been my first step) and have them do an Apache restart (it’s fast but it will take down all the [shared?] websites on that server for a few seconds).

Regards,

DK

Not likely that this is the problem. If there is a runtime error in PHP, it shows up differently. Maybe if mod_php core_dumps, it could take Apache down as well, but then I would expect the server to crash; Not to keep responding.

After looking at the output, I’ll rule this out. Such a problem would display PHP source code in the output, but this isn’t the case. The so-called “code” in the output, is because of HTTP chunked-response and has nothing to do with PHP.

Did you try this, slaterino?

Hey guys,
Thanks so much for your help on this! I’m in contact with the web host at the moment and seeing if they’ll restart the server. It’s a shared host but I am hoping they’ll still restart it. I’ll let you know the outcome.

Russ

Ah, the apache server has been restarted but the problem persists. Does anyone have any other suggestions for fixing this? I know there are some suggestions above but I’m not sure how I can resolve these in layman’s terms. If someone could give us some pointers that would be great.

In the meantime, once I get the chance, which probably won’t be til tomorrow now I will delete the entries in the database and trying inputting some fresh data. See if that makes any difference…

Cheers
Russ

I’ve tried deleting all image data and adding image data again but am now having a problem in the admin area when uploading images. This code is prefacing the document that appears.

///

HTTP/1.1 200 OK
Date: Wed, 30 Jul 2008 18:42:24 GMT
Server: Apache/2
X-Powered-By: PHP/4.4.8
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Keep-Alive: timeout=15, max=93
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html

134f

///

Again I am having trouble on the pages that show the galleries. Is it possible that this is something to do with coding? Is it worth me posting on this page all my code in case that is the issue?

There was some earlier suggestions around ideas of PHP5 compatibility and shortform. How can I work out if these are the problems to this issue.

Thanks
Russ

slat,

If your page is displaying the headers, something is definitely wrong! I’ve never come across this before so I’d guess it’s in the coding - which would be at the start of your script.

Regards,

DK

i’ve realised that this problem is only occuring in Firefox and not in Internet Explorer! does anyone know why this might be the case and whether this helps explain anything!

russ

Could we by any chance see the code?

of course, this is the code for the three pages that are directly related to this problem

functions.php
///

<?php
function uploadImage($inputName, $uploadDir)
{
$image = $_FILES[$inputName];
$imagePath = ‘’;
$thumbnailPath = ‘’;

if (trim($image[‘tmp_name’]) != ‘’) {
$ext = substr(strrchr($image[‘name’], “.”), 1);

$imagePath = strtolower(md5(rand() * time()) . “.$ext”);
$result = move_uploaded_file($image[‘tmp_name’], $uploadDir . $imagePath);

if ($result) {
$result = createThumbnail($uploadDir . $imagePath, $uploadDir . $imagePath, FullImage_WIDTH);
$thumbnailPath=strtolower(md5(rand() * time()) . “.$ext”);
$result = createThumbnail($uploadDir . $imagePath, $uploadDir . ‘thumbnail/’ . $thumbnailPath, THUMBNAIL_WIDTH);

if (!$result) {
unlink($uploadDir . $imagePath);
$imagePath = $thumbnailPath = ‘’;
} else {
$thumbnailPath = $result;
}
} else {
$imagePath = $thumbnailPath = ‘’;
}

}

return array(‘image’ => $imagePath, ‘thumbnail’ => $thumbnailPath);
}

function createThumbnail($srcFile, $destFile, $width, $quality = 75)
{
$thumbnail = ‘’;

if (file_exists($srcFile) && isset($destFile))
{
$size = getimagesize($srcFile);
$w = number_format($width, 0, ‘,’, ‘’);
$h = number_format(($size[1] / $size[0]) * $width, 0, ‘,’, ‘’);

$thumbnail = copyImage($srcFile, $destFile, $w, $h, $quality);
}

}

function copyImage($srcFile, $destFile, $w, $h, $quality = 75)
{
$tmpSrc = pathinfo(strtolower($srcFile));
$tmpDest = pathinfo(strtolower($destFile));
$size = getimagesize($srcFile);

if ($tmpDest[‘extension’] == “gif” || $tmpDest[‘extension’] == “jpg”)
{
$destFile = substr_replace($destFile, ‘jpg’, -3);
$dest = imagecreatetruecolor($w, $h);
//imageantialias($dest, TRUE);
} elseif ($tmpDest[‘extension’] == “png”) {
$dest = imagecreatetruecolor($w, $h);
//imageantialias($dest, TRUE);
} else {
return false;
}

switch($size[2])
{
case 1: //GIF
$src = imagecreatefromgif($srcFile);
break;
case 2: //JPEG
$src = imagecreatefromjpeg($srcFile);
break;
case 3: //PNG
$src = imagecreatefrompng($srcFile);
break;
default:
return false;
break;
}

imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);

switch($size[2])
{
case 1:
case 2:
imagejpeg($dest,$destFile, $quality);
break;
case 3:
imagepng($dest,$destFile);
}
return $destFile;

}

function getPagingLink($totalResults, $pageNumber, $itemPerPage = 10, $strGet = ‘’)
{
$pagingLink = ‘’;
$totalPages = ceil($totalResults / $itemPerPage);

// how many link pages to show
$numLinks = 10;

if ($totalPages > 1) {
$self = ‘http://’ . $_SERVER[‘HTTP_HOST’] . $_SERVER[‘PHP_SELF’] ;

if ($pageNumber > 1) {
$page = $pageNumber - 1;
if ($page > 1) {
$prev = " <a href=\“$self?pageNum=$page&$strGet\”>[Prev]</a> “;
} else {
$prev = " <a href=\”$self?$strGet\">[Prev]</a> ";
}

$first = " <a href=\“$self?$strGet\”>[First]</a> ";
} else {
$prev = ‘’; // we’re on page one, don’t show ‘previous’ link
$first = ‘’; // nor ‘first page’ link
}

if ($pageNumber < $totalPages) {
$page = $pageNumber + 1;
$next = " <a href=\“$self?pageNum=$page&$strGet\”>[Next]</a> “;
$last = " <a href=\”$self?pageNum=$totalPages&$strGet\">[Last]</a> ";
} else {
$next = ‘’; // we’re on the last page, don’t show ‘next’ link
$last = ‘’; // nor ‘last page’ link
}

$start = $pageNumber - ($pageNumber % $numLinks) + 1;
$end = $start + $numLinks - 1;

$end = min($totalPages, $end);

$pagingLink = array();
for($page = $start; $page <= $end; $page++) {
if ($page == $pageNumber) {
$pagingLink = " $page “; // no need to create a link to current page
} else {
if ($page == 1) {
$pagingLink = " <a href=\”$self?$strGet\“>$page</a> “;
} else {
$pagingLink = " <a href=\”$self?pageNum=$page&$strGet\”>$page</a> ";
}
}

}

$pagingLink = implode(’ | ', $pagingLink);

$pagingLink = $first . $prev . $pagingLink . $next . $last;
}

return $pagingLink;
}

function showBreadcrumb()
{
if (isset($_GET[‘album’])) {
$album = $_GET[‘album’];
$sql = “SELECT al_name
FROM tbl_album
WHERE al_id = $album”;

$result = mysql_query($sql) or die(‘Error, get album name failed. ’ . mysql_error());
$row = mysql_fetch_assoc($result);
echo ’ > <a href="index.php?page=list-image&album=’ . $album . ‘">’ . $row[‘al_name’] . ‘</a>’;

if (isset($_GET[‘image’])) {
$image = $_GET[‘image’];
$sql = “SELECT im_title
FROM tbl_image
WHERE im_id = $image”;

$result = mysql_query($sql) or die('Error, get image name failed. ’ . mysql_error());
$row = mysql_fetch_assoc($result);

echo ’ > <a href=“index.php?page=image-detail&album=’ . $album . ‘&image=’ . $image . '”>’ . $row[‘im_title’] . ‘</a>’;
}
}
}

?>

///

config.php
///
<?php
session_start();

$dbhost = ‘xxxxxxxx’;
$dbuser = ‘xxxxxxxx’;
$dbpass = ‘xxxxxxxx’;
$dbname = ‘xxxxxxxx’;

define(‘ALBUM_IMG_DIR’, ‘/home/sites/thedaffodilsociety.com/public_html/gallery/images/album/’);

define(‘GALLERY_IMG_DIR’, ‘/home/sites/thedaffodilsociety.com/public_html/gallery/images/gallery/’);

define(‘THUMBNAIL_WIDTH’, 100);

define(‘FullImage_WIDTH’, 450);

$conn = mysql_connect ($dbhost, $dbuser, $dbpass) or die ("I cannot connect to the database because: " . mysql_error());
mysql_select_db ($dbname) or die ("I cannot select the database ‘$dbname’ because: " . mysql_error());
?>

///

thanks!

russ

Could you, or a moderator, modify your post to be wrapped in

 tags, please? It'll make it a little easier to read :)

Dan - members need to do that with their own code (if they really want help).

Slat - without going back into the php.net pages, it looks like you’re trying to output the images from GD which should result in the image’s headers being sent - and would be the cause of the problem. When I upload images, the purpose is to (resize and) save them THEN worry about outputting the stored image in the normal way. Try that and see if that doesn’t correct the problem.

Regards,

DK