0
votes

I've taken over a website that has been built on PHP. Basically, most pages do not display correctly in IE, and I've worked this out to be because the DOCTYPE isn't getting set.

Upon further investigation, it seems all the head information (including the DOCTYPE) is in a PHP include file called header.php, which is fair enough, however on the pages that do not load correctly, there is code before this include is called for which is why the DOCTYPE isn't being set.

It would be easy to move the include for header.php to the first line of the code, but the first lines of code are PHP variables that include keywords, page description and the page title, which the header.php include then refers to. If I move the include code to above these variables then the header.php won't know the keywords, page title or description as they won't have been set yet.

The whole site has been built like this - not sure how it's ever worked.

Is there a way around this that I can't see for looking? I've included the header.php file part of a page that doesn't display correctly below.

Any help gratefully received and thank you for taking time to read this.

header.php ->

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title><?php echo $pageTitleDisp ?></title>
    <meta name="keywords" content="<?php echo $pageKeywords?>" />
    <meta name="description" content="<?php echo $pageDesc?>" />
    <meta http-equiv="content-language" content="en-gb" />
    <meta name="copyright" content="My company" />
    <meta name="revisit-after" content="3 days" />
    <meta name="Robots" content="index, follow" />
    <meta name="Robots" content="all" />
    <meta name="Distribution" content="Global" />
    <meta name="Author" content="Me"/>
    <meta name="Accessibility" content="WCAG Double-A" />
    <meta name="Audience" content="All Users" />
    <meta name="Coverage" content="UK" />
    <meta name="Language" content="English" />
    <meta name="Rights" content="Reproduction by Permission Only" />
    <meta name="Rights.Copyright" content="My company" />
    <meta name="Type" content="Web Page" />
    <link href="<?php echo $canonical; ?>"  rel="canonical" />
    <link href="/css/webstyle.css" rel="stylesheet" type="text/css" />
    <script src="/js/site-functions.js" type="text/javascript"></script>
    <!--[if IE 6]><style type="text/css">img,div { behavior: url(/js/iepngfix.htc) }</style><![endif]-->
    <!--[if IE 7]><link href="/css/ie7.css" rel="stylesheet" type="text/css" /><![endif]-->  

and this is a page that then calls the above header.php info:

    <?php include("../includes/config.php"); ?>
    <?php 
    // SEO TAGGING 
    $pageTitleDisp="Page Title";
    $pageKeywords="cars planes bikes";
    $pageDesc="this is a webpage that looks pap in IE";
    $canonical="http://mydomain.com/about/";
    ?>
    <?php $popbox="true"; ?>
    <?php include("../includes/header.php"); ?>

After some suggestions from helpful folk, it seems only the pages that call the config.php have this problem. Below is the config.php file - if anyone has the time or knowledge to spot what could be causing the problem I'd be very grateful. If I comment out the calling of config.php then the page doesn't show at all!

    <?php
$user="user";
$password="password";
$database="DB";
mysql_connect("my.database.com",$user,$password);
@mysql_select_db($database) or die( "Unable to select database");

$bulkPrice="24.99";
$singlePrice="3.49";

function makeClickableLinks($text) {

  $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_+.~#?&//=]+)',
    '<a href="\1" target="_blank">\1</a>', $text);
  $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_+.~#?&//=]+)',
    '\1<a href="http://\2" target="_blank">\2</a>', $text);
  $text = eregi_replace('([_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3})',
    '<a href="mailto:\1" target="_blank">\1</a>', $text);

return $text;

}

function multiInsert($table,$cols){
                $cols= explode(",", $cols);

                     foreach ($cols as $colname) {
                                            $sqlinsert1.= "$colname, ";
                                            global $$colname;
                                            if ($colname=="dateAdded")
                                                        {
                                                        $sqlinsert2.= "Now(), ";
                                                        }
                                                    else {
                                                        $sqlinsert2.= "'".$$colname."', ";
                                                        }
                                              }
                        $sqlinsert1=removeComma($sqlinsert1);
                        $sqlinsert2=removeComma($sqlinsert2);               
                        $sqlinsert= "($sqlinsert1) values ($sqlinsert2)";

                $query="insert into $table $sqlinsert ";
                $result=mysql_query($query)or die(  mysql_error()."<BR><BR><B>$query</b>"); 
                return mysql_insert_id();
}


