Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/70897-weird-include-issue/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/70897-weird-include-issue/#findComment-356421
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/70897-weird-include-issue/#findComment-356423
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/70897-weird-include-issue/#findComment-356430
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/70897-weird-include-issue/#findComment-356434
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/70897-weird-include-issue/#findComment-356441
Share on other sites

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!

Link to comment
https://forums.phpfreaks.com/topic/70897-weird-include-issue/#findComment-356464
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.