How to include php db config file form sub directory - php

I want to use this database config file which located in-> "/main/config.php" directory. Now please tell me how can i include that config.php from-> "/main/admin/login.php" file.
i have simply tried following but not worked-
<?php
include "/main/config.php";
?>
thanks in advance

If you are trying to go back to the file directory then this will include the file. However you have missed the brackets.
<?php include("../main/config.php"); ?>

Related

include files from subdirectory which calls from another directory

I have a file here: public_html/wiki/index.php
Im calling these files:
<?php
include "../auth.php";
include "../header.php";
?>
However, my page looks like this:
If I paste the same file in public_html/index.php then it will look like this:
I think I've narrowed it down to my header.php file which is being called from /wiki/ but header calls files from ../css.. and ../js and so on.
How can this be adressed? I've looked at many posts already which tells me to define a global variable but it doesn't help me. Like so:
<?php //config file in root folder
define("ROOT", __DIR__ ."/");
?>
and then calling them with this:
<?php
include_once("../config.php");
include (ROOT ."auth.php");
include (ROOT ."header.php");
?>
How should I do this so I can get all my javascripts and the actual site with me in another folder?
NOTE: I WANTED THIS AS A COMMENT BUT DO NOT HAVE THE REP
We have a different folder structure, but it is good practice to put your includes in one folder and from there link all the JS files (best practice is, place them in a pre-footer for speed) and then do
<?php include $_SERVER['DOCUMENT_ROOT'].'/includes/page-footer.php'; ?>
Just change the file name to suit you :) hope this helps
the problem was my include file didnt specify links as /js/file.js
i had them as js/file.js.
thanks #cd001

Index file conflict with include files

I have a problem with my index.php together with my "include files". My index.php file is located inside the main folder named mysite and the rest inside it are subfolders with the respective .php files. The sub-folder files are working perfectly, except the index.php. The include files are messed up every time I preview it in the browser.
The path for the index.php file goes like this:
mysite/index.php
The path for the include files goes like this:
mysite/pages/include/header.php
Here is the HTML file with the include files:
<html>
<head>
<?php
include ("pages/include/headertop.php");
include ("pages/include/header.php");
include ("pages/include/nav.php");
?>
</head>
</html>
Kindly correct me if I have missed something here.
By the way, I'm using XAMPP.
Thank you and More power!
edit your index.php:
include ("../pages/include/headertop.php");
include ("../pages/include/header.php");
include ("../pages/include/nav.php");
and try again "mysite/index.php"
Firstly make sure that you are calling these include commands in a .php file and not .html file.
Secondly make sure that your server is up and running well.
After this try this code:
<html>
<head>
<?php
include "pages/include/headertop.php";
include "pages/include/header.php";
include "pages/include/nav.php";
?>
</head>
</html>
P.S: just remove the brackets from include.
Please also make sure that the file names are correct and they follow the proper case.

How to include php file in a php file

Hey I would like to include a navigation.php file in my website, but i cant figure out how to include an external file in a .php file, and the only way i could find to include the navigation.php into the files was to make a copy of it in each directory.
<?php include("navigation.php"); ?> works when i make a copy of the navigation.php in each directory.
<?php include("http://www.mysite.net/format/navigation.php"); ?> gives me an error.
I would like to only have one of these navigation.php files in a "Format" directory and then make each page fetch the file and display it.
Also ill be monitoring this question for a while so just ask me if you need any additional info.
Put that file one place. Then refer to it in each file by its proper path.
// navigation.php is in the same directory
<?php include("navigation.php"); ?>
<?php include("./navigation.php"); ?>
// navigation.php is one directory below the current directory
<?php include("../navigation.php"); ?>
// navigation.php is two directories below the current directory
<?php include("../../navigation.php"); ?>
But this can get tedious if you have lots of files in lots of directories. The best way to do this is to use the full path to the file on the file system:
// Full path relative to the root of the account
<?php include("/full/path/to/navigation.php"); ?>
But this can get tedious if the path changes. So define a variable or constant to hold that value for you so you only need to change it in one place:
// In a config file
define('INCLUDE_PATH', '/full/path/to/');
// In each page after you include your config file
<?php include(INCLUDE_PATH . "navigation.php"); ?>

Why won't my function include work?

I'm trying to include other page from other folder. and i tried this and it won't show the "side.php"
<?php
include 'side.php';
?>
And the "side.php" has:
home
Well you have the answer to your question inside your question.
" Im trying to include other page from other folder"
It means that you have to go inside that folder in order to access the file
<?php
include 'folder_name/side.php'
?>
or just copy the side.php file where your working file is so that all files are in one place
Maybe I'm wrong because I don't know php so it might not work but you could check this out
Include PHP file into HTML file

How can i get link path if i want include another folder

I want to know that how can i give include path of another folder.
For example, i have a project named test folder. In test folder has one folder which is assets folder and index.php file. In assets folder has two folder which are include and xyz folder. In include folder has two file which are header.php and footer.php.In header.php file has three link home, product and customer. In xyz folder has product.php and customer.php file.
In index.php, product.php and customer.php i want to include header.php and footer.php.
But i can't right path.
<?php include('../../header.php');?>
...........
<?php include('../../footer.php');?>
Please help me, how can i give right path which if i click product link then header.php and footer.php will be load.
Try this :
<?php
include_once(dirname(__FILE__) . '/database.class.php');
?>
Ref: http://php.net/manual/en/function.dirname.php
If your Directory look something like this:
src
->includes
->header.php
->footer.php
web
->index.php
And you want to include the header and the footer in the index.php, do the following:
<?php
//index.php
include __DIR__.'/../src/includes/header.php';
echo "foobar";
include __DIR__.'/../src/includes/footer.php';
?>
http://php.net/manual/en/language.constants.predefined.php
Basically i have used like below
<?php include_once('../include/header.php');?>
<?php include_once('../include/footer.php');?>
echo realpath('../../header.php');
echo realpath('../../footer.php');
I think that if you can replace
../../header.php by ../include/header.php and
../../footer.php by ../include/footer.php.
then you will get accurate result.

Categories