Help with server side includes in html

Need help with a PHP script issue then post away :-)

Help with server side includes in html

Postby phpnewbie » Sun Mar 15, 2009 9:25 pm

Hi everyone,

I have a question that has been eluding me for a few hours of frustrating trial and error.

I want to include a server side include in a PHP file that is html code which gets called form and original PHP file. Sound confusing well it is.
So what I have tried in the main php file is :
Code: Select all
include "../templates/template.php";


Then in the template.php file I have a regular html page that calls on a server side include but since that does not seem to work I switched it to a php include like so:
Code: Select all
<?php include ("/includes/head.html"); ?>


but when I go to run the original file all I get is a blank page and when I view source it shows:
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<META content=PFAUPCXLEO name=SKYPE_FRAMEID></HEAD>
<BODY></BODY></HTML>


so I don't know what is going wrong.

Any help would be greatly appreciated
phpnewbie
 
Posts: 2
Joined: Sun Mar 15, 2009 9:00 pm

Re: Help with server side includes in html

Postby Rice Rocket » Mon Mar 16, 2009 9:52 am

Hi there PHPnewbie,

Your code looks good to me. The only think I can think of is that your main file that calls the first include (second PHP file) is calling it from a different location than where the second PHP file is located.
To put it in simpler terms:

mainfile.php has an include to 2ndfile.php like so
Code: Select all
<?php include ("../templates/2ndfile.php"); ?>


so your mainfile.php is in a different folder since you have this "../" in front of your other PHP include. and this is where it is easy to go wrong because you have to make the next file call as if the 2ndfile.php is in the same folder as the mainfile.php. So what you would need to do is have the "2ndfile" call:

Code: Select all
<?php include "../templates/includes/head.html ?>

instead of
Code: Select all
<?php include ("/includes/head.html"); ?>


It is a common mistake people make because they want to check their coding as they make the script, upload that part and it works then they call it from the main function in the other script and it doesn't work.

Hope this helps and welcome to the fun world of PHP coding. :D
Rice Rocket
 
Posts: 3
Joined: Mon Mar 16, 2009 9:33 am

Re: Help with server side includes in html

Postby phpnewbie » Mon Mar 16, 2009 10:14 am

Hey thanks Rice Rocket! It works now :mrgreen:

Guess I still have a lot to learn but with the help of knowledgeable people like yourself I think it will make it a bit easier.
phpnewbie
 
Posts: 2
Joined: Sun Mar 15, 2009 9:00 pm


Return to PHP Help

Who is online

Users browsing this forum: No registered users and 0 guests

cron