The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP-General - Help with my php include function.
Discuss Help with my php include function. in the PHP Development forum on Dev Shed. Help with my php include function. PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 30th, 2012, 05:52 AM
|
|
Contributing User
|
|
Join Date: Apr 2012
Posts: 48
Time spent in forums: 10 h 2 m 36 sec
Reputation Power: 2
|
|
|
PHP-General - Help with my php include function.
hi everyone
i really just want some ideas from everyone as to why my PHP include function is not working.
i am trying to include a page into the home page of a site.
the relevant page, for inclusion, is held in another folder on the same site. it held in the following folder :
/cms/
so, from the home page i did the following include:
PHP Code:
include('/cms/db_fns.php');
this should have linked to the relevant page but it does not work .
when i however did the following it worked;
PHP Code:
include('cms/db_fns.php');
so, i am unclear where i have gone wrong.
i need to have an absolute root so that i can use the relevant "includes" on any pages on the site, irrespective of which folder they are in.
ideally, i would like to use a web root
i.e
PHP Code:
include('http://www.thesite.com/cms/db_fns.php');
is there anyway to do this on a site where u cannot get access to the apache server. i.e hosting companies do not allow u access to the apache server to switch it off and on again.
can someone please advise me where i have gone wrong. thank you so much.
warm regards
Andreea
|

November 30th, 2012, 06:58 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Hi,
please read the documentation on the include statement (it's not a function, so you don't need the parentheses).
Any path you specify for "include" refers to the file system, not the document root or something. So "/cms" would be the "cms" folder in the root directory of the server (which of course makes no sense).
What you're trying to do requires a custom php.ini with the include_path set to your "cms" directory. Relative paths will then use this directory.
However, I'm not sure if this is a good idea. I'd simply specify paths relative to the current script's directory.
|

November 30th, 2012, 07:15 AM
|
|
Contributing User
|
|
Join Date: Apr 2012
Posts: 48
Time spent in forums: 10 h 2 m 36 sec
Reputation Power: 2
|
|
Quote: | Originally Posted by Jacques1 Hi,
please read the documentation on the include statement (it's not a function, so you don't need the parentheses).
Any path you specify for "include" refers to the file system, not the document root or something. So "/cms" would be the "cms" folder in the root directory of the server (which of course makes no sense).
What you're trying to do requires a custom php.ini with the include_path set to your "cms" directory. Relative paths will then use this directory.
However, I'm not sure if this is a good idea. I'd simply specify paths relative to the current script's directory. |
HI again everyone and special thanks to Jacques1 for his response.
i am a little bit confused by the answer though.
are you saying that i am not suppose to use the slash symbol i.e /
or i am not suppose to use the " "
is it possible for you or someone to give me an example of how the include should be. .
i looked at the "include documentation" but its not clear about the syntax for includes where the file is in another folder from the actual page.
warm regards
Andreea
|

November 30th, 2012, 07:36 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Quote: | Originally Posted by andreea115 i looked at the "include documentation" but its not clear about the syntax for includes where the file is in another folder from the actual page. |
This has nothing to do with a special "syntax" or something. You have to understand the difference between web paths and file system paths.
The "include" statements operates on the file system. So when you are on a Windows machine, it would take paths like "C:\\htdocs\\cms\\index.php". On a Linux machine, it would take paths like "/var/www/cms/index.php". We're talking about actual, physical paths of the operating system here.
This is completely different from web paths like "/cms/index.php" in the URL "http://www.mycoolsite.com/cms/index.php". Those are "virtual" paths that are resolved relative to the webserver's document root and don't even have to actually exist.
Again: "include" uses the file system. So when you specify the path "/cms", PHP will actually look in the root directory of your Linux/Windows installation. And I'm pretty sure there is no "cms" folder.
|

November 30th, 2012, 07:55 AM
|
|
Contributing User
|
|
Join Date: Apr 2012
Posts: 48
Time spent in forums: 10 h 2 m 36 sec
Reputation Power: 2
|
|
Quote: | Originally Posted by Jacques1 This has nothing to do with a special "syntax" or something. You have to understand the difference between web paths and file system paths.
The "include" statements operates on the file system. So when you are on a Windows machine, it would take paths like "C:\\htdocs\\cms\\index.php". On a Linux machine, it would take paths like "/var/www/cms/index.php". We're talking about actual, physical paths of the operating system here.
This is completely different from web paths like "/cms/index.php" in the URL "http://www.mycoolsite.com/cms/index.php". Those are "virtual" paths that are resolved relative to the webserver's document root and don't even have to actually exist.
Again: "include" uses the file system. So when you specify the path "/cms", PHP will actually look in the root directory of your Linux/Windows installation. And I'm pretty sure there is no "cms" folder. |
thank you. that is great. i am starting to understand. but i am still not clear what i am suppose to do now.
is it possible to give an example of how i can write the path?
regards
Andreea
|

November 30th, 2012, 08:16 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Please don't make a full quote of every reply. The text is right above your post, so no need to repeat it.
Quote: | Originally Posted by andreea115 is it possible to give an example of how i can write the path? |
I said that in my first reply. If you put your "cms" directory into the include_path (read the documentation), you can refer to it with relative paths. For example, if you say
PHP Code:
include 'scripts/db.php';
then "scripts/db.php" will be looked up in your "cms" directory (among others).
If you have problems understanding how paths in general work, google for "file path".
|

December 1st, 2012, 05:29 PM
|
 |
Lost in code
|
|
|
|
Most PHP software handles this by defining a constant that references the root of the application. For example, something like:
PHP Code:
define('ROOT', '/var/www/html/');
Then throughout the application whenever they need to include something, they use the ROOT constant to do it:
PHP Code:
include(ROOT . 'libraries/includeme.php');
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|