Error when using template system

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
yechi2001
Forum Newbie
Posts: 10
Joined: Sun Apr 23, 2006 8:34 pm

Error when using template system

Post by yechi2001 »

Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hi guys 
I am very new to php so if I mess up thats why.

I am using a template system and i keep on getting this error

Code: Select all

PHP Notice: Undefined variable: Page in C:\Inetpub\wwwroot\chapter8\main.php on line 5 PHP Warning: fopen(templates/.tpl) [function.fopen]: failed to open stream: No such file or directory in C:\Inetpub\wwwroot\chapter8\global\i_template.php on line 9 PHP Warning: filesize() [function.filesize]: stat failed for templates/.tpl in C:\Inetpub\wwwroot\chapter8\global\i_template.php on line 10 PHP Warning: fread(): supplied argument is not a valid stream resource in C:\Inetpub\wwwroot\chapter8\global\i_template.php on line 10 PHP Warning: fclose(): supplied argument is not a valid stream resource in C:\Inetpub\wwwroot\chapter8\global\i_template.php on line 11 PHP Notice: Undefined variable: Page in C:\Inetpub\wwwroot\chapter8\main.php on line 8 PHP Notice: Undefined variable: sessErrors in C:\Inetpub\wwwroot\chapter8\main.php on line 11
here is the main.php file

Code: Select all

<?php
include_once("global/i_template.php");
session_start();

$strFileName = "templates/" . strtolower($Page) . ".tpl";
$objTemplate = new o_Template;
$objTemplate->m_LoadFile($strFileName);
$objTemplate->m_AddElements($Page);

if ($sessErrors)
	{	$objTemplate->m_AssignContent("ERRORS", $sessErrors);
	}
else
	{	$objTemplate->m_AssignContent("ERRORS", "");
	};

session_unregister("sessErrors");
$objTemplate->m_Output();
?>
And here is the i_template.php which is supposed to work but doesnt i think.

Code: Select all

