PHP displaying after </html> tag

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mattmartin
Forum Newbie
Posts: 3
Joined: Mon Jul 25, 2005 10:40 pm

PHP displaying after </html> tag

Post by mattmartin »

Hi,

I am trying use a DMOZ script on a site and I put the PHP inside of a table cell, but it showing at the end of the page instead of in the cell where I want it to. here is the code for template_index.php that is supposed to show up inside of the cell.

Code: Select all

<?
// HEADING LABELS: [CATEGORY_NAME] [CATEGORY_PATH]

$TEMPLATE["HEADING"] = <<<EOF

	<FORM ACTION="search.php">
	<INPUT TYPE="text" NAME="q" SIZE=80>
	<INPUT TYPE="submit" VALUE="Search" CLASS="BUTTON">
	</FORM>
	<HR SIZE=1 COLOR="#DDDDDD">
EOF;

// PATH:CATEGORY LABELS: [CATEGORY_URL] [CATEGORY_NAME]
// PATH:CURRENT_CATEGORY LABELS: [CATEGORY_NAME]

$TEMPLATE["PATH"]["HEADING"] = <<<EOF
	
EOF;
$TEMPLATE["PATH"]["CATEGORY"] = <<<EOF
	<A HREF="[CATEGORY_URL]">[CATEGORY_NAME]</A>
EOF;
$TEMPLATE["PATH"]["CURRENT_CATEGORY"] = <<<EOF
	[CATEGORY_NAME]
EOF;
$TEMPLATE["PATH"]["SEPARATOR"] = <<<EOF
	 : 
EOF;
$TEMPLATE["PATH"]["FOOTER"] = <<<EOF
	<HR SIZE=1 COLOR="#DDDDDD">
EOF;

// BAR:ITEM LABELS: [ITEM_URL] [ITEM_NAME]

$TEMPLATE["BAR"]["HEADING"] = <<<EOF
	
EOF;
$TEMPLATE["BAR"]["ITEM"] = <<<EOF
	<A HREF="[ITEM_URL]">[<B> [ITEM_NAME] </B>]</A>
EOF;
$TEMPLATE["BAR"]["FOOTER"] = <<<EOF
	<HR SIZE=1 COLOR="#DDDDDD">
EOF;

// CATEGORIES:CATEGORY LABELS: [CATEGORY_URL] [CATEGORY_NAME] [@] [CATEGORY_PAGES]

$TEMPLATE["SUBCATEGORIES"]["HEADING"] = <<<EOF
	<TABLE CELLPADDING=0 CELLSPACING=0 BORDER=0>
	<TR>
	<TD WIDTH=260 VALIGN=top>
EOF;
$TEMPLATE["SUBCATEGORIES"]["CATEGORY"] = <<<EOF
	<LI><A HREF="[CATEGORY_URL]"><B>[CATEGORY_NAME]</B></A>[@] [CATEGORY_PAGES]
EOF;
$TEMPLATE["SUBCATEGORIES"]["BETWEEN_COLUMNS"] = <<<EOF
	</TD><TD WIDTH=20>&nbsp;</TD><TD WIDTH=260 VALIGN=top>
EOF;
$TEMPLATE["SUBCATEGORIES"]["FOOTER"] = <<<EOF
	</TD>
	</TR>
	</TABLE>
	<HR SIZE=1 COLOR="#DDDDDD">
EOF;

// PAGES:HEADING LABELS: [TOTAL_PAGES] [CATEGORY_NAME]
// PAGES:PAGE LABELS: [PAGE_TITLE] [PAGE_DESCRIPTION] [PAGE_URL]

$TEMPLATE["PAGES"]["HEADING"] = <<<EOF
	[TOTAL_PAGES] pages found in [CATEGORY_NAME]:
EOF;
$TEMPLATE["PAGES"]["PAGE"] = <<<EOF
	<P><A HREF="[PAGE_URL]"><B>[PAGE_TITLE]</B></A><BR>
	[PAGE_DESCRIPTION]<BR>
	[PAGE_URL]
