Simple PHP Include Problem

  • altom55
  • Novice
  • Novice
  • altom55
  • Posts: 24
  • Loc: UK

Post 3+ Months Ago

Im new to PHP.
The last few days Ive spent trying to get a php include to work on the index page of my site.

I have a folder with a file that Im trying to call but it does not show up. My hosting support cannot help me with code, so Ill ask here.

Ive tried it with all sorts of dots and forward slashes combinations but the php include in the footer of my index page at designerrugs dot co simply does not work. My site is an HTML site, maybe thats a problem, but I was told this is perfectly OK.

When I look at the source code, the code shows in red and quotes something about using an xml file in a html file. I dont understand any of this.

I think this is a correct example of the right path for an index page - I think
<?php include("./adirectory/afile.php") ?>

Any sort of help would be much appreciated
Regards Alan
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Poly
  • Guru
  • Guru
  • User avatar
  • Posts: 1091
  • Loc: Same place you left me.

Post 3+ Months Ago

You will need to change the extension of your index page from index.html to index.php. Be sure to remove or re-name the old file. This will not change anything else with your site or layout. Once you have done that, the include will work. In order for your web server to understand the <?php include(''); ?> command, you need to tell it that the file it is loading contains PHP code.

So:
Step 1: Rename index.html to index.php
Step 2: Add your <?php include(''); ?> at the bottom of the page.

As far as the dots and slashes, if the file you are including is in the same directory as your index file, you do not need to have any dots or slashes.
Ex:
Code: [ Select ]
<?php include ('test2.php'); ?>


If the file is in a sub directory of the index files directory, you will need a slash between the directory name and the file name. No leading slash and no trailing slash.
Ex:
Code: [ Select ]
<?php include ('testdir/test2.php'); ?>


If the file is in a directory above the directory containing your index file, you will need a slash and two dots.
Ex:
Code: [ Select ]
<?php include ('../test2.php'); ?>


Hope this helps!
  • altom55
  • Novice
  • Novice
  • altom55
  • Posts: 24
  • Loc: UK

Post 3+ Months Ago

Thanks for the reply.

I dont really want to change the extension of my index page, because I want to eventually have includes on every page of the site - that would mean changing the entire site to different file extensions.

Im sure this will cause major problems. I think there is a way of treating all html pages as php in the .htacess or apache but this is beyond me'

I was told that I could have php includes enabled on html pages by configuring a certain file in Cpanel, but I dont know how to

Alan...........
  • Poly
  • Guru
  • Guru
  • User avatar
  • Posts: 1091
  • Loc: Same place you left me.

Post 3+ Months Ago

I'm not sure how to do it via cPanel, but changing the .htaccess page to parse HTML files as PHP files is quite simple. You can contact your host to have them assist you with this, but you will only need to add one line to the .htaccess file:

Code: [ Select ]
AddType application/x-httpd-php .php .html


That tells Apache to treat all .html files as .php files.
  • altom55
  • Novice
  • Novice
  • altom55
  • Posts: 24
  • Loc: UK

Post 3+ Months Ago

Thanks for your help Poly
  • Poly
  • Guru
  • Guru
  • User avatar
  • Posts: 1091
  • Loc: Same place you left me.

Post 3+ Months Ago

Not a problem, good luck. Be sure to stop by to have your site reviewed once your done, most people get some really good feedback from that.
  • Zealous
  • Guru
  • Guru
  • User avatar
  • Posts: 1244
  • Loc: Sydney

Post 3+ Months Ago

Poly wrote:
Not a problem, good luck. Be sure to stop by to have your site reviewed once your done, most people get some really good feedback from that.


agreed, a few effective idea's thrown in could make a huge difference.
  • Zealous
  • Guru
  • Guru
  • User avatar
  • Posts: 1244
  • Loc: Sydney

Post 3+ Months Ago

Poly wrote:
Step 2: Add your <?php include(''); ?> at the bottom of the page.


don't we include at the top of the page? :)
  • altom55
  • Novice
  • Novice
  • altom55
  • Posts: 24
  • Loc: UK

Post 3+ Months Ago

Poly wrote:
I'm not sure how to do it via cPanel, but changing the .htaccess page to parse HTML files as PHP files is quite simple. You can contact your host to have them assist you with this, but you will only need to add one line to the .htaccess file:

Code: [ Select ]
AddType application/x-httpd-php .php .html


That tells Apache to treat all .html files as .php files.



This is the problem because my web host refuse to help me with anything that relates to "code"
  • Poly
  • Guru
  • Guru
  • User avatar
  • Posts: 1091
  • Loc: Same place you left me.

Post 3+ Months Ago

Hi altom, to modify the .htaccess file yourself, use either the FTP program in cPanel/WHM or use your own. Connect to your site, navigate to the root site directory(Ex: /home/username/public_html/). Find the file .htaccess in that directory. If one does not exist you will need to create one. Use either cPanel's built in file editor, or save it to your computer. Open it, add that line at the bottom. Save the file, re-upload it(unless you use the built in editor, in which case save and close it). That should be all you need to do.
  • altom55
  • Novice
  • Novice
  • altom55
  • Posts: 24
  • Loc: UK

Post 3+ Months Ago

Thanks Poly! Ive managed to sort it all out now by taking your advice ie - "AddType application/x-httpd-php .php .html" in the .htaccess file.

Im really pleased with the result as it makes 'menu keeping' easy from just 1 file. I wish I knew about the power of PHP before I started building my sites in traditional HTML/CSS.

PS if this code does not show - Can you tell me how to include code in posts on this forum...

Regards....
  • Poly
  • Guru
  • Guru
  • User avatar
  • Posts: 1091
  • Loc: Same place you left me.

Post 3+ Months Ago

Hi Altom,

To include code in a forum post use the code brackets like so: [ code][ /code], removing the space inside the brackets.

Post Information

  • Total Posts in this topic: 12 posts
  • Users browsing this forum: No registered users and 86 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.