<?php
class o_Template
	{
		var $p_strOutputContents = "";
		var $p_arrSections = array();
		var $p_arrPaired = array();

		function m_LoadFile($strFile, $strTagName = "")
			{	$objFile = fopen($strFile, "r");
				$strContents = fread($objFile, filesize($strFile));
				fclose($objFile);
				
				if (empty($strTagName))
					{	$this->p_strOutputContents = $strContents;
					}
				else
					{	$this->p_strOutputContents = str_replace("{{" . $strTagName . "}}", $strContents, $this->p_strOutputContents);
					};
					
			}
			
		function m_AssignContent($strTagName, $strReplace, $strContent="")
			{	
				if (empty($strContent))
					{	$this->p_strOutputContents = str_replace("{{" . $strTagName . "}}", $strReplace, $this->p_strOutputContents);
					}
				else
					{	return str_replace("{{" . $strTagName . "}}", $strReplace, $strContent);
					};
			}
			
		function m_DefineSection($strSectionName)
			{	$longSectionStart = strpos($this->p_strOutputContents, "{{section $strSectionName }}");
				$longSectionEnd = strpos($this->p_strOutputContents, "{{/section $strSectionName }}");
				
				if (($longSectionEnd > 0))
					{	$longSectionLength = $longSectionEnd - $longSectionStart + strlen("{{/section $strSectionName }}");
						$strSection = substr($this->p_strOutputContents, $longSectionStart, $longSectionLength);	
						$this->p_arrSections[$strSectionName] = trim($strSection);
						return $this->p_arrSections[$strSectionName];
					};
					
			}
			
		function m_AssignSection($strSectionName, $strReplace, $strContent="")
			{	$strSection = trim($this->p_arrSections[$strSectionName]);
				
				if (empty($strContent))
					{	
						if (!empty($strSection))
							{	$this->p_strOutputContents = str_replace(trim($strSection),  $strReplace . "\r\n" . $strSection, $this->p_strOutputContents);
							};
							
					}
				else
					{	
						if (!empty($strSection))
							{	return str_replace($strSection, $strReplace . "\r\n" . $strSection, $strContent);
							};
							
					};
					
			}

		function m_DefinePair($strPairName, $boolUseBoth=false)
			{	$longFirstStart = strpos($this->p_strOutputContents, "{{paired $strPairName }}");
				$longFirstEnd = strpos($this->p_strOutputContents, "{{/paired $strPairName }}");
				$longSecondStart = strpos($this->p_strOutputContents, "{{unpaired $strPairName }}");
				$longSecondEnd = strpos($this->p_strOutputContents, "{{/unpaired $strPairName }}");
				
				if (($longFirstEnd > 0))
					{	$longFirstLength = $longFirstEnd - $longFirstStart + strlen("{{/paired $strPairName }}");
						$longSecondLength = $longSecondEnd - $longSecondStart + strlen("{{/unpaired $strPairName }}");
						$strFirstPairContents = substr($this->p_strOutputContents, $longFirstStart, $longFirstLength);
						$strSecondPairContents = substr($this->p_strOutputContents, $longSecondStart, $longSecondLength);
						$this->p_arrPaired[$strPairName][0] = trim($strFirstPairContents);
						if ($boolUseBoth)
							{	$this->p_arrPaired[$strPairName][1] = trim($strSecondPairContents);
							}
						return $this->p_arrPaired[$strPairName][0];
					};
					
			}

		function m_AssignPair($strPairName, $strFirstReplace, $strSecondReplace="")
			{	$strFirstPair = $this->p_arrPaired[$strPairName][0];
				
				if (!empty($strFirstPair))
					{	$this->p_strOutputContents = str_replace($strFirstPair, $strFirstReplace . "\r\n" . $strFirstPair, $this->p_strOutputContents);
						
						if (!empty($strSecondReplace))
							{	$strSecondPair = $this->p_arrPaired[$strPairName][1];
								$this->p_strOutputContents = str_replace($strSecondPair, $strSecondReplace . "\r\n" . $strSecondPair, $this->p_strOutputContents);
							};
							
					};
					
			}

		function m_AddElements($strPageTitle)
		{	$this->m_LoadFile("templates/navigation.tpl","NAVIGATION");
			$this->m_LoadFile("templates/footer.tpl","FOOTER");
			$this->m_AssignContent("TITLE","DWF - " . $strPageTitle);
		}
		
		function m_PrepTemplate($boolDeleteSection = true)
			{	
				if ($boolDeleteSection)
					{	$arrSections = $this->p_arrSections;
						
						while (list($strSectionName, $strSectionContent) = each($arrSections))
							{	$this->p_strOutputContents = str_replace(trim($strSectionContent), "", $this->p_strOutputContents);
								$this->p_strOutputContents = str_replace("{{section " . $strSectionName . " }}", "", $this->p_strOutputContents);
								$this->p_strOutputContents = str_replace("{{/section " . $strSectionName . " }}", "", $this->p_strOutputContents);
							};
							
						$arrPair = $this->p_arrPaired;
						
						while (list($strPairName) = each($arrPair))
							{	
								while (list($intArrayVal, $strPairContent) = each($arrPair["$strPairName"]))
									{	$this->p_strOutputContents = str_replace(trim($strPairContent), "", $this->p_strOutputContents);
									};
								$this->p_strOutputContents = str_replace("{{paired " . $strPairName . " }}", "", $this->p_strOutputContents);
								$this->p_strOutputContents = str_replace("{{/paired " . $strPairName . " }}", "", $this->p_strOutputContents);
								$this->p_strOutputContents = str_replace("{{unpaired " . $strPairName . " }}", "", $this->p_strOutputContents);
								$this->p_strOutputContents = str_replace("{{/unpaired " . $strPairName . " }}", "", $this->p_strOutputContents);
							};
							
					};
					
				return $this->p_strOutputContents;
			}
		
		function m_Output()
		{	echo $this->p_strOutputContents;
		}
	}
?>
Sorry its a little long but I have spent 2 weeks on this and I need help. Its for a freind.


Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

grrr... please use php tags when posting.
yechi2001
Forum Newbie
Posts: 10
Joined: Sun Apr 23, 2006 8:34 pm

Post by yechi2001 »

I read about that but I didnt understand whet that meant. Doesnt it men to use <?php at the begining.?
yechi2001
Forum Newbie
Posts: 10
Joined: Sun Apr 23, 2006 8:34 pm

Post by yechi2001 »

I think I got it now

Code: Select all

PHP Notice: Undefined variable: Page in C:\Inetpub\wwwroot\chapter8\main.php on line 5 PHP Warning: fopen(templates/.tpl) [function.fopen]: failed to open stream: No such file or directory in C:\Inetpub\wwwroot\chapter8\global\i_template.php on line 9 PHP Warning: filesize() [function.filesize]: stat failed for templates/.tpl in C:\Inetpub\wwwroot\chapter8\global\i_template.php on line 10 PHP Warning: fread(): supplied argument is not a valid stream resource in C:\Inetpub\wwwroot\chapter8\global\i_template.php on line 10 PHP Warning: fclose(): supplied argument is not a valid stream resource in C:\Inetpub\wwwroot\chapter8\global\i_template.php on line 11 PHP Notice: Undefined variable: Page in C:\Inetpub\wwwroot\chapter8\main.php on line 8 PHP Notice: Undefined variable: sessErrors in C:\Inetpub\wwwroot\chapter8\main.php on line 11
here is the main.php file

