Forum Moderators: coopster

Message Too Old, No Replies

Absolute URL's in PHP Includes?

         

Asia_Expat

9:22 pm on Apr 27, 2007 (gmt 0)

10+ Year Member



I have two related questions.

1) I use common includes ( <?php require("divleft.html");?> ) but I want to create one that I can change site wide instead of on a directory basis but when I try to make one with the full path, I get errors. How can I do this?

2) On each page, I have a PHP module that adds a PHP generates RSS feed to each page. I want to include the code to invoke this in a PHP include... will having a PHP generated feed within a PHP include, like the one above, cause any problems?... here is what I want to add to the include...


<?php
require_once '/homepages/24/d143989976/htdocs/mydirectory/carp/carp.php';
CarpConf('iorder','link,');
CarpConf('bi','# ');
CarpConf('maxitems',10);
CarpConf('poweredby','<br /><i>Powered by <a href="h##p://www.examplecom">www.example.com</a></i>');
CarpCacheShow('h##p://www.example.com/forum/index.php?act=rssout&id=5');
?>

Thanks for your help and advice.

[edited by: Asia_Expat at 9:26 pm (utc) on April 27, 2007]

[edited by: dreamcatcher at 8:41 am (utc) on April 28, 2007]
[edit reason] Use example.com, thanks. [/edit]

Asia_Expat

10:04 pm on Apr 27, 2007 (gmt 0)

10+ Year Member



Regarding my first question, I got it to work by using the full path like this...

<?php require("/homepages/24/d1234567/htdocs/mydirectory/centre-content.php");?>

... but if I moved servers, it would be a nightmare to update every page for the new path... how would I get around this?

thecoalman

10:27 pm on Apr 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm pretty new to PHP myself but I've been using:

include($_SERVER['DOCUMENT_ROOT'] . '/somepublicdirectory/somefile.php');

Works on my local machine and the server.

Asia_Expat

12:49 am on Apr 28, 2007 (gmt 0)

10+ Year Member



I'm not sure that would work. I need to include files from various locations on the domain from various folders, some in the root directory (called on pages in deeper folders) and vice cersa.

g1smd

12:31 am on Apr 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In the server configuration you can usually set the default path for the base "include" folder.

Asia_Expat

2:31 am on Apr 29, 2007 (gmt 0)

10+ Year Member



I figured it out... it works perfectly as described above... now for a few days of copying and pasting into every page :( ... (teaches you to get it right the first time I guess).

I also changed from 'require' to 'include'... can anyone explain to me the advantage of using 'require' and the fact that it produces a fatal error? Why would you want that?

Also, any comments on my second question in the OP?

[edited by: Asia_Expat at 2:32 am (utc) on April 29, 2007]

eelixduppy

2:37 am on Apr 29, 2007 (gmt 0)



As with your second question, I do not think that it will cause any troubles for you.

>> can anyone explain to me the advantage of using 'require' and the fact that it produces a fatal error?

As you have mentioned, require produces a fatal error if there is a problem. This is useful is you don't want to continue the execution of the following script if the file was not included correctly. In the case of

include
, if the file wasn't successfully included a warning will be thrown, however the rest of the script will still execute.

Tastatura

2:41 am on Apr 29, 2007 (gmt 0)

10+ Year Member




require() and include() are identical in every way except how they handle failure. They both produce a Warning, but require() results in a Fatal Error. In other words, don't hesitate to use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include_path setting as well.

from php.net/require [php.net]

Asia_Expat

2:52 am on Apr 29, 2007 (gmt 0)

10+ Year Member



Yes, I read that but I still don't understand WHY you would want the processing of the page to stop.

[edited by: Asia_Expat at 2:53 am (utc) on April 29, 2007]

eelixduppy

3:05 am on Apr 29, 2007 (gmt 0)



Well, what would happen if the rest of the script required (hence the name :)) that include for it to work properly? Then you would get unexpected results and many errors. It would be much better to stop the execution before anything like that ever happens.

Asia_Expat

3:13 am on Apr 29, 2007 (gmt 0)

10+ Year Member



Hmmm... seems obvious when explained properly.

So, about my second question... Can I have includes within and include?... and can I safely call other PHP scripts/modules with this method, such as the carp rss feed output I described above?

[edited by: Asia_Expat at 3:13 am (utc) on April 29, 2007]

willybfriendly

3:36 am on Apr 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can I have includes within and include?

Yes.

I usually include and "include" file. That allows me to update all of my includes in one place.

<?php
/* includes.php lists all files needed for script (A misnomer, since this file is "required" */

require_once("config.php");
require_once("vars.php");
require_once("db-functions.php");
require_once("display-functions.php");

?>

etc.

By doing this I know that all the required scripts have been loaded up, and if they are not, everything comes to a screaching halt.

WBF

cameraman

4:57 am on Apr 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's also usually a good idea to use include_once or require_once as wbf just did, instead of include or require - that way if, in that example, vars.php included db-functions inside it, the second inclusion of db-functions won't cause any duplicate function errors or re-execution of common code that might not be desired.