Jump to content

Basic Question Regarding PHP Includes


Ricky55

Recommended Posts

Hi

 

Just starting out with PHP.

 

Working on a site that uses some basic includes, these are working fine for all my files in the root of my site as the links from the header include is just linking straight to pages for example contactUs.php

 

I am now however having to create some new folders to hold some product ranges, these are two folders down from the root of the site.  I can link to the includes fine but the links don't work, to work they would need ../../ etc

 

Now I could create a new header include file especially for use by the files in the deeper folders or I could make the links absolute but I just wondered if there was another way?

 

I wondered if you could use PHP to perhaps detect the folder level and then add the necessary path???

 

I don't know am I way off the mark?

 

Any help / tips much appreciated.

Link to comment
Share on other sites

Yeah you could probably make PHP workout how many directory levels in you are and prepend a string of ../ onto a include's filename. Check out this user's example from the php manual's user comments under function include(): http://www.php.net/manual/en/function.include.php#76092

What a pain! I have struggled with including files from various subdirectories.  My server doesn't support an easy way to get to the root HTML directory so this is what I came up with:

<?php

$times = substr_count($_SERVER['PHP_SELF'],"/");
$rootaccess = "";
$i = 1;

while ($i < $times) {
$rootaccess .= "../";
$i++;
}
include ($rootaccess."foo/bar.php");

?>

This will give you what it takes to get to the root directory, regardless of how many subdirectories you have traveled  through.

Link to comment
Share on other sites

My prefered method is to make a config file

<?php
$path='/home/path/public_html';
$path_includes = "$path/includes";
$url = 'http://MyDomain.com';
$url_images = "$url/images";
?>

 

Then include the config file into all your pages. I usually hard code it with something like this

if (!file_exists("c:/home/singlechef/includes/config.php"))
{include('/home/singlechef/htdocs/includes/config.php');}
else {include('c:/home/singlechef/includes/config.php');}

I do it this way so I can build and test on my development computer at home then upload to the server without having to alter code for the config file(s). But you can just use an include with an absolute path if you wish.

include('/home/user/public_html/includes/config.php');

 

The you just use the path/url vars and you have absolute values and never have problems no matter where the file are or are called from.

 

 

HTH

Teamatomic

Link to comment
Share on other sites

Thanks for all your replies.

 

I think I probably would just use absolute paths but they make local testing a bit of a pain and I'm also uploading the site to a different server while I build the site for the client to approve all the links would be broken until its on the correct server.

 

I'll check them out and let you know how I get on.

 

Thanks

Link to comment
Share on other sites

Thanks for all your replies.

 

I think I probably would just use absolute paths but they make local testing a bit of a pain and I'm also uploading the site to a different server while I build the site for the client to approve all the links would be broken until its on the correct server.

 

I'll check them out and let you know how I get on.

 

Thanks

 

Just remember, if you're using absolute paths (ie: http: //yoursite.com/includes/a.php) you will lose all included functions and variables, as it will pull the results (from an http request) rather than actually including as intended. You can simply list the document path first, in place of an absolute path (which is more preferred) like so:

$docroot = getenv("DOCUMENT_ROOT");
include($docroot.'includes/a.php');

 

which will pull something such as /usr/var/name/ht_docs/includes/a.php.

Link to comment
Share on other sites

The issue in this thread are the links (URLs) in the included file.

 

You can use $_SERVER['HTTP_HOST'] when forming the absolute URLs to get the host name so that you don't need to change anything when you move from your development system and a live server.

Link to comment
Share on other sites

Pleased you said that I was getting a bit confused there myself.

 

Yes just to recap its not linking to the includes, I can do that no bother at all as each page can just link to the include  however it needs to.

 

Its the links inside the include file that I'm referring to.

 

They are all currently looking for root based files for example contact.php

 

So when I use this include in a file at the root level all is well but when I use the file several folders in the links obviously no longer work.  For them to work they'd need to have ../../ in front of them but I can do this as this will break the links for the files that us the include at the root level.

 

So my use of absolute paths or URLS would be on the links inside the include.

 

I know some of these answers where looking at this problem.

 

Thanks for everyone who has responded its good to be on such a lively forum.

 

So on balance then what is the preferred method of getting around this problem?

 

Thanks

 

 

 

Link to comment
Share on other sites

PFMaBiSmAd, just read your post back again, that sounds ideal so I can use absolute urls but not have to change them.

 

How would I use this then? in my link?

 

Sorry my knowledge of PHP is very small at the moment.

 

I was going to mention this but forgot. Anyway, Like this:

$host = $_SERVER['HTTP_HOST'];
echo "http://$host/folder/test.html"; //http://127.0.0.1/folder/test.html or http://site.com/folder/test.html

 

That will display the correct host, in development or live environment.

 

For your reference I'd look at this:

http://www.php.net/manual/en/reserved.variables.server.php

 

It will list all the $_SERVER variables which may become handy to you over time.

Link to comment
Share on other sites

Thanks very much.

 

I understand that code but would that be used on every link in my include file?

 

For example currently my links look like links...

 

<a href="">contactUs.php</a>

 

Could I declare $host = $_SERVER['HTTP_HOST']; that once for instance and then use the echo on my links?

Link to comment
Share on other sites

Sorry my links don't look like that at all  they look like this

 

<a href="contactUs.php">contact Us</a>

 

That is what you'd call a relative URI. You would need to do nothing, as it simple includes it from the path. Such as './contactUs.php' , it doesn't need a host name.

Link to comment
Share on other sites

Sorry I'm getting a bit confused, if I use that relative link though they won't work when I use the include file in files that two folders down from the root.

 

So i do need to turn my links into absolute paths but this will break on my test server as its not the final host so how would I use your example in this?

 

<a href="htttp://www.mysite/includes/contact.php">contact.php</a>

 

Sorry if I being a bit thick!

Link to comment
Share on other sites

So i do need to turn my links into absolute paths but this will break on my test server as its not the final host so how would I use your example in this?

 

Use $_SERVER['HTTP_HOST'] instead of hardcoding your host (domain) name.

Link to comment
Share on other sites

Sorry to be a pain Thorpe.

 

My link looks like this

 

<a href="http://www.choicecreative.co.uk/welcome/index.php">Home</a>

 

How would it look if I replaced the domain with that code, I tried a few options and it didn't seem to work.

 

Also, do I still need the initial part of

 

$host = $_SERVER['HTTP_HOST'];

 

And if so where does that need to be located as the include file although saved with the PHP extension is just HTML code.

Link to comment
Share on other sites

Sorry to push this up again, I still can't get this working,

 

I've tried $_SERVER[HTTP_HOST] instead of hard coding my domain name in my links but this still remains in the address bar of the browser so its not getting parsed as PHP.

 

Besides changing the links inside my include file what else do I need to do?

 

<a href="http://$_SERVER[HTTP_HOST]/index.php">Home</a>

 

Also my include file is saved with the extension of .PHP but inside there are no PHP tags its just regular HTML is this correct or should I be using the <?PHP tags?

 

Thanks

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.