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 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题