gpt4 book ai didi

php - 要求的对立面?

转载 作者:行者123 更新时间:2023-12-04 22:31:47 26 4
gpt4 key购买 nike

是否有可能让一个 php 脚本知道另一个脚本是否通过 require 或 require_once 调用它?

例如:

if scripta.php calls me do xyz;
if scriptb.php calls me do abc;

编辑:感谢大家的建议。这更像是一个假设问题,而不是实际问题。我意识到我可以设置一个变量 $caller 并在我发出 require 语句时更新它。我只是想知道在被调用的文件中是否有另一种方法可以做到这一点:)

最佳答案

如果您只想检查您的文件是否包含在内:

$_SERVER['SCRIPT_FILENAME'] 将始终返回最初调用的脚本的文件名。 (例如“start.php”)

常量 __FILE__ 将始终返回使用它的脚本的真实文件名。(例如“library.inc.php”)

所以你可以这样做:

if ($_SERVER['SCRIPT_FILENAME'] !== __FILE__) {
echo "script was included!";
}
else {
echo "script was called directly!";
}

如果要从文件被包含的地方区分:

$include_from = get_included_files();
if (count($include_from) > 1) {
$include_from = $include_from[count($include_from)-2];
}
else {
$include_from = $include_from[0];
}
switch ($include_from) {
case __FILE__:
// not included, called directly
break;
case "/path/to/scripta.php":
// do abc
break;
case "/path/to/scriptb.php":
// do xyz
break;
default:
// do other stuff
break;
}

关于php - 要求的对立面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11898601/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com