Bob_Robertson Posted September 27, 2007 Share Posted September 27, 2007 Hey all. I've got a couple of scripts that aren't doing what I think they should do. I have a directory, and two subdirectories within it; call them "a" and "b". In the main directory, I have a password file containing access credentials to a database; call it "dbinfo.php". I am trying to use the contents of pass.php in scripts in directories a and b (a.php and b.php, say), and here the weirdness begins. a.php is a linear script that takes POST information, does some database lookup and processing, and outputs a GET-style text string. b.php, on the other hand, contains a class definition, with two include() statements in the constructor function. a.php works correctly, and I might even say well. The first include() statement in b.php works properly, and requests "../../../config.php". The second include() statement in b.php does not work properly, and requests "../dbinfo.php". E.g.: a.php: include("../flt_dbinfo.php"); $db = mysql_connect($flt_host,$flt_user,$flt_password) or die ("success=false&Reason=database connection failed"); mysql_select_db($flt_database); b.php: class service { function service() { <snip> include ("../../../config.php"); $this->mdl_host=$CFG->dbhost; $this->mdl_user=$CFG->dbuser; $this->mdl_password=$CFG->dbpass; $this->mdl_database=$CFG->dbname; echo "mdl DB creds:<br />"; echo $this->mdl_host; echo $this->mdl_user; echo $this->mdl_password; echo $this->mdl_database; include("../dbinfo.php"); $this->flt_host=$flt_host; $this->flt_user=$flt_user; $this->flt_password=$flt_password; $this->flt_database=$flt_database; echo "flt DB creds:<br />"; echo $flt_host; echo $this->flt_user; echo $this->flt_password; echo $this->flt_database; } <snip> } dbinfo.php: <?php $flt_host="localhost"; $flt_user="faultscript"; $flt_password=""; $flt_database="3631faults"; ?> A global named $CFG is defined in config.php. My plaintive question, then, is "What am I doing wrong?". I can access dbinfo.php (and echo its contents) from within a function in a test script placed in directory b. b.php can access an include file several directories higher successfully. I've copied and pasted the include() statement from a.php to b.php directly. I'm at a loss, and slowly going bald to boot. Any ideas? Many Thanks, Bob Robertson Quote Link to comment https://forums.phpfreaks.com/topic/70897-weird-include-issue/ Share on other sites More sharing options...
BlueSkyIS Posted September 27, 2007 Share Posted September 27, 2007 aren't a.php and b.php including different db files? include("../flt_dbinfo.php"); vs. include("../dbinfo.php"); Quote Link to comment https://forums.phpfreaks.com/topic/70897-weird-include-issue/#findComment-356411 Share on other sites More sharing options...
Bob_Robertson Posted September 27, 2007 Author Share Posted September 27, 2007 Bad anonymization on my part, sorry. "dbinfo.php" was a ferexample; "flt_dbinfo.php" is in the production code. -Bob Quote Link to comment https://forums.phpfreaks.com/topic/70897-weird-include-issue/#findComment-356413 Share on other sites More sharing options...
BlueSkyIS Posted September 27, 2007 Share Posted September 27, 2007 okay. are you seeing errors? maybe try require() instead of include() to see if you get either of the "file does not exist" or "permission denied" errors. i personally make a point to never use relative references for include() or require(), preferring to direct reference the files from $_SERVER['DOCUMENT_ROOT'], e.g., include($_SERVER['DOCUMENT_ROOT']."/includes/somefile.php"); probably not a problem here, just a tip. Quote Link to comment https://forums.phpfreaks.com/topic/70897-weird-include-issue/#findComment-356421 Share on other sites More sharing options...
cooldude832 Posted September 27, 2007 Share Posted September 27, 2007 actually what is becoming the standard (there really aren't standards to php, but there is some common practices) is to create as single file called paths.php or config.php or constants.php. This defines a bunch of constants paths/session,constants, etc so then when you have the rest of your includes it is like require_once(SCRIPTS."sqlconnection.php"); For anything that is a config file/connection, etc that is a run once deal (your function list) use the require_once(); nomenclature as this best represents what you are trying to do. A lot of issues arrise if you have includes that incldue the same file and they do the same thing Quote Link to comment https://forums.phpfreaks.com/topic/70897-weird-include-issue/#findComment-356423 Share on other sites More sharing options...
Bob_Robertson Posted September 27, 2007 Author Share Posted September 27, 2007 Hm. Strange. When I edit b.php to create a service instance, and access it with my browser, it works fine. I had to comment out some other includes to get it to work properly, but those shouldn't matter, should they? I don't see how including a couple other files would influence including a third file. I can confirm that require() does kill the script when asking for "flt_dbinfo.php", so it either can't access the file or thinks that the file doesn't exist. Also, a test script placed in directory b can correctly access "flt_dbinfo.php" with both include() and require(). The only difference that I can see is that one's in a class constructor, and the other isn't. -Bob Quote Link to comment https://forums.phpfreaks.com/topic/70897-weird-include-issue/#findComment-356430 Share on other sites More sharing options...
rarebit Posted September 27, 2007 Share Posted September 27, 2007 Can you draw use a directory map structure, e.g: . index.php a/ a.php b/ b.php c/ index.php etc??? Quote Link to comment https://forums.phpfreaks.com/topic/70897-weird-include-issue/#findComment-356431 Share on other sites More sharing options...
BlueSkyIS Posted September 27, 2007 Share Posted September 27, 2007 The only difference that I can see is that one's in a class constructor my understanding is that you can't include inside a class without using a function, but i would assume that the constructor meets the criteria for a function. but maybe not?? maybe try moving your constructor include()s into a new class function? now i'm reaching.... Quote Link to comment https://forums.phpfreaks.com/topic/70897-weird-include-issue/#findComment-356434 Share on other sites More sharing options...
rarebit Posted September 27, 2007 Share Posted September 27, 2007 I've just tested it, you can do includes from within class function, but not from elsewhere... Quote Link to comment https://forums.phpfreaks.com/topic/70897-weird-include-issue/#findComment-356440 Share on other sites More sharing options...
Bob_Robertson Posted September 27, 2007 Author Share Posted September 27, 2007 This mess is located about five directories down from the web root, but okay: . flt_dbinfo.php - file that I'm trying to access /a a.php - script with working include /b b.php - script with irritating include test.php - script with working include Tried adding a function to retrieve the variables, and called it from the constructor: function getFaultInfo(){ include("../flt_dbinfo.php"); $this->flt_host=$flt_host; $this->flt_user=$flt_user; $this->flt_password=$flt_password; $this->flt_database=$flt_database; echo "flt DB creds:<br />"; echo $flt_host; echo $this->flt_user; echo $this->flt_password; echo $this->flt_database; } No joy, though, the echoes don't output anything, implying that the variables aren't initialized. Quote Link to comment https://forums.phpfreaks.com/topic/70897-weird-include-issue/#findComment-356441 Share on other sites More sharing options...
rarebit Posted September 27, 2007 Share Posted September 27, 2007 Try echoing a literal ("hi") from the same place, just to make sure the function is being called properly! Quote Link to comment https://forums.phpfreaks.com/topic/70897-weird-include-issue/#findComment-356453 Share on other sites More sharing options...
Bob_Robertson Posted September 27, 2007 Author Share Posted September 27, 2007 echo "flt DB creds:<br />"; does that, doesn't it? I can see that appear. -Bob Quote Link to comment https://forums.phpfreaks.com/topic/70897-weird-include-issue/#findComment-356456 Share on other sites More sharing options...
rarebit Posted September 27, 2007 Share Posted September 27, 2007 ok, sorry only precursory glance... Is b.php being called directly or included by something else? Try variations on directory (trial and error with (../)) Sorry if you've tried these, or think you have. This is the nature of the beast, you could of tried something while trying something else, etc... It's annoying I know, especially if it's late or going on for too long, and when you know it should be straight forward. Usually my solution is a cup of tea, then eureka! Quote Link to comment https://forums.phpfreaks.com/topic/70897-weird-include-issue/#findComment-356464 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.