Question:
require php file once for include?
paul young
2009-03-08 12:10:40 UTC
Hi all,

i have 2 global.php files that conflict.

so this is what i want to do.

on the index.php file i include header.php now in that header file i want to include global1.php just for that include file, and not the rest of the page.

and for the rest of the page i want to include global2.php without it getting used on header include file.

can this be done?



can someone give me the script for doing it.

Thanks

- Paul
Three answers:
Mike S
2009-03-09 00:30:04 UTC
Your request is a little confusing, but it sounds like a situation of variables, rather than output of HTML code.



To make this issue a little clearer: Include files are processed in the order they are read. Any output produced in the first includes (header.php, and global1.php) will *not* be duplicated in the rest of the page. And output from global2.php will not get output in the processing of the header include.



Likewise, variables that get set in global2.php will not *generally* be available in global1.php or header.php. I say *generally* because variables _would_ be available if there is a loop executing between the two global includes. But it didn't sound like you're looping between these two includes. As you can see, this can get confusing really quickly.



Does global1.php set variables you don't want in the rest of the page? If that's why you don't want it in the rest of the page, then you should unset the variables at the end of header.php. Format:

unset( $variableName );



If you're just wanting to ensure that global1.php only *loads* once, use the command: include_once("global1.php");



If this doesn't answer your question, could you post a little about what you're doing in the header and global include files? That would remove all the doubt and I could give a more concise answer.
TallPaul
2009-03-08 15:40:16 UTC
I don't know any way of doing it in one Apache request but assuming that PHP is configured to allow you to open URLs as files (i.e. allow_url_fopen is true) then you could replace



include("header.php");



with



readfile("http://xyz.com/header.php");



(where xyz.com is your web site). That will make header.php be run as a separate Apache request so when it includes global1.php that won't affect index.php as that will only get the resulting HTML output.



It's a bit of a cludge, not least as you're doubling the work the web server has to do in that it now has two requests to process for every hit on index.php but it should solve your problem.
?
2016-10-15 08:50:17 UTC
attempt making use of a distinctive browser to work out if the subject nevertheless exists. If the different pages that use that function are working, then the server and browser have not cached the function, yet there's a controversy on the internet site in touch, probably a be conscious misspelled. in case you're making use of hostgator, basically wait approximately 10 minutes and examine it returned. terrific needs.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...