我可以在PHP中存储一个可以在所有文件中访问的变量,而不包含另一个文件吗?
The idea is to store the base address of the Test Website, which is essentially a subfolder within the main domain (eg: www.mydomain.com/mywebsite/). It is not difficult to get this string value. I just have to use $_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']
in the index page.
But what I want is to store this value and use it to include other files. So it doesn't make sense to include a file storing the variable to get the full path. I want to use it as, something similar to $_SESSION["someName"]
. The problem with this is that, for security reasons, I wrapped the session_start()
in a function secure_session_start()
, which is in another file and needs to be included before being called.
So, in short, I need to get the value of this variable before even a single file is included()
or required()
, after it was once set from the 'index.php' page.
If I could use the session_start(), It would have been possible to store in the $_SESSION. But I can't...
EDIT:-
Here's the hierarchy of files:
Now here Public_html points to WWW.MYDOMAIN.COM. That is my website.
I am currently creating a website for my client: WWW.CLIENTWEBSITE.COM. But I'm hosting it only after the website is completed. But for now, the client can see the progress through, WWW.MYDOMAIN.COM/CLIENTWEBSITE/
So when I'm developing the website, I need to either use 'relative paths' (eg: ../somefolder/something.etc) or I can use the 'WWW.MYDOMAIN.COM/CLIENTWEBSITE/' to prefix the url's for include, require, etc. But it will change, once I host it on their domain to WWW.CLIENTWEBSITE.COM. But I don't want to end up changing each and every url's in all the pages. So I want a variable with somewhat a super-global scope which can store this base-url, that I need to change it only once.
I've been experiencing few problems with Relative paths. So, I want to use the full URL to link to a file. That is the reason why I had this question raised.
Problem I'm facing on Relative Paths: (This is not the actual question, but I'm extending it)
index.php
<?php
include_once 'includes/bootstrap_functions.php';
/* ...functions following it... */
bootstrap_functions.php
<?php
include_once "../common/dbhandler.php";
function someDBFunction(){
global $DBH;
.
.
.
/* ...functions following it... */
dbhandler.php
<?php
include_once "psl-config.php";
$DBH = new PDO("mysql:host=".HOSTNAME.";dbname=".DBNAME, DBUSER, DBPASS);
/* ...functions following it... */
psl-config.php
<?php
define("HOSTNAME","localhost");
define("DBNAME","somename");
define("DBUSER","someuser");
define("DBPASS","somepassword");
And then when I run index.php, I get this:
Lots of scripts use a constant for the base URL/path of the original script. Put this before your includes:
define( "URL", $_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'] );
Now you can use use within those files, and they can't change it.
$img = URL . "/image.png";
I'm not sure HTTP_HOST and SCRIPT_NAME are correct for your usage, but that was not the question.
Note: Don't use sessions/cookies to store this. It's just weird. Unless the path was random. If you need this same variable on multiple scripts to be the same, you need a structural change. Consider what might happen if the user lands on a page other than the front page.
UPDATE
A problem was introduced that the script which is defining the URL is not actually in the "document root", and in fact the domain changes from localhost
to example.org
.
Note: I am unable use mydomain dot com in this answer. Using example.org instead!
If your script is in a subfolder, you have two options. Navigate up two folders using dirname
or remove the subfolder via str_replace
. The first option is less likely to break if you rename your folders/scripts in the future.
Input: http://example.org/subfolder/include/common/common_functions.php
Target http://example.org/subfolder/
Option 1: Up two folders (and strip filename)
$siteurl = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
$siteurl = dirname($siteurl); // removes "common_function.php"
$siteurl = dirname($siteurl); // removes "common/"
$siteurl = dirname($siteurl); // removes "include/"
define( "URL", $siteurl ); // result: http://example.org/subfolder/
Option 2: Removing relative filename
$siteurl = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
$siteurl = str_replace('include/common/common_function.php', '', $siteurl);
define( "URL", $siteurl ); // result: http://example.org/subfolder/
In both options, we use $_SERVER['HTTP_HOST']
. By this answer's definition, HTTP_HOST is the hostname that the visitor sees. This should be localhost
for you, and example.org
for your client. I encourage you to test this yourself.
In both options, the use of $site_url
is completely optional, so that it is easier to read. You can combine these if you don't like clean code:
define( "URL", dirname(dirname(dirname('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] ))) );
Paths VS URLs
My above examples are using URLs. These are what you send to the browser, especially for images, javascript and CSS files. eg: <img src="<?php echo URL; ?>/images/my-image.png">
.
If you are including other scripts, you do not want to use a URL. You want a path instead.
$sitepath = __FILE__; // /var/www/example.org/include/common/common_functions.php
$sitepath = dirname( $sitepath ); // removes common_functions.php
$sitepath = dirname( $sitepath ); // removes common/
$sitepath = dirname( $sitepath ); // removes include/
define( "PATH", $sitepath ); // /var/www/example.org/
Using your path may not even be necessary, but you may define it:
include( PATH . '/include/common/another-script.php' );
// Alternatives relative to current script:
include( 'another-script.php' ); // When the current script is in the same folder
include( 'common/another-script.php' ); // Current script is in the "include" folder
include( '../rare/another-script.php' ); // "rare" folder is next to "common", current script is in "common"
You can configure an auto_prepend_file
using the php.ini directive that will automatically be included before every web request
Here's one option I found, use setcookie("root","www.mydomain.com/subfolder/");
and then use echo $_COOKIE["root"]
later wherever you needed it, irrespective of the page.
This works brilliantly, but is not at all secure for sensitive data.
The negative with this method, is that the cookie will be sent along with all types of communications between the client and server, which makes page loading slower, especially if the cookie is storing some big data (eg: image)
Quoting what is said at Best Practices in PHP :
Making your application location independent
PHP has problems in some situations when include files are nested and reside in different folders and it is unclear at which directory level the file will be included. One can solve this by using absolute path names or using $_SERVER['DOCUMENT_ROOT'] as a starting point. However this makes your code location dependent - it will not run anymore if you move it down your directory structure one level. Of cource we do not like that. I have found a convenient solution to this problem. The toplevel page (the one that is called by the browser) needs to know the relative path to the application root directory. Unfortunately there is no such function in PHP and the webapp context concept is completely absent in PHP. So we can not automatically determine the application root reliably in all situations (It is really impossible. Don't even try. It's not worth the effort.) Let's define a global variable called $ROOT in an include file in every directory that contains toplevel pages. The include file (call it root.inc.php) must be included by the page logic before any other include files. Now you can use the $ROOT variable to reference include files with their exact path!
Sample: We have toplevel pages in /admin/pages/. The $ROOT variable must therefore be set to $ROOT = '../..';. The page logic included by pages in that folder would reference their include files like require_once("$ROOT/lib/base.inc.php");
This sure uses include, and the file is repeated in all the directories, but is a rather useful way. The first paragraph explains the exact kind of dilemma I'm in. The second paragraph is a good way to make sure you can use the same code in all the php files I'm using and still have the freedom to move the files around in different directories, rename the directories, or even change domains... Pretty simple but effective!!
I did have a thought of putting the 'root address' in a file near the index.php file. But to include that, you still need to know the location of the current file. I never thought of repeating the file in all directories.