Root path variable in a PHP project - php

I created a php project like this:
img
js
php
config
config.php
pages
news.php
contact.php
I need to include my config.php into all my files news.php, contact.php
How to avoid to have a relative path like ../config/config?
I would like to have a root path like php/config/config.php.
I use MAMP Pro. Should I use an environment variable to save the root path?

In PHP there is a global variable containing various details related to the server. It's called $_SERVER. It contains also the root:
$_SERVER['DOCUMENT_ROOT']
The only problem is that the entries in this variable are provided by the web server and there is no guarantee that all web servers offer them.

Make a folder in your root. Name it e.g. Helpers/. Make a file in it path.php and inside it put this code.
function base_path($path = "") {
return realpath(__DIR__ . '/../') . DIRECTORY_SEPARATOR . $path;
}
then include this file at the top of your every web document same as you do for your session start and then you can use this function any where in your code. Like so
<?php
require "Helpers/path.php";
require base_path('php/config/config.php');

Create a constant with absolute path to the root by using define in ShowInfo.php:
define('ROOTPATH', DIR);
OR (PHP <= 5.3)
define('ROOTPATH', dirname(FILE));

1 Find out your document root. This value is the same for all your scripts
echo $_SERVER['DOCUMENT_ROOT']; exit;
2 Use the path relative to the document root whenever you include config.php (adjust the line below once you know your document root)
require_once $_SERVER['DOCUMENT_ROOT'].'/php/config/config.php';

Related

include file(s) from another directory

Currently I am trying to include a PHP file from another directory.
public_html/a/class/test.php <-- from this file i would to include a file from
public_html/b/common.php <-- wanted to include this file
Not sure what I should do because I have tried using
dirname(__FILE__)
and this keeps on returning public_html/a/ for me instead.
I have tried something like this
dirname(__FILE__).'/../b/common.php'
but it does not help me in getting my file.
You can simply keep moving up the directory tree until you have the common ancestor:
require dirname(dirname(__DIR__)) . '/b/common.php';
The magic constant __DIR__ equals dirname(__FILE__), and was introduced in 5.3. Each use of dirname() goes back one directory, i.e.:
dirname('public_html/a/class'); // public_html/a
dirname('public_html/a'); // public_html
Btw, editors such as PhpStorm also understand this use of relative paths.
First of all i suggest you to define a variable for basepath and include that defined variable in relative files.
// This should be on any root directory file
define("PR_BASEPATH", dirname(__FILE__));
And according to your implementation, Assume you are in
public_html/a/class/test.php
and dirname(__FILE__) returns the directory name of the current file that always return the directory class according to test.php file.
And you want to include public_html/b/common.php that is on the other directory /b. So you have to get the document root directory first.
include $_SERVER['DOCUMENT_ROOT'] . "/b/common.php";
Take a look on $_SERVER['DOCUMENT_ROOT']
include('../../b/common.php');
would include file for you, make sure both directory have same usergroup as user.

moving website to doteasy - best place to put database constants?

This is my first time moving a locally-hosted PHP site to a hosting service. My site is built with the inc folder (which includes the database constants) outside of the web root, so on my local machine it's in
XAMPP/xamppfiles/htdocs/inc/
and the rest of the files, including the common/ directory, are in
XAMPP/xamppfiles/htdocs/mydomain/
Most of the pages call
include_once "common/base.php";
which lives inside
htdocs/mydomain/common/
and which includes the database constants by calling
include_once $_SERVER['DOCUMENT_ROOT'] . "/inc/constants.inc.php";
On my local site, $_SERVER['DOCUMENT_ROOT'] outputs "Applications/XAMPP/xamppfiles/htdocs/", and the inc files are found.
Unfortunately, on the DotEasy site, the files aren't being found. If I echo $_SERVER['DOCUMENT_ROOT'], the output is "/home/myusername/public_html".
I'm able to get to the inc/ directory by this:
$temp_path = $_SERVER['DOCUMENT_ROOT'];
$temp_path = str_replace('public_html', '', $temp_path);
include_once $temp_path . "/inc/constants.inc.php";
but is this a good idea? Am I wrong to worry about having the constants in the public_html folder?
Don't use $_SERVER['DOCUMENT_ROOT'].
Instead you should define the root directory based by one file, for example in the config file.
define('ROOT', __DIR__); //php >= 5.3
or
define('ROOT', dirname(__FILE__)); // php < 5.3
The use the ROOT as the base to include other file.

Require file including variable

