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

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大