My basic question is how can I set an address so that works in all of
the directories of the script?
The issue is you are relying on relative paths—like require('/inc/languages/…)
—which is not good practice. You can go nuts trying to balance where to set /
or ../
or even ../../
. And the best way to avoid it is to set a base path in your main config.
So you would set something like this in a config file:
$BASE_PATH = '/full/path/to/your/codebase/here/';
If you don’t know what your file system base path is, just place this line of code in your PHP code; like index.php
:
echo "Your path is: " . realpath(dirname(__FILE__)) . "<br />";
Then load that page. Somewhere near the top will be this text:
Your path is: /full/path/to/your/codebase/here/
Then with that set you can change your code to be something like this:
include_once 'config.php';
function p($name,$langIn = 'fa_IR')
{
global $BASE_PATH;
switch ($langIn) {
case 'en_US':
require_once($BASE_PATH . 'inc/languages/en_US/all_lang_en_US.php');
break;
default:
require_once($BASE_PATH . 'inc/languages/fa_IR/all_lang_fa_IR.php');
}
if(isset($lang[$name]))
echo $lang[$name];
else
echo '<span style="color: red;">Error!</span>';
}
Note the changes I made are as follows:
- Setting
include_once
for the config.php
for this example. Place it wherever makes sense in your code.
- Setting
global $BASE_PATH;
within the function so that $BASE_PATH
can easily be loaded in the function.
- Changing the
require
calls to require_once
since it is just better practice & helps the code avoid situations where multiple require
calls might cause your script to bomb out.
- Setting
$BASE_PATH . 'inc/languages/
in the require_once
calls to actually use the $BASE_PATH
concept.
Yes, it seems like more work to setup a config.php
that is loaded via a relative path and then using $BASE_PATH
on the next line, but the benefit is that once those config.php
lines are set, you no longer have to worry about it. Then $BASE_PATH
can be set to meet the needs of whatever setup you are on.
EDIT: As per the comments, the original poster is not understanding the difference between a file system path & a web server URL. To be as clear as possible, this is the file system path as reported by the original poster in Windows:
C:\wamp\www\aroozi
And this is the web URL that accesses that WAMP URL via a web browser:
http://localhost/aroozi
When a request is made to the files for http://localhost/aroozi
the web server looks inside C:\wamp\www\aroozi
on the file system. So that path is consider the base path on the file system.
So using my example above this would be set as follows for Apache PHP use; Windows paths have he slashes reversed since PHP & Apache in WAMP work via Unix/Linux path setups:
$BASE_PATH = '/wamp/www/aroozi';
Or using the original posters config setup in constants.php
:
define('BASE_PATH','/wamp/www/aroozi');
And then just prepend either $BASE_PATH
or BASE_PATH
to the require
/include
suff like this:
require_once($BASE_PATH . 'inc/languages/en_US/all_lang_en_US.php');
Or this:
require_once(BASE_PATH . 'inc/languages/en_US/all_lang_en_US.php');