drci47425 2010-10-20 10:47 采纳率: 100%
浏览 38
已采纳

需要帮助PHP“include” - 如何在变量中保存我的网站路径?

I use the following scheme to include PHP files:

require($_SERVER['DOCUMENT_ROOT'] . "Path/To/My/Website/Path/To/My/File.php");

I would like to save

$_SERVER['DOCUMENT_ROOT'] . 'Path/To/My/Website'

in some variable, say $my_website, and write:

require("$my_website/Path/To/My/File.php");

This way, if I decide to change the path to my website, I will need to modify it only in one place in my code.

Some PHP files may be included several times and from different directory levels. For example:

$_SERVER['DOCUMENT_ROOT']
                         Path
                             To
                               My
                                 Website
                                        Dir1
                                            a.php
                                        Dir2
                                            b.php that includes a.php
                                        Dir3
                                            Dir4
                                                c.php that includes a.php

However, I can't think how to do this.

Please suggest.

  • 写回答

3条回答 默认 最新

  • dongtiaozhou4914 2010-10-20 11:00
    关注

    Use include path - will make your life simpler (you can control it even from .htaccess).

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

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计