Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用Include、require、file\u get\u contents、readfile获取$\u服务器_Php - Fatal编程技术网

Php 使用Include、require、file\u get\u contents、readfile获取$\u服务器

Php 使用Include、require、file\u get\u contents、readfile获取$\u服务器,php,Php,为什么每当我使用include、require、file\u get\u contents或readfile来获取$\u SERVER['SERVER\u NAME']时,都会得到不同的结果 file1.php 网站:example1.com <?php //sample code inside this file $_SERVER['SERVER_NAME']; ?> file2.php 网站:example2.com <?php require("exa

为什么每当我使用include、require、file\u get\u contents或readfile来获取$\u SERVER['SERVER\u NAME']时,都会得到不同的结果

file1.php
网站:example1.com

<?php
  //sample code inside this file

  $_SERVER['SERVER_NAME'];
?>

file2.php
网站:example2.com

<?php
  require("example1.com/file1.php"); //result: example1.com

  //my expected result is example2.com
?>

我在file2.php中的预期结果是example2.com,但它只显示example1.com而不是example2.com。为什么$\u服务器只在另一端工作,而不按预期工作,而假定它只获取代码?


我只是想说清楚:

下面是一个例子:
example1.com
file1.php
在file1.php内部是$_SERVER['SERVER_name'];


example2.com
包括example1.com/file1.php


example3.com
包括example1.com/file1.php


我希望使用集中文件(file1.php from example.com)在各自的网站中显示每个网站的url



但问题是每个网站都显示example1.com,而不是自己的网站url。

使用
require(“example1.com/file1.php”)//结果:example1.com
您不需要代码。您需要代码执行的结果。如果包含代码本身,只需将
file1.php
重命名为,例如,
file1.txt

您可以使用:$\u服务器['HTTP\u主机]。$\u服务器['REQUEST\u URI']

网站:www.example-one.com 文件:linkone.php

<?php 
$addrone = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
?>
<?php
    include_once 'www.example-one.com'; // Adds $addrone;
    $addrtwo = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
?>

<!DOCTYPE html>
<html>
<head>
    <script>
    document.onreadystatechange = function()
    {
        if(document.readyState == "complete")
        {
            var onex = document.getElementById("one");
            var twox = document.getElementById("two");
            onex.onclick = function()
            {
                alert("<?php echo $addrone; ?>");
            }
            twox.onclick = function()
            {
                alert("<?php echo $addrtwo; ?>");
            }
        }
    }
    </script>
</head>
<body>
    <button id="one">one</button>
    <button id="two">two</button>
</body>
</html>

网站:www.example-two.com 文件:linktwo.php

<?php 
$addrone = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
?>
<?php
    include_once 'www.example-one.com'; // Adds $addrone;
    $addrtwo = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
?>

<!DOCTYPE html>
<html>
<head>
    <script>
    document.onreadystatechange = function()
    {
        if(document.readyState == "complete")
        {
            var onex = document.getElementById("one");
            var twox = document.getElementById("two");
            onex.onclick = function()
            {
                alert("<?php echo $addrone; ?>");
            }
            twox.onclick = function()
            {
                alert("<?php echo $addrtwo; ?>");
            }
        }
    }
    </script>
</head>
<body>
    <button id="one">one</button>
    <button id="two">two</button>
</body>
</html>

document.onreadystatechange=函数()
{
如果(document.readyState==“完成”)
{
var onex=document.getElementById(“一”);
var twox=document.getElementById(“两”);
onex.onclick=函数()
{
警报(“”);
}
twox.onclick=函数()
{
警报(“”);
}
}
}
一
二

希望这能有所帮助。

目前还不清楚到底是什么情况。
$\u服务器
变量是从HTTP请求初始化的。您包含的所有后续文件都位于相同的HTTP请求上下文中,并共享相同的
$\u服务器变量。在同一个请求中,变量不会从一个文件更改为另一个文件。因此,这意味着除了将$\u服务器直接放入file2.php之外,没有其他方法可以将它包含到另一个文件中?我仍然不清楚您在问什么。你到底想做什么?为什么?@deceze我真正想做的是获取example2.com的url。我之所以使用这种场景,是因为我正在做一个集中的文件,其中我将只在file2.php、file3.php、file4.php中包含file1.php。每个文件都将显示其当前的服务器url,而不是file1.php服务器url文件没有url。或者它可能有无数个不同的URL,它们都指向同一个文件。你想要的没有意义,在我的情况下我不能用这个。抱歉。@GlenDraco,您不能影响其他服务器的行为。