Code: Select all

<?php 
include_once("global/i_template.php"); 
session_start(); 

$strFileName = "templates/" . strtolower($Page) . ".tpl"; 
$objTemplate = new o_Template; 
$objTemplate->m_LoadFile($strFileName); 
$objTemplate->m_AddElements($Page); 

if ($sessErrors) 
{ $objTemplate->m_AssignContent("ERRORS", $sessErrors); 
} 
else 
{ $objTemplate->m_AssignContent("ERRORS", ""); 
}; 

session_unregister("sessErrors"); 
$objTemplate->m_Output(); 
?>
And here is the i_template.php which is supposed to work but doesnt i think.

Code: Select all

<?php 
class o_Template 
{ 
var $p_strOutputContents = ""; 
var $p_arrSections = array(); 
var $p_arrPaired = array(); 

function m_LoadFile($strFile, $strTagName = "") 
{ $objFile = fopen($strFile, "r"); 
$strContents = fread($objFile, filesize($strFile)); 
fclose($objFile); 

if (empty($strTagName)) 
{ $this->p_strOutputContents = $strContents; 
} 
else 
{ $this->p_strOutputContents = str_replace("{{" . $strTagName . "}}", $strContents, $this->p_strOutputContents); 
}; 

} 

function m_AssignContent($strTagName, $strReplace, $strContent="") 
{ 
if (empty($strContent)) 
{ $this->p_strOutputContents = str_replace("{{" . $strTagName . "}}", $strReplace, $this->p_strOutputContents); 
} 
else 
{ return str_replace("{{" . $strTagName . "}}", $strReplace, $strContent); 
}; 
} 

function m_DefineSection($strSectionName) 
{ $longSectionStart = strpos($this->p_strOutputContents, "{{section $strSectionName }}"); 
$longSectionEnd = strpos($this->p_strOutputContents, "{{/section $strSectionName }}"); 

if (($longSectionEnd > 0)) 
{ $longSectionLength = $longSectionEnd - $longSectionStart + strlen("{{/section $strSectionName }}"); 
$strSection = substr($this->p_strOutputContents, $longSectionStart, $longSectionLength); 
$this->p_arrSections[$strSectionName] = trim($strSection); 
return $this->p_arrSections[$strSectionName]; 
}; 

} 

function m_AssignSection($strSectionName, $strReplace, $strContent="") 
{ $strSection = trim($this->p_arrSections[$strSectionName]); 

if (empty($strContent)) 
{ 
if (!empty($strSection)) 
{ $this->p_strOutputContents = str_replace(trim($strSection), $strReplace . "\r\n" . $strSection, $this->p_strOutputContents); 
}; 

} 
else 
{ 
if (!empty($strSection)) 
{ return str_replace($strSection, $strReplace . "\r\n" . $strSection, $strContent); 
}; 

}; 

} 

function m_DefinePair($strPairName, $boolUseBoth=false) 
{ $longFirstStart = strpos($this->p_strOutputContents, "{{paired $strPairName }}"); 
$longFirstEnd = strpos($this->p_strOutputContents, "{{/paired $strPairName }}"); 
$longSecondStart = strpos($this->p_strOutputContents, "{{unpaired $strPairName }}"); 
$longSecondEnd = strpos($this->p_strOutputContents, "{{/unpaired $strPairName }}"); 

if (($longFirstEnd > 0)) 
{ $longFirstLength = $longFirstEnd - $longFirstStart + strlen("{{/paired $strPairName }}"); 
$longSecondLength = $longSecondEnd - $longSecondStart + strlen("{{/unpaired $strPairName }}"); 
$strFirstPairContents = substr($this->p_strOutputContents, $longFirstStart, $longFirstLength); 
$strSecondPairContents = substr($this->p_strOutputContents, $longSecondStart, $longSecondLength); 
$this->p_arrPaired[$strPairName][0] = trim($strFirstPairContents); 
if ($boolUseBoth) 
{ $this->p_arrPaired[$strPairName][1] = trim($strSecondPairContents); 
} 
return $this->p_arrPaired[$strPairName][0]; 
}; 

} 

function m_AssignPair($strPairName, $strFirstReplace, $strSecondReplace="") 
{ $strFirstPair = $this->p_arrPaired[$strPairName][0]; 

if (!empty($strFirstPair)) 
{ $this->p_strOutputContents = str_replace($strFirstPair, $strFirstReplace . "\r\n" . $strFirstPair, $this->p_strOutputContents); 

if (!empty($strSecondReplace)) 
{ $strSecondPair = $this->p_arrPaired[$strPairName][1]; 
$this->p_strOutputContents = str_replace($strSecondPair, $strSecondReplace . "\r\n" . $strSecondPair, $this->p_strOutputContents); 
}; 

}; 

} 

function m_AddElements($strPageTitle) 
{ $this->m_LoadFile("templates/navigation.tpl","NAVIGATION"); 
$this->m_LoadFile("templates/footer.tpl","FOOTER"); 
$this->m_AssignContent("TITLE","DWF - " . $strPageTitle); 
} 

function m_PrepTemplate($boolDeleteSection = true) 
{ 
if ($boolDeleteSection) 
{ $arrSections = $this->p_arrSections; 

while (list($strSectionName, $strSectionContent) = each($arrSections)) 
{ $this->p_strOutputContents = str_replace(trim($strSectionContent), "", $this->p_strOutputContents); 
$this->p_strOutputContents = str_replace("{{section " . $strSectionName . " }}", "", $this->p_strOutputContents); 
$this->p_strOutputContents = str_replace("{{/section " . $strSectionName . " }}", "", $this->p_strOutputContents); 
}; 

$arrPair = $this->p_arrPaired; 

while (list($strPairName) = each($arrPair)) 
{ 
while (list($intArrayVal, $strPairContent) = each($arrPair["$strPairName"])) 
{ $this->p_strOutputContents = str_replace(trim($strPairContent), "", $this->p_strOutputContents); 
}; 
$this->p_strOutputContents = str_replace("{{paired " . $strPairName . " }}", "", $this->p_strOutputContents); 
$this->p_strOutputContents = str_replace("{{/paired " . $strPairName . " }}", "", $this->p_strOutputContents); 
$this->p_strOutputContents = str_replace("{{unpaired " . $strPairName . " }}", "", $this->p_strOutputContents); 
$this->p_strOutputContents = str_replace("{{/unpaired " . $strPairName . " }}", "", $this->p_strOutputContents); 
}; 

}; 

return $this->p_strOutputContents; 
} 

function m_Output() 
{ echo $this->p_strOutputContents; 
} 
} 
?>
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