function cleanUserText ($para){

    $para=html_entity_decode($para);

  $array = array("itxtvisited=\"1\"","<EM>","<EM >","</EM>","<em>","</em>","<i>","</i>");
  $para = str_replace($array, "", $para);

$para = str_replace("\n", "<BR>", $para);
$para = str_replace("\n\b", "<BR>", $para);

  $para = str_replace("&amp;quot;", "\"", $para);
      $para = str_replace("&amp;", "&", $para);

     $para = str_replace("•", "&middot;", $para);
     $para = str_replace("’", "'", $para);
    $para = str_replace("®", "", $para);
    $para = str_replace("™", "", $para);
    $para = str_replace("…", "...", $para);
   $para = str_replace("“", "\"", $para);
   $para = str_replace("”", "\"", $para);


    $para = str_replace('<span style="font-weight: bold;">', "<B>", $para);
        $para = str_replace('<span style="font-weight: bold;" class="largeblack">', "<B>", $para);
         $para = str_replace('<i style="font-weight: bold;">', "<B>", $para);
      $para = str_replace('BACKGROUND: white', "", $para);

     $para = str_replace('</span>', "</B>", $para);

        $search = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript
                '@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
               '@<![\s\S]*?--[ \t\n\r]*>@',        // Strip multi-line comments including CDATA
                '@<SPAN[^>]*?>@siU',    // Strip style tags properly
                '@</SPAN>@siU',    // Strip style tags properly
                '@<FONT[^>]*?>@siU',    // Strip style tags properly
                '@</FONT>@siU',    // Strip style tags properly
                    );
        $para = preg_replace($search, '', $para);

$para=makeClickableLinks($para);

    return $para;


}




function encLink($name){
    $name=strtolower($name);
$name=str_replace(" ","-",$name);
return "$name";


}

function decLink($name){
    $name=strtoupper($name);
$name=str_replace("-"," ",$name);
return "$name";


}


function getColours(){

    global $colours;

$query="SELECT * from colours";
$result=mysql_query($query)or die(mysql_error()); ;

while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
            foreach($row as $k => $v){$$k = $v;}
            $colours["$colourID"]="$colourTitle";
                }   
}


function dispCatLink($catsLink){


$catsList = explode(",", $catsLink); 
$catsLink1=$catsList[0];


$query="SELECT * FROM cats  where catID = $catsLink1";
$result=mysql_query($query)or die(mysql_error()."Query is: <b>$query</b>"); 
$row = mysql_fetch_assoc($result);
if ($row){foreach($row as $k => $v){$$k = $v;}}


$catTitleF=str_replace(" ","-",$catTitle);
echo "<a href=/$catTitleF/products/>$catTitle</a>";

}

function alertMSG($alerter){

if ($alerter) echo "<div ID=alerter><img src=/icon/asterisk_yellow.png class=icn width=16 height=16 />$alerter</div>";

}

function open_image ($file) {
        # JPEG:
        $im = @imagecreatefromjpeg($file);
        if ($im !== false) { return $im; }
        # GIF:
        $im = @imagecreatefromgif($file);
        if ($im !== false) { return $im; }
        # PNG:
        $im = @imagecreatefrompng($file);
        if ($im !== false) { return $im; }
        # WBMP:
        $im = @imagecreatefromwbmp($file);
        if ($im !== false) { return $im; }
       # Try and load from string:
        $im = @imagecreatefromstring(file_get_contents($file));
        if ($im !== false) { return $im; }
        return false;
}

function upFile($upfile){


    if ($_FILES[$upfile]['tmp_name']){


                $newName= str_replace(" ","_",basename( $_FILES[$upfile]['name']));
                    $dest=  $_SERVER[DOCUMENT_ROOT]."/uploads/".$newName;
                    if(move_uploaded_file($_FILES[$upfile]['tmp_name'], $dest)) {

                         return $newName;
                    }
                    else {
                        echo $_FILES[$upfile]['name']." - There was an error uploading the file, please try again!";

                        return "";
                    }
        }
        else {
            return "";
        }

}



    function multiUpdate($table,$clause,$cols){
                $cols= explode(",", $cols);

                     foreach ($cols as $colname) {
                                            global $$colname;
                                            $sqlinsert.= " $colname = '".$$colname."',";
                                }

                    $sqlinsert=removeComma($sqlinsert);


                    global $$clause;

                $query="update $table set $sqlinsert where $clause=".$$clause;
                $result=mysql_query($query)or die(  mysql_error()."<BR><BR><B>$query</b>");

}

  function removeComma($var){
        if(substr($var, -1)==","){$var = substr($var,0,-1);}
        if(substr($var, -2)==", "){$var = substr($var,0,-2);}
        return $var;
                        }


function conv($size){

$converted=2.54*$size;
$converted= sprintf("%01.0f", $converted);

if ($converted=="0"){$converted="N/A ";}
return $converted;
}

