I'm working on some web application, learning PHP OOP. I have 3 scripts: index.php, database.php and config.php.
In config.php are the constants for accessing the DB.
In the database.php is a class for opening and closing the connection to the DB and so on. This file includes the config.php file.
Now on the index file I include the database.php file and I'm testing if $database exists. The files database.php and config.php are in different folder than the index.php.
When I run the index.php file it says: Use of undefined constant DB_SERVER - assumed 'DB_SERVER'
Everything works fine if they are in the same folder. It also works if I include the config.php file separately in the index file, but it won't work if only the database file is included
config.php
<?php constants ?>
database.php
<?php
require_once("config.php");
...
?>
index.php
<?php
require_once("../includes/database.php");
...
?>
Why won't the index.php file recognize the variables from config.php?
Its probably some problem with the include path. Even though database.php and config.php are in the same folder, its index.php that is making the calls and therefore the include path is the index.php folder. When database.php attempts to include config.php it will be looking for it there.
There are a couple ways you can solve this:
Make index.php call all the includes, including the database.php dependencies;
Make database.php aware that its path relative to the site root is ../includes and it too will refer to config.php as ../includes/config.php even though its right there in the same folder with it (not recommended specially if you need database.php in subfolders of your site);
Use set_include_path(). Example:
index.php:
set_include_path('../includes');
require_once 'database.php';
I believe the problem lies with the directories. When you include the ../includes/database.php file, all paths defined in that file become relative to IT! This means that instead of looking for the configuration file in the directory that index.php is in (lets say /var/www/html/funfile/index.php), it's now looking for it in /var/www/html/includes/ (so the literal path of the include would be /var/www/html/includes/config.php). It's a pretty easy fix, just make the path to the config file literal.
Try:
require_once("/var/www/html/funfile/config.php");
And replace the path I made up, with the actual path to the config file. That should solve your problem.
You should give DB_SERVER in quotes like this:
define('DB_SERVER',$db_host);
define('DB_USER',$db_user);
define('DB_PASS',$db_pass);
define('DB_NAME',$db_name);
Related
My project has two entry point
project (root-folder)
/config(folder)
config.php
/service(folder)
service.php
index.php
for example
File 1:/index.php ( first entry point) - here we include the config form the config folder
<?php
require_once('config/config.php');
require_once('service/service.php');
?>
File 2:service/service.php - here we include the config form the config folde
<?php
require_once('../config/config.php');
?>
if i call the File 2:service/service.php has no fatal error
but when i call the File 1:/index.php it became the fatal error as failed to require 'service/service.php' because it require again and config path is invalid
How to solve this issue.
Reason:
This issue arises because your execution starts from index.php and then you require service/service.php. Now in service.php when you do ../config/config.php, PHP tries to resolve the directory path from index.php point of view and it doesn't find any such file or directory. Hence, the error.
Solution:
Declare a constant in index.php. Check if this constant exists in service/service.php. If not, then require it, else skip it like below:
index.php:
<?php
define('INDEX_ENTRY_POINT',true);
require_once('config/config.php');
require_once('service/service.php');
?>
service.php:
<?php
if(!defined('INDEX_ENTRY_POINT')){
require_once('../config/config.php');
}
?>
Note: It is always better to use __DIR__ giving absolute paths than relative paths to avoid such issues.
You have to consider that when you call service.php from index.php, the root is that of index.php. Now there are many ways to get around this. You could decide that service.php is a main controller, just as is index.php, and thus belongs in the root folder. If for some reason you want to keep it as it is, then you have to define the root in order for it to adapt to the situation as in vivek_23 answer just above. Personally, i would keep service.php in the root folder, it is more logic.
i am trying to include the same connect.php file as a part of the nav.php file throughout multiple different directories and directory depth's with a generic 'dynamic' substitute for the include path back to the connect.php file in the website root folder eg.
www.mywebsite.com/shop/shoes/sandals/index.php
and
www.mywebsite.com/shop/shoes/sandals/index.php
etc.
with the connect.php file being located in the root directory
www.mywebsite.com/connect.php
the code I am currently using in the nav.php (which is included in all pages and directories that are accessible by users)is as follows.
<?php
include '../../connect.php';
?>
however this will obviously not work in directory paths like
www.mywebsite.com/shop/shoes/sandals/index.php
I have no previous experience making a single connect.php file accessible by multiple directory paths using an absolute path with php.
however some guidance on this matter would be extremely helpful.
Thanks
You could try using something like
include $_SERVER['DOCUMENT_ROOT'] . "/connect.php";
To continue reading...
Hope this helps!
Here is config.php
<?php
define("DB_SERVER","localhost");
define("DB_NAME","photo_galley");
define("DB_USER_NAME","root");
define("DB_PASSWORD","");
echo DB_SERVER."config file<br>";
?>
Here is classes.php
<?php
var_dump(include_once("config.php"));
echo DB_SERVER."class file<br/>";
?>
<?php
class MySQLDatabase{...
and here comes third file temp.php
<?php
include_once("includes/classes.php");
echo DB_SERVER." tem.php <br/>";
?>
both classes.php and config.php are in includes folder, includes folder is in photo_gallery folder , temp.php is in photo_gallery folder
how it looks like when i open classes.php in browser.
it shows thing as i thought. First show DB_SERVER value from config.php file then from classes.php file
But when i include classes.php file in any other file like in temp.php as shown in third piece of code it gives something that i didn't understand
here is it
I didn't understand output.
I think it's output should be same as that of classes.php with one more line showing DB_SERVER value from temp.php.
Would you please tell me why these constants are shown as undefined when i open temp.php in browser?
if include function in line is successful as shown by int(1) then why not it show DB_SERVER value from config.php?
You include config.php from wrong location in temp.php. Remove the path. Also you should use require_once rather than include_once as config.php is crucial, so lack of it should stop the script.
In temp.php your config.php file path is wrong. Thats why you got the undefined constant.
config.php file inside the includes directory. But your temp.php file outside the includes directory.
in classes.php, You can use $_SERVER['DOCUMENT_ROOT'] add full path in such way you can access the same in all files.
Your scripts are fine. You just have a problem with your server configuration.
They're working correctly in mine:
When you include classes.php relative path to config.php is includes/config.php.
<?php
include_once("includes/config.php");
class MySQLDatabase{...
so we can say that when we use require include function it just copies code from file mentioned to file from which this function called. so when i call config from classes file it works fine. But when i call classes from temp it just copies code from there but for temp file config file location is different so it doesn't work. Am i correct Mr. Developers...
My database config class file is bellow
pard_config/class/Config.php
pard_config is a folder which is in the root folder.
And i have a json file which contain,host name,database,user and password details
pard_config/class/config.json
How would i include Config.json file to the Config.php file without reading it from the root.For now i'm doing it like bellow
Config.php
include("pard_config/class/config.json");
Config.json and the config.php files are in the same folder.So is there any way to read from that class folder like bellow ???
Config.php
include(config.json);
I would advice you to use a relative path and use the __DIR__ constant:
include(__DIR__ . '/config.json');
note that paths will be resolved from the script which where called in browser (or command line) .. like index.php. Therefore it will suite better in many cases to use a relative path.
I have made a config file that includes te database information (connection, and more).
The config file is called by index.php. So I have this:
index.php->calls->config.php->calls->db.php
This works, but sometimes the config.php is not called by the index.php but by for example header.php. The problem is that the relative path in the config.php to the db.php doesn't work.
How can I solve that? Should I use absolute paths?
Thanks!
If all the files are located in the same directory, you can include it like this:
include __DIR__."/config.php";
Otherwise create a constant in your index.php page before all includes that define your application path, and use that to include your files, E.G:
in index.php:
define('APP', __DIR__);
include APP.'/config/config.php';
in config.php:
include APP.'/lib/db.php';