【问题标题】:PHP - How to determine the file is in public_html folder?PHP - 如何确定文件在 public_html 文件夹中?
【发布时间】:2021-12-22 18:17:44
【问题描述】:

我在包含文件中使用以下 $_SERVER 变量。

$page_url = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$ip_address = $_SERVER['REMOTE_ADDR'];

当我将此文件包含在我的 Cron 作业主目录中的 PHP 脚本中时,它会将错误 Undefined array key "HTTP_HOST"Undefined array key "REQUEST_URI"Undefined array key "REMOTE_ADDR" 抛出到 error_log 文件中。 p>

为避免这种情况,我想让此代码仅在文件位于 public_html 目录中时显示。 PHP if 语句怎么说?

谢谢!

【问题讨论】:

  • 您可以使用getcwd()查看当前目录。查看更多w3schools.com/php/func_directory_getcwd.asp
  • To avoid this, I want to make this code to show only if the file is in the public_html directories - 这是一个奇怪的方向,它与您要解决的问题没有直接关系。你可以使用empty($_SERVER['HTTP_HOST']) 作为一个简单但功能性的测试;但是检查 sapi 名称是正常的方法。
  • 到目前为止它对我来说工作正常,并且没有错误或问题。如果我将来遇到任何问题,我一定会尝试复杂的方法。感谢您的建议!
  • the complicated way ?

标签: php cron


【解决方案1】:
/**
 * Check if file is in public directory. This will not work if running via CLI.
 *
 * @param bool $checkCallerScript Set to `true` to check for caller script using trace.
 * @return bool Return `true` if it is in public directory, return `false` for otherwise.
 */
function isInPublicDir(bool $checkCallerScript = true): bool
{
    if (!isset($_SERVER['HTTP_HOST']) || !isset($_SERVER['REMOTE_ADDR'])) {
        // if no HTTP or ip address from user.
        return false;
    }

    if (!isset($_SERVER['DOCUMENT_ROOT']) || empty($_SERVER['DOCUMENT_ROOT'])) {
        // if no document root. run from CLI don't have this value.
        // see https://www.php.net/manual/en/reserved.variables.server.php for more details.
        return false;
    }
    $docRoot = str_replace(['\\', '/', DIRECTORY_SEPARATOR], DIRECTORY_SEPARATOR, $_SERVER['DOCUMENT_ROOT']);

    if (true === $checkCallerScript) {
        $traces = debug_backtrace();
        if (is_array($traces)) {
            foreach ($traces as $trace) {
                $callerFile = str_replace(['\\', '/', DIRECTORY_SEPARATOR], DIRECTORY_SEPARATOR, ($trace['file'] ?? null));
                if (!isset($callerFile) || stripos($callerFile, $docRoot) === false) {
                    return false;
                }
            }
            unset($callerFile, $trace);
        }
        unset($traces);
    }
    unset($docRoot);

    return true;
}

如果通过 CLI 或没有 DOCUMENT_ROOT 值的东西运行,上述功能将不起作用。 阅读更多关于$_SERVER

如果我有这样的文件...

/var/www/public_html/run.php
/var/www/test.php
/var/www/functions.php

公共目录是/var/www/public_html

require dirname(__DIR__) . '/functions.php';



var_dump(isInPublicDir());// expect true
var_dump(isInPublicDir(false));// expect true


require dirname(__DIR__) . '/test.php';

run.php 内容。

.

<?php



echo 'hello from ' . __FILE__ . '<br>' . PHP_EOL;

var_dump(isInPublicDir());// expect **false**
var_dump(isInPublicDir(false));// expect true

test.php 内容。

【讨论】:

  • 哦,伙计..,我真的很感谢你的努力 :) 但是这个if (strpos(getcwd(), 'public_html') !== false) { $myVariables; } 对我来说就足够了。我为你的回答投了赞成票。非常感谢队友..!
【解决方案2】:

根据 iazaran 的回复,此代码对我有用!

if (strpos(getcwd(), 'public_html') !== false) {
  $page_url = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
  $ip_address = $_SERVER['REMOTE_ADDR'];
}

我认为它可能对像我这样的人有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-22
    • 2016-05-13
    • 2016-10-04
    • 2016-08-22
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    • 2018-09-02
    相关资源
    最近更新 更多