function dispThumbs($prodID,$max_i,$size){



        for ($i=1; $i<=$max_i; $i++)  {

         $filename=$_SERVER['DOCUMENT_ROOT']."/prodImages/$size/".$prodID."/".$i.".jpg";

            if (file_exists($filename)) {
                        echo "<a href='/photos/$prodID/$i/'><img  border=0 class='imgTH' width=100 height=67 src=/prodImages/$size/".$prodID."/".$i.".jpg></a>";

                }

        }
}



        function open_dir ($dir){
        if (is_dir($dir)) {
            if ($dh = opendir($dir)) {
                while (($file = readdir($dh)) !== false) {
                        if (preg_match ("/.jpg/i", "$file")){
                        $files.="$file,";
                    }
                }
                closedir($dh);
            }
        }
        if(substr($files, -1)==","){$files = substr($files,0,-1);}
        return $files;
}



  function sec2hms ($sec, $padHours = false) 
  {

    $hms = "";

    $hours = intval(intval($sec) / 3600); 

//    $hms .= ($padHours) 
   //       ? str_pad($hours, 2, "0", STR_PAD_LEFT). ':'
  //       : $hours. ':';

    $minutes = intval(($sec / 60) % 60); 

    $hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ':';

    $seconds = intval($sec % 60); 

    $hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT);

    return $hms;

  }

     function checkIMG($imgLink){

    if (file_exists($_SERVER['DOCUMENT_ROOT'].$imgLink)) {
        echo $imgLink;
        }
        else {
         echo "/content/images/nophoto.gif";
        }

   }

        function checkTH($imgLink){

    if (file_exists($_SERVER['DOCUMENT_ROOT'].$imgLink)) {
        echo $imgLink;
        }
        else {
         echo "/images/noimage.gif";
        }

   }

function getIMGsize($img){


list($width, $height, $type, $attr) = getimagesize($_SERVER['DOCUMENT_ROOT'].$img);
echo "params='lightwindow_width=$width,lightwindow_height=$height'";

}


function  titleCase($string)  { 
        $len=strlen($string); 
        $i=0; 
        $last= ""; 
        $new= ""; 
        $string=strtoupper($string); 
        while  ($i<$len): 
                $char=substr($string,$i,1); 
                if  (ereg( "[A-Z]",$last)): 
                        $new.=strtolower($char); 
                else: 
                        $new.=strtoupper($char); 
                endif; 
                $last=$char; 
                $i++; 
        endwhile; 

        $new= str_replace("Led ","LED ",$new);
        $new= str_replace("Lcd ","LCD ",$new);

        return($new); 
}; 




?>
1
Is the config called on every page? - Paul Radich
Why do you say the DOCTYPE isn't getting set. There is nothing wrong with having the php code first to set variables for the header. Use the browser's page viewer (F12) to compare the ones that work to the one's that don't. - mseifert
Hi - thanks for your responses. The config.php is not called on every page. If I use IE's developer tools and compare a page that is working to one that isn't, I can see the doctype isn't being set. - Webbnos
Since your are including header.php, it "should" write the header shown in the code. When I post your code into a new document, I get the header. Make sure your paths are correct - is error reporting on? If no error, perhaps something is wrong with config.php? Does it output any html? What happens if you comment it out, does the header still not show. - mseifert
Hi, thank you for your comments - I have investigated and the pages that do work don't call the config.php so it must be something in this file. If I comment it out then the page doesn't load at all. I'll add the config file to my post. If anyone has the time or knowledge to scan through and see if they can see the problem I'd be very grateful. I've looked but am new to PHP. Thank you in advance, Sam. - Webbnos

1 Answers

0
votes

Given that the problem is most likely one of whitespace, nothing is going to be ideal. The simple answer would be to just put the doctype in its own file and prepend every single page with <?php include("../includes/doctype.php"); ?> before anything else. Like I said, not ideal, but with a site like that there's no neat and tidy solution.

I'm assuming that the inclusion of config.php isn't consistent across the whole site.

EDIT: The config.php file has whitespace before the opening <?php tag. Delete that and any after the last ?> tag and you should be fine. If that doesn't work, the next step would be to supply the first few lines of the output the browser receives, or a URL to the site.

Final edit: It's definitely a whitespace issue. You can tell by viewing source on the application page, it has a blank line before the DTD. Consider refactoring the pages to use a single PHP tag instead of one for each line, like this:

<?php include("../includes/config.php");

// SEO TAGGING 
$pageTitleDisp="Page Title";
$pageKeywords="cars planes bikes";
$pageDesc="this is a webpage that looks pap in IE";
$canonical="http://mydomain.com/about/";
$popbox="true";

include("../includes/header.php"); ?>

There's no good reason to have separate PHP tags on each line. Also consider removing the closing ?> tag from the config.php file.