I am currently using the following code to include a file in my webpage:
require_once $_SERVER['DOCUMENT_ROOT'] . '/include/file.php';
However, I have now edited my system so I input the URL into a data file in the root which makes other parts of my site function and allows me to use different directories.
In short, I am using a splash page on a website, where the root is now /directory rather than in the root, thus the URL in my data file is http://www.domain.com/directory.
So, what I need to work out is how to point this line at the directory using the variable from the data file which contains the URL
So $_SERVER['DOCUMENT_ROOT'] becomes irrelevant because I need to grab the data from the variable in the data file which is NOT in the root anymore.
It needs to be something like:
require_once (variable from file a few directories back) + absolute path to file;
I want this line of code to be future-proof too, if I need to build a site using a different directory then the root.
I hope that makes sense!
Create a SITE_ROOT and use that instead. That way, it will work with any directory change.
define('SITE_BASE', '/directory/');
define('SITE_ROOT', $_SERVER['DOCUMENT_ROOT'] . SITE_BASE);
You can then use SITE_BASE for creating your URIs:
link
and SITE_ROOT for accessing files on the system:
require_once SITE_ROOT . 'include/file.php';
Have you considered setting include_path in your php.ini file? For instance, my own php.ini file has include_path = ".:/path/to/web/root", which allows me to not worry about where my files are when including them.
In your case, you would want to set it to include_path = ".:/path/to/web/root/directory". If you don't know the path to the web root, just run echo $_SERVER['DOCUMENT_ROOT']; to find it.
This solution is "future-proof", as you only need to change that php.ini value and everything falls into place.

php question include an included file

I got a problem about including a included file in PHP.
Project
functions(folder) has a.php
xml(folder) has b.xml
index.php
This is my project structure(sorry about that, I can't post images).
I try to use "index.php" to include "a.php" while "a.php" is using "b.xml"
this is what i did on XAMPP and it works perfectly:
in index.php I wrote: include 'functions/a.php';
in a.php I wrote: $xml->load('xml/b.xml');
However if I copy these to my Uni's apache server, it can't open this b.xml.
This is not permission because when i change to absolute path it works...
Thank you guys in advance:-)
in a.php you should refer to ../xml/b.xml if you use include
thing is, it depeneds on when $xml->load() is defined. if it's your own code then put the path relative to the definition. otherwise "../xml/b.xml" should work.
you can always to $_SERVER['DOCUMENT_ROOT'], but i myself like defining directories as constants (with absolute path) and using them around the project.
define('DIR_ROOT', $_SERVER['DOCUMENT_ROOT'] . '/');
define('DIR_FUNCTIONS', DIR_ROOT . 'functions/');
define('DIR_XML', DIR_ROOT . 'xml/');
Try using set_include_path() to set the include path to your application's root directory; then you should be able to include files relative to this path.
It's always better to use absolute paths, even if you have to construct it (e.g. $XML_PATH = $PATH_TO_BASE . 'xml/b.xml'; )
If you can't do that, you should add xml's parent to your path.

PHP including files

What's the difference between
$_SERVER['DOCUMENT_ROOT'];
and
dirname(__FILE__);
I wonder what's the difference because when I 'echo' them, they're returning same path. Which do you prefer should I use and why?
Thanks!
Both are different
_FILE_
The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, FILE always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.
source : PHP magic constants
Let's said, your document is /var/www,
and your index page is /var/www/index.php
dirname(__FILE__) == $_SERVER['DOCUMENT_ROOT'];
But if you drill-down to sub-folder like /var/www/posts/index.php
dirname(__FILE__) != $_SERVER['DOCUMENT_ROOT'];
/var/www/posts != /var/www
The use of $_SERVER['DOCUMENT_ROOT'] is more appropriate in this case.
__FILE__ always points to the current file path, and $_SERVER['DOCUMENT_ROOT'] points to the document root path ;-)
I prefer first one, as it is more semantic.
If you will try to compare the values of the files, that are located not in your docroot - then you'll get different values.
The former one is a root folder for the HTTP server (or VirtualHost) and it is a server setting.
The latter is the folder containing the current file.
The usage is entirely based on requirements in my opinion.
You would normally use $_SERVER['DOCUMENT_ROOT'] when you want to reference your website's root folder from any where within your website or web application.
You will find using dirname(__FILE__) handy if you were including a file, that then needed to include some more files from the same directory. I use this in my PHP wrapper for the Dribbble API
class Dribbble {
function __construct() {
require_once(dirname(__FILE__) . '/base.php');
require_once(dirname(__FILE__) . '/shot.php');
require_once(dirname(__FILE__) . '/player.php');
}
}
This means I can just include dribbble.php from any where in my website or web application and not worry about also including base.php, shot.php, and player.php at the same time.

Categories