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

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.

Related

How can i get the file url and file path inside the code PHP [duplicate]

I want to use __dir__.
However, I can't find any good tutorial on how to set it up. I have my htdocs in Dropbox.
Does it work something like this?
define(__DIR___, 'd:documents/dropbox/yolo/swag/htdocs/myproject/test/newtest/
testphp/test_new/testincludes/1/new_folder/')
That is the directory where my project is located and it has sub folders. I want to include a file into another file that is in the parent folder.
Should I then just type:
include'__DIR__/warlock.php';
Or do I have to type something like this?
include '___DIR__/wow/newb/guidesfornabz/classes/casters/warlock.php';
You can use __DIR__ to get your current script's directory. It has been in PHP only since version 5.3, and it's the same as using dirname(__FILE__). In most cases it is used to include another file from an included file.
Consider having two files in a directory called inc, which is a subfolder of our project's directory, where the index.php file lies.
project
├── inc
│   ├── file1.php
│   └── file2.php
└── index.php
If we do include "inc/file1.php"; from index.php it will work. However, from file1.php to include file2.php we must do an include relative to index.php and not from file1.php (so, include "inc/file2.php";). __DIR__ fixes this, so from file1.php we can do this:
<?php
include __DIR__ . "/file2.php";
To answer your question: to include the file warlock.php that is in your included file's upper directory, this is the best solution:
<?php
include __DIR__ . "/../warlock.php";
I've been looking to use an executed in apache's root htdocs _DIR_ variable and be able to include other scripts containing sensitive data such as database login credentials sitting outside it. I struggled a bit trying different options but the below is working really well.
Firstly, in my apache virtual host config I set/include a full linux path to apache's htdocs (you can add more paths by appending at the end :/path/to/folder/):
php_value include_path ".:/var/www/mywebsite.ext/uat/htdocs"
Then in .htacess stored in apache's htdocs root (and git repo):
php_value auto_prepend_file "globals.php"
Both of the above can be set in .htacess although it wouldn't work well for multiple environments suchas as DEV, UAT, PRODUCTION in particular when using git repo.
In my globals.php file inside apache's htdocs I have defined a variable called DIR that's globally used by htdocs php scripts:
define('DIR', __DIR__);
Then in each file am was now able to include/require necessary files dynamically:
require_once(DIR.'/file_folder_inside/apache's_htdocs/some-file.php');
The DIR variable would always resolve to /var/www/mywebsite.ext/uat/htdocs no matter where in the tree I call it in the above example producing
/var/www/mywebsite.ext/uat/htdocs/file_folder_inside/apache's_htdocs/some-file.php.
Now, I was looking to access a php file that's sitting outside my apache's htdocs root folder which is also easily achievable by using:
require_once(DIR.'/../apache's_htdocs_parent_folder/some-file-stored-outside-htdocs-eg-snesitive-credentials.php');
This is an example of how to use __DIR__ and go to the parent directory in different PHP versions, so if you can recognize which one is old and which one is new:
For PHP < 5.3 use:
$upOne = realpath(dirname(__FILE__) . '/..');
In PHP 5.3 to 5.6 use:
$upOne = realpath(__DIR__ . '/..');
In PHP >= 7.0 use:
$upOne = dirname(__DIR__, 1);

Root path variable in a PHP project

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';

How to use __dir__?

I want to use __dir__.
However, I can't find any good tutorial on how to set it up. I have my htdocs in Dropbox.
Does it work something like this?
define(__DIR___, 'd:documents/dropbox/yolo/swag/htdocs/myproject/test/newtest/
testphp/test_new/testincludes/1/new_folder/')
That is the directory where my project is located and it has sub folders. I want to include a file into another file that is in the parent folder.
Should I then just type:
include'__DIR__/warlock.php';
Or do I have to type something like this?
include '___DIR__/wow/newb/guidesfornabz/classes/casters/warlock.php';
You can use __DIR__ to get your current script's directory. It has been in PHP only since version 5.3, and it's the same as using dirname(__FILE__). In most cases it is used to include another file from an included file.
Consider having two files in a directory called inc, which is a subfolder of our project's directory, where the index.php file lies.
project
├── inc
│   ├── file1.php
│   └── file2.php
└── index.php
If we do include "inc/file1.php"; from index.php it will work. However, from file1.php to include file2.php we must do an include relative to index.php and not from file1.php (so, include "inc/file2.php";). __DIR__ fixes this, so from file1.php we can do this:
<?php
include __DIR__ . "/file2.php";
To answer your question: to include the file warlock.php that is in your included file's upper directory, this is the best solution:
<?php
include __DIR__ . "/../warlock.php";
I've been looking to use an executed in apache's root htdocs _DIR_ variable and be able to include other scripts containing sensitive data such as database login credentials sitting outside it. I struggled a bit trying different options but the below is working really well.
Firstly, in my apache virtual host config I set/include a full linux path to apache's htdocs (you can add more paths by appending at the end :/path/to/folder/):
php_value include_path ".:/var/www/mywebsite.ext/uat/htdocs"
Then in .htacess stored in apache's htdocs root (and git repo):
php_value auto_prepend_file "globals.php"
Both of the above can be set in .htacess although it wouldn't work well for multiple environments suchas as DEV, UAT, PRODUCTION in particular when using git repo.
In my globals.php file inside apache's htdocs I have defined a variable called DIR that's globally used by htdocs php scripts:
define('DIR', __DIR__);
Then in each file am was now able to include/require necessary files dynamically:
require_once(DIR.'/file_folder_inside/apache's_htdocs/some-file.php');
The DIR variable would always resolve to /var/www/mywebsite.ext/uat/htdocs no matter where in the tree I call it in the above example producing
/var/www/mywebsite.ext/uat/htdocs/file_folder_inside/apache's_htdocs/some-file.php.
Now, I was looking to access a php file that's sitting outside my apache's htdocs root folder which is also easily achievable by using:
require_once(DIR.'/../apache's_htdocs_parent_folder/some-file-stored-outside-htdocs-eg-snesitive-credentials.php');
This is an example of how to use __DIR__ and go to the parent directory in different PHP versions, so if you can recognize which one is old and which one is new:
For PHP < 5.3 use:
$upOne = realpath(dirname(__FILE__) . '/..');
In PHP 5.3 to 5.6 use:
$upOne = realpath(__DIR__ . '/..');
In PHP >= 7.0 use:
$upOne = dirname(__DIR__, 1);

How can I access a page that is not on the root and has inclusions

I am working on a web application that uses PHP and JavaScript and I have some troubles accessing some pages that are not on the root if they are including other files (ie. 'include once').
[file system]/www/project/test/test.php
The project folder has the index.php. Here is an example of my code:
include_once('../config/config.php');
include_once('../database/database.php');
echo 'Test.php';
$config = new Config();
$dbhandle = new Database();
[...]
What am I doing wrong here?
The error log from Apache tells me that there is no file config.php and database.php.
use $_SERVER['DOCUMENT_ROOT'] to relocate the path to the root path of your server like this :
$root_path = $_SERVER['DOCUMENT_ROOT'] ; //Returns the root path of your server
//Then call your inlude_once with the absolute path
include_once($root_path . '/project/config/config.php')
So now all your path are Absolute and not relative.
Hope that helps.
PS : You can also get confortable using a php framework that will do the magic of routing your dependencies easier on a bigger project.
to get document root use
$_SERVER['DOCUMENT_ROOT'];
and hence
include_once($_SERVER['DOCUMENT_ROOT'] . '/project/config/config.php');
should work for you.

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