Jump to content

Php Include


kfordham281
 Share

Recommended Posts

I'm new to this command sorry if this is a dumb question....I looked around a bit and didn't see an answer. :lol:

 

I'm trying to include my nav bar (navigation.php) as a php include on my website because the site is getting larger and to be able to change the nav bar only once will save me a lot of time. My problem is that I can't seen to get the include function to work on html files. I've even done a simple example and the php example worked fine but the html did not.

 

www.cpadventures.com/test/main.php - works fine and you can see the test

 

www.cpadventures.com/test/main.html - blank as the php include function isn't getting processsed. the files are exactly the same except for their extension.

 

So how do I get the php include function to work inside html files?

 

Thank You

Link to comment
Share on other sites

I've come across a different problem with php include. My nav bar is different for my website based on where in the site you are. The different files reflect the "../" that leads back to the webroot

 

cpa_navigation.php - anything in the root of my web directory

event_cpa_navigation.php - anything that is in a subfolder of the web root.

 

When trying to link to the event_cpa_navigation.php I get the following error:

 

Code below from: http://www.cpadventures.com/adventure24/directions2.htm

 

>Warning: main(../phpincludes/event_cpa_navigation.php): failed to open stream: No such file or directory in /home/<username>/public_html/adventure24/directions2.htm on line 37

Warning: main(../phpincludes/event_cpa_navigation.php): failed to open stream: No such file or directory in /home/<username>/public_html/adventure24/directions2.htm on line 37

Warning: main(../phpincludes/event_cpa_navigation.php): failed to open stream: No such file or directory in /home/<username>/public_html/adventure24/directions2.htm on line 37

Warning: main(): Failed opening '../phpincludes/event_cpa_navigation.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/<username>/public_html/adventure24/directions2.htm on line 37

 

When using the exact same include path; only changing the name from "event_cpa_navigation.php" to cpa_navigation.php it works fine.

 

Summary:

<?php include ("../phpincludes/event_cpa_navigation.php"); ?> - Does not work

<?php include ("../phpincludes/cpa_navigation.php"); ?> - Does work

 

And yes, the event_cpa_navigation.php file IS in the location mentioned. Though it does not seem like it. Very strange!

Edited by kfordham281
Link to comment
Share on other sites

I don't know the answer (sorry) but you should edit your "code" section to remove your cpanel username... ;) Generally, you'll want to replace that with something like <UserName> or something. That way, we can still see the structure of the error message, but your cPanel username isn't compromised.

 

For example:

 

>Warning: main(../phpincludes/event_cpa_navigation.php): failed to open stream: No such file or directory in /home/<UserName>/public_html/adventure24/directions2.htm on line 37

Link to comment
Share on other sites

There are several ways to do what you want, but they're all basically the same: you have to have include instructions that are independent of where the file is in the site structure.

 

Most common:

 

@(include $_SERVER['DOCUMENT_ROOT'].'/dir/to/file.php') or die('include file one not found');

Link to comment
Share on other sites

I'm new to this command sorry if this is a dumb question....I looked around a bit and didn't see an answer. :)

 

I'm trying to include my nav bar (navigation.php) as a php include on my website because the site is getting

 

go to www.jaysarahandy.com

 

I did my site in PHP and have a navbar

Link to comment
Share on other sites

you can do two ways:

 

1. issue include statement defining include directory each time:

<?php include $_SERVER['DOCUMENT_ROOT']."/phpincludes/event_cpa_navigation.php" ; ?>

 

2. what happens if you decide to change servers, portability is an issue, you will have to go to wherever your include statement are at and change each one. If you have a bunch of pages you will be busy a while. Better method is to set a an includes path so if you change servers you just reset the include path once. you would need to:

  1. add .inc to the file name just so you know it is an include file (this is optional)
  2. place all your .inc include files in a library directory,
  3. set the path to this library directory as below:
    include_path .:/home/user/cpanelid/htdocs/cpadventures/lib/
  4. and then call the specific include file as below:
    <?php include("event_cpa_navigation.inc.php") ; ?>

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...