douqiao8032 2014-06-14 18:19
浏览 38
已采纳

在PHP中从root设置函数中的地址

I’m working on a script on localhost using a WAMP server. I wanted to make my website multilingual so I defined a function which you can see below:

function p($name,$langIn = 'fa_IR')
{
    switch ($langIn) {
        case 'en_US':
            require('/inc/languages/en_US/all_lang_en_US.php');
        break;
        default:
            require('/inc/languages/fa_IR/all_lang_fa_IR.php');
    }

    if(isset($lang[$name]))
        echo $lang[$name];
    else
        echo '<span style="color: red;">Error!</span>';
}

The function stored in localhost/aroozi/inc/functions/global_func.php. The required address works properly on localhost/aroozi but when I post the registration form values to localhost/aroozi/processors/register.php it doesn't work and the script will stopped working because I used p() function in the header of pages in localhost/aroozi/inc/header.php:

<!Doctype html>
<head>
.
.
.
<title><?php p('title',$lang); ?></title>
.
.
.

My basic question is how can I set an address so that works in all of the directories of the script?

  • 写回答

1条回答 默认 最新

  • duanaoou4105 2014-06-14 18:28
    关注

    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 therequire 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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)