EOF;
$TEMPLATE["PAGES"]["FOOTER"] = <<<EOF
	<HR SIZE=1 COLOR="#DDDDDD">
EOF;

$TEMPLATE["ERROR"] = <<<EOF
	<I>ODP data temporarily unaccessible</I>
	<HR SIZE=1 COLOR="#DDDDDD">
EOF;

$TEMPLATE["FOOTER"] = <<<EOF

	<P><A HREF="http://www.site-directory.org/[CATEGORY_URL]" target="_blank">[CATEGORY_NAME] category</A> powered by <A HREF="http://www.site-directory.org/odp_script.php" target="_blank">Free PHP ODP Script</A> &copy; <A HREF="http://www.site-directory.org/" target="_blank">Site Directory</A>
EOF;
?>
and here is the code in index.php

Code: Select all

<?
require_once("config.php");
require_once("include.php");
require_once("template_index.php");
$dir = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if (!strpos($dir,"?") === false) $dir = substr($dir,0,strpos($dir,"?"));
$dir = substr($dir,0,strrpos($dir,"/"))."/";
if ($c){
	$c = substr($_SERVER['REQUEST_URI'],strpos($_SERVER['REQUEST_URI'],"index.php?c=")+12);
	$c2 = $c;
	$c2 = str_replace("%83%C2","",$c2);
	$c2 = str_replace("&%3b","%26",$c2);
	$c2 = preg_replace("/(%..)/e","strtolower('\\1')",$c2);
	if (substr($c2,strlen($c2)-1,1) == "/") $c2 = substr($c2,0,strlen($c2)-1);
	if (substr($c2,0,1) == "/") $c2 = substr($c2,1,strlen($c2)-1);
	if ($c != $c2){
		header("HTTP/1.1 301 Moved Permanently"); 
		header("status 301 Moved Permanently"); 
		header("Location: {$dir}index.php?c={$c2}");
		exit;
	};
};
$c_title_array = urldecode($c);
if (get_magic_quotes_gpc()) $c_title_array = stripslashes($c_title_array);
$c_title_array = str_replace("_"," ",$c_title_array);
$c_title_array = explode("/",$c_title_array);
if (substr($home_path,strlen($home_path)-1,1) == "/") $home_path = substr($home_path,0,strlen($home_path)-1);
if (substr($home_path,0,1) == "/") $home_path = substr($home_path,1,strlen($home_path)-1);
$path = $home_path;
if ($home_path & $c) $path .= "/";
$path .= $c;

function decode_title($title){
	if (function_exists("html_entity_decode")){
		$title = html_entity_decode($title,2,"ISO-8859-15");
	}else{
		$title = str_replace("&","&",$title);
	};
	return $title;
};
function decode_path($path){
	if (function_exists("html_entity_decode")){
		$path = urldecode($path);
		$path = html_entity_decode($path,2,"ISO-8859-15");
		$path = urlencode($path);
		$path = str_replace("%2F","/",$path);
	}else{
		$path = str_replace("%26amp%3b","%26",$path);
	};
	return $path;
};
function shorten_path($path){
	global $home_path;
	if (substr($path,0,1) == "/") $path = substr($path,1,strlen($path)-1);
	if (substr($path,strlen($path)-1,1) == "/") $path = substr($path,0,strlen($path)-1);
	if ($home_path){
		if (strpos($path,$home_path."/") === 0){
			$path = substr($path,strlen($home_path)+1);
		};
	};
	return $path;
};

$category_name = ($c)? $c_title_array[count($c_title_array)-1] : $site_title;
if ($c){
	$category_path = urldecode($c);
	$category_path = str_replace("/"," : ",$category_path);
	$category_path = str_replace("_"," ",$category_path);
	$category_path = $site_title." : ".$category_path;
}else{
	$category_path = $site_title;
};
$replace = array("[CATEGORY_NAME]" => htmlentities($category_name,2,"UTF-8"), "[CATEGORY_PATH]" => htmlentities($category_path,2,"UTF-8"));
echo strtr($TEMPLATE["HEADING"],$replace);