where does $Page come from?
yechi2001
Forum Newbie
Posts: 10
Joined: Sun Apr 23, 2006 8:34 pm

Post by yechi2001 »

$Page is what is appended to the URl. for example http://localhost/chapter8/main.php?Page=login

login being the .tpl file.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

then you need to use $_GET['Page']; not $Page. Read about register_globals. They are a big no-no in the php world (security issue). I'll let you read.
yechi2001
Forum Newbie
Posts: 10
Joined: Sun Apr 23, 2006 8:34 pm

Post by yechi2001 »

Now I get this error

Code: Select all

PHP Warning: include_once(global/i_template.php) [function.include-once]: failed to open stream: Operation not permitted in C:\Inetpub\wwwroot\chapter8\main.php on line 2 PHP Warning: include_once() [function.include]: Failed opening 'global/i_template.php' for inclusion (include_path='.;C:\php5\pear') in C:\Inetpub\wwwroot\chapter8\main.php on line 2 PHP Fatal error: Class 'o_Template' not found in C:\Inetpub\wwwroot\chapter8\main.php on line 6
I made these changes

Code: Select all

<?php
include_once("global/i_template.php");
session_start();

$strFileName = "templates/" . strtolower ($_GET['Page']) . ".tpl";
$objTemplate = new o_Template;
$objTemplate->m_LoadFile($strFileName);
$objTemplate->m_AddElements ($_GET['Page']);

if ($sessErrors)
	{	$objTemplate->m_AssignContent("ERRORS", $sessErrors);
	}
else
	{	$objTemplate->m_AssignContent("ERRORS", "");
	};

session_unregister("sessErrors");
$objTemplate->m_Output();
?>
what else do i need to do please.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Operation not permitted in C:\Inetpub\wwwroot\chapter8\main.php on line 2
Sounds strangely like a permissions problem...
yechi2001
Forum Newbie
Posts: 10
Joined: Sun Apr 23, 2006 8:34 pm

Post by yechi2001 »

how does permissions work. is it an IIS issue or PHP issue. what do you suggest.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I believe it will be an IIS issue with the IUSR's permissions on that folder. But I could be wrong.
yechi2001
Forum Newbie
Posts: 10
Joined: Sun Apr 23, 2006 8:34 pm

Post by yechi2001 »

I have all permissions set to execute read write and browse. what else can i do.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You know what, Burrito had a problem similar to this about a month and a half ago. Try searching these boards for author 'Burrito' and search terms 'IUSR'. It might turn up something.
yechi2001
Forum Newbie
Posts: 10
Joined: Sun Apr 23, 2006 8:34 pm

Post by yechi2001 »

I reaserched burrito but it brought up nothing. Any suggestions or resources that a NewBie like me could read and comprehend. All the stuff ive founf I just dont understand.

Thanks
Post Reply