dpqvwgr74759 2015-06-05 07:43
浏览 51
已采纳

PHP路径开始/

In HTML you can set your href to "/folder/page" and the starting / would go to the root-directory of your web application (meaning it would start at www.yoursite.com) and move on from there.

Now, in PHP, can i do this? I am having a problem, since i have a file, that ALWAYS needs to start at the root, and then go a folder down. I can't use relative paths, since it will be included in other pages and of thus, i wouldn't know how many layers to go up before i was in the root directory.

I tried using "$_SESSION['DOCUMENT_ROOT']" but that gives me an address too far up the directory tree. For example (on a test site) i have this URL: http://localhost:8888/kasseNet-BitBucet/

And i want THAT to be my root. Currently it's something like "Applications/Mamp/htdocs".

How can i achieve this? Obviously i could use an absolute path for this, but then it wouldn't be easy for me to launch it, since i would have to change it to not be on a localhost environment.

I have been googling my ass off, but i can't seem to find the right fit. It should be an easy task to accomplish.

For completeness sake, here is my structure:

(assuming we are in "root" - the localhost htdocs/site folder) - Login *Authenticate.php *login.php - Other *test.php - default.php

Now, both "default.php" and "test.php" will include the authenticate.php. The authenticate.php will redirect to the login.php. Now, the problem is that if i include it in the default.php, my path in the Authenticate.php file to the login.php would be "Login/Authenticate.php". BUT, if i include it in the test.php, i would have to do "../Login/Authenticate.php". This illustrates the problem, and it only gets worse as the structure grows.

As you can imagine, it is for a login script where i include the authenticate file, that will redirect to the login page if the user is not logged in. All the examples i've found though, just uses a flat structure with no extra directories, so they don't have that problem.

Hoping for some help, been stuck for days thinking of a solution xD

Thank you in advance. Best Regards /JBJ

  • 写回答

1条回答 默认 最新

  • douqu8828 2015-06-05 07:47
    关注

    Use this to get the root URL and append whatever you want after it.

    $root = (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';


    If your path is something like http://localhost:8888/kasseNet-BitBucet/path1/path2/index.php you have to define kasseNet-BitBucet as your root directory. PHP cannot undrestand that kasseNet-BitBucet is your root directory and path1 for example is not.

    So:

    $my_root_dir = 'kasseNet-BitBucet';
    $root = (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/' . $my_root_dir;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源