$c_array = explode("/",$c);
echo $TEMPLATE["PATH"]["HEADING"];
if (!$c){
	$replace = array("[CATEGORY_NAME]" => $site_title);
	echo strtr($TEMPLATE["PATH"]["CURRENT_CATEGORY"],$replace);
}else{
	$replace = array("[CATEGORY_URL]" => $dir, "[CATEGORY_NAME]" => $site_title);
	echo strtr($TEMPLATE["PATH"]["CATEGORY"],$replace);
	if (count($c_array) > 1){
		for ($x = 0; $x <= count($c_array)-2; $x++){
			if ($slash) $accumulated_path .= "/";
			$slash = true;
			$accumulated_path .= $c_array[$x];
			$replace = array("[CATEGORY_URL]" => $dir."index.php?c=".$accumulated_path, "[CATEGORY_NAME]" => htmlentities($c_title_array[$x],2,"UTF-8"));
			echo $TEMPLATE["PATH"]["SEPARATOR"].strtr($TEMPLATE["PATH"]["CATEGORY"],$replace);
		};
	};
	$replace = array("[CATEGORY_NAME]" => htmlentities($category_name,2,"UTF-8"));
	echo $TEMPLATE["PATH"]["SEPARATOR"].strtr($TEMPLATE["PATH"]["CURRENT_CATEGORY"],$replace);
};
echo $TEMPLATE["PATH"]["FOOTER"];
flush();

if ($use_cache){
	$filename = "{$cache_directory}/browse".md5($path);
	if (file_exists($filename)){
		if (time()- filemtime($filename) < $cache_expiration){
			$cache = true;
		};
	};
};
if (!$cache){
	$url = "http://dmoz.org/{$path}";
	if (strlen($path) > 0 & strpos($path,".html") === false) $url .= "/";
	if((@$fp = fopen($url,"r")) != false){
		$file = "";
		while(!feof($fp)) {
			$file = $file . fread($fp, 1024);
		};
		fclose($fp);
	}else{
		$error = true;
		if ($cached){
			$cache = true;
		}else{
			$access_error = true;
		};
	};
	if (!$error){
		if (!$path){
			$n_blocks += 1;
			$n_categories += 1;
			$odp["categories"][$n_blocks][$n_categories]["category"] = "Adult";
			$odp["categories"][$n_blocks][$n_categories]["path"] = "/Adult/";
			while (eregi("(<b><a href=\")([^\"]+)(\">)([^\n]+)(</a></b>)",$file,$out)){
				$n_categories += 1;
				$odp["categories"][$n_blocks][$n_categories]["category"] = ereg_replace("(<[^>]+>)","",$out[4]);
				$odp["categories"][$n_blocks][$n_categories]["path"] = $out[2];
				$file = substr($file,strpos($file,$out[0])+strlen($out[0]));
			};
		}else{
			while (eregi("(\[|\|)( <a href=\")([^\"]+)(\"><b>)([^<]+)(</b></a>)",$file,$out)){
				$n_bar += 1;
				$odp["bar"][$n_bar]["category"] = $out[5];
				$odp["bar"][$n_bar]["path"] = $out[3];
				$file = substr($file,strpos($file,$out[0])+strlen($out[0]));
			};
			while (eregi("(<a href=\")([^\"]+)(\"><b>)([^<]+)(</b></a>@?\n &nbsp;<i>\()([^\)]+)",$file,$out)){
				$crossed = false;
				if (!strpos($out[5],"@") === false) $crossed = "@";
				if (strpos($file,$out[0]) > strpos($file,"<hr>")){
					$n_blocks += 1;
					$n_categories = 0;
				};
				if ($c_title_array[0] != "Adult" || $out[5] != "World"){
					$n_categories += 1;
					$odp["categories"][$n_blocks][$n_categories]["category"] = $out[4];
					$odp["categories"][$n_blocks][$n_categories]["path"] = $out[2];
					$odp["categories"][$n_blocks][$n_categories]["pages"] = $out[6];
					if ($crossed) $odp["categories"][$n_blocks][$n_categories]["crossed"] = $crossed;
				};
				$file = substr($file,strpos($file,$out[0])+strlen($out[0]));
			};
			while (eregi("(<li><a href=\")([^\"]+)(\">)([^<]+)(</a>)([^-]+)?(- )([^\n]+)(\n)",$file,$out)){
				$n_pages += 1;
				$odp["pages"][$n_pages]["url"] = $out[2];
				$odp["pages"][$n_pages]["title"] = $out[4];
				$odp["pages"][$n_pages]["description"] = $out[8];
				$file = substr($file,strpos($file,$out[0])+strlen($out[0]));
			};
		};
		if ($use_cache){
			if ((@$cf = fopen($filename,"w")) != false){
				fwrite($cf,serialize($odp));
				fclose($cf);
			};
		};
	};
};
if ($use_cache){
	if ($cache){
		if((@$fp = fopen($filename,"r")) != false){
			$odp = unserialize(stripslashes(fread($fp,filesize($filename))));
			fclose($fp);
		};
	};
};
if ($access_error){
	echo $TEMPLATE["ERROR"];
}else{
	if ($odp["bar"]){
		echo $TEMPLATE["BAR"]["HEADING"];
		for ($x = 1; $x <= count($odp["bar"]); $x++){
			$replace = array("[ITEM_NAME]" => htmlentities($odp["bar"][$x]["category"],2,"UTF-8"), "[ITEM_URL]" => $dir."index.php?c=".shorten_path($odp["bar"][$x]["path"]));
			echo strtr($TEMPLATE["BAR"]["ITEM"],$replace);
		};
		echo $TEMPLATE["BAR"]["FOOTER"];
	};
	if ($home_path){
		for ($x = 1; $x <= count($odp["categories"]); $x++){
			for ($y = 1; $y <= count($odp["categories"][$x]); $y++){
				if (strpos($odp["categories"][$x][$y]["path"],"/".$home_path."/") !== 0){
					array_splice($odp["categories"][$x],$y-1,1);
					array_unshift($odp["categories"][$x],"");
					unset($odp["categories"][$x][0]);
					$y--;
				};
			};
		};
		for ($x = 1; $x <= count($odp["categories"]); $x++){
			if (!count($odp["categories"][$x])){
				array_splice($odp["categories"],$x-1,1);
				array_unshift($odp["categories"],"");
				unset($odp["categories"][0]);
				$x--;
			};
		};
	};
	if (count($odp["categories"])){
		for ($x = 1; $x <= count($odp["categories"]); $x++){
			echo $TEMPLATE["SUBCATEGORIES"]["HEADING"];
			for ($y = 1; $y <= ceil(count($odp["categories"][$x])/2); $y++){
				$category_pages = ($odp["categories"][$x][$y]["pages"])? "(".$odp["categories"][$x][$y]["pages"].")" : "";
				$replace = array("[CATEGORY_NAME]" => htmlentities($odp["categories"][$x][$y]["category"],2,"UTF-8"), "[CATEGORY_URL]" => $dir."index.php?c=".shorten_path($odp["categories"][$x][$y]["path"]), "[@]" => $odp["categories"][$x][$y]["crossed"], "[CATEGORY_PAGES]" => $category_pages);
				echo strtr($TEMPLATE["SUBCATEGORIES"]["CATEGORY"],$replace);
			};
			echo $TEMPLATE["SUBCATEGORIES"]["BETWEEN_COLUMNS"];
			for ($y = ceil(count($odp["categories"][$x])/2)+1; $y <= count($odp["categories"][$x]); $y++){
				$category_pages = ($odp["categories"][$x][$y]["pages"])? "(".$odp["categories"][$x][$y]["pages"].")" : "";
				$replace = array("[CATEGORY_NAME]" => htmlentities($odp["categories"][$x][$y]["category"],2,"UTF-8"), "[CATEGORY_URL]" => $dir."index.php?c=".shorten_path($odp["categories"][$x][$y]["path"]), "[@]" => $odp["categories"][$x][$y]["crossed"], "[CATEGORY_PAGES]" => $category_pages);
				echo strtr($TEMPLATE["SUBCATEGORIES"]["CATEGORY"],$replace);
			};
			echo $TEMPLATE["SUBCATEGORIES"]["FOOTER"];
		};
	};
	if (count($odp["pages"])){
		$replace = array("[TOTAL_PAGES]" => count($odp["pages"]), "[CATEGORY_NAME]" => htmlentities($category_name,2,"UTF-8"));
		echo strtr($TEMPLATE["PAGES"]["HEADING"],$replace);
		for ($x = 1; $x <= count($odp["pages"]); $x++){
			$odp["pages"][$x]["title"] = decode_title($odp["pages"][$x]["title"]);
			$replace = array("[PAGE_TITLE]" => htmlentities($odp["pages"][$x]["title"],2,"UTF-8"), "[PAGE_DESCRIPTION]" => htmlentities($odp["pages"][$x]["description"],2,"UTF-8"), "[PAGE_URL]" => $odp["pages"][$x]["url"]);
			echo strtr($TEMPLATE["PAGES"]["PAGE"],$replace);
		};
		echo $TEMPLATE["PAGES"]["FOOTER"];
	};
};
echo <<<EOF
<!--//
DONT REMOVE THIS. READ http://dmoz.org/become_an_editor/
//-->
<TABLE BORDER="0" BGCOLOR="#666699" CELLPADDING="3" CELLSPACING="0"><TR><TD>
<TABLE WIDTH=100% CELLPADDING=2 CELLSPACING=0 BORDER=0><TR ALIGN=center>
<TD><FONT COLOR="#FFFFFF">Help build the largest human-edited directory on the web.</FONT></TD>
</TR><TR BGCOLOR="#DDDDDD" ALIGN=center>
<TD>&nbsp; <A HREF="http://dmoz.org/cgi-bin/add.cgi?where=$path">Submit a Site</A> - <A HREF="http://dmoz.org/about.html"><B>Open Directory Project</B></A> - <A HREF="http://dmoz.org/cgi-bin/apply.cgi?where=$path">Become an Editor</A> &nbsp;</TD>
</TR></TABLE>
</TD></TR></TABLE>
EOF;
$path_title_array = urldecode($path);
if (get_magic_quotes_gpc()) $path_title_array = stripslashes($path_title_array);
$path_title_array = str_replace("_"," ",$path_title_array);
$path_title_array = explode("/",$path_title_array);
$category_name = ($path)? $path_title_array[count($path_title_array)-1] : "Top";
$category_url = ($path)? "Top/{$path}/" : "";
$replace = array("[CATEGORY_NAME]" => htmlentities($category_name,2,"UTF-8"), "[CATEGORY_URL]" => $category_url);
echo strtr($TEMPLATE["FOOTER"],$replace);
?>
Can anyone tell me what is making it display after the </html> tag please?

Thanks, Matt
mattmartin
Forum Newbie
Posts: 3
Joined: Mon Jul 25, 2005 10:40 pm

Post by mattmartin »

Anyone?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

It's way too much to wade through :P

Things to solve it:

- validate your html..
- remove all the blocks, add them one by one back (and after each cycle, validate..)

- done ;)
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

mattmartin wrote:Anyone?
I've stared at it mutliple times, but I can't see where its generating any </html> elements in the first place so I can't tell why its getting out of order, etc. Do you know what outputs the </html> in the first place?
mattmartin
Forum Newbie
Posts: 3
Joined: Mon Jul 25, 2005 10:40 pm

Post by mattmartin »

Hi,

in template_index there is a </html> at the end, but that's the only one. I just took that php and stuck it in a table in an html template that I made, but it is displaying it at the very end of the html instead of in the cell.

Thanks, Matt
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Can you show the source of the output HTML? Also, can you run just the PHP code to see what parts of the PHP output are getting thrown after the HTML?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

your probebly missing a </tr> or a <td> or somtin like that somewhere but that right there is way to much to look at and go though
Post Reply