douhan5547 2014-01-02 02:16
浏览 10
已采纳

使用常量来包含文件

I got something like this, but it doesn't work, seems like it's not working because of the servername is shown.

DEFINE("APP_FOLDER", "website");
DEFINE("MODULES_DIR", $_SERVER["SERVER_NAME"].APP_FOLDER."/modules");

include_once(MODULES_DIR."/class-ViewRender.php");

It shows an error: *Warning: include_once(localhost/website/modules/class-ViewRender.php): failed to open stream.*

What I also want is that I can have a file like this

myappfolder/pages/users.php -> the file class-ViewRender.php should be successfully included

myappfolder/pages/administration/administrativeusers.php -> Here the file class-ViewRender.php should be successfully included as well.

How can I achieve this including the same config.php in every php file?

  • 写回答

2条回答 默认 最新

  • doukeyong3746487 2014-01-02 03:46
    关注

    This is a one major problem beginners face in PHP, I was affected alot. Best way to over come the issue is to using the 'File system_path'

    for eg.

    $white_list = array('127.0.0.1', '127.0.0.0','::1');
    
    defined('DS')? null:define('DS', DIRECTORY_SEPARATOR);
    
    if(!in_array($_SERVER['REMOTE_ADDR'], $white_list)){
        // Remote SERVER
        define('SITE_ROOT',       DS.'home'.DS.'some_name'.DS.'public_html'.DS.'your_web_root_folder_name');
    }
    else{
        //localhost
        define('SITE_ROOT', 'c:'.DS.'wamp'.DS.'www'.DS.'your_web_root_folder_name');
    }
    

    Then you can simply use SERVER_ROOT constant it in your PHP files.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据