简体   繁体   English

如何在外部文件中执行php

[英]How to execute php in an external file

Why doesn't this work? 为什么不起作用?

// File hosted on `example.com`
// db-con.php
<?php
  define("DB_HOST" , "host");
  define("DB_NAME" , "name");
  define("DB_USER" , "user");
  define("DB_PASS" , "pass");

- --

// File hosted on `another-example.com`
// index.php
<?php
  include 'http://example.com/db-con.php';
  echo DB_HOST;

- --

// output
Notice: Use of undefined constant DB_HOST - assumed 'DB_HOST' in C:\Users\Alex\Dropbox\Shared\Web\htdocs\BASE_TEMPLATE\index.php on line 14

Surely by including the external file, the php is run, and the constants are defined? 当然,通过包括外部文件,可以运行php,并定义常量吗?

You are not including the file as you see it, but instead including the response of the remote web server when that file is requested . 您所看到的不是包含文件,而是包含请求该文件时远程Web服务器的响应

That is, the remote web server sees a request for db-con.php , loads it up, executes the code (defining constants in its own local process) and returns the output to you (which is probably empty, as the code does not echo anything). 也就是说,远程Web服务器看到对db-con.php的请求,将其加载,执行代码(在其自己的本地进程中定义常量),然后将输出返回给您(该输出可能为空,因为代码没有回声)。 Therefore the end result is the same as if you had included an empty file. 因此,最终结果与包含一个空文件的结果相同。

Update: dug up the reference from the manual: 更新:从手册中挖出参考:

If "URL fopen wrappers" are enabled in PHP (which they are in the default configuration), you can specify the file to be included using a URL (via HTTP or other supported wrapper - see Supported Protocols and Wrappers for a list of protocols) instead of a local pathname. 如果在PHP中启用了“ URL fopen包装器”(默认配置),则可以使用URL指定要包含的文件(通过HTTP或其他受支持的包装器-有关协议列表,请参阅受支持的协议和包装器)而不是本地路径名。 If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. 如果目标服务器将目标文件解释为PHP代码,则可以使用与HTTP GET一起使用的URL请求字符串将变量传递到包含的文件。 This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; 严格来讲,这与包括文件并让其继承父文件的变量作用域不同; the script is actually being run on the remote server and the result is then being included into the local script. 该脚本实际上是在远程服务器上运行的,然后将结果包含在本地脚本中。

So how to do it? 那怎么办呢?

Well, including code from a remote server is something you shouldn't really think of doing (although there are ways to make it happen, it's a really bad idea). 嗯,包括来自远程服务器的代码是您不应该真正想到的事情(尽管有多种方法可以实现它,但这是一个非常糟糕的主意)。 In any case you won't be able to do it without the explicit cooperation of the remote server (otherwise anyone could include anyone else's configuration file and use get_defined_constants to get the passwords). 无论如何,如果没有远程服务器的明确配合,您将无法做到这一点(否则任何人都可以包括其他人的配置文件,并使用get_defined_constants来获取密码)。 And if you do it, anyone else would be able to follow the same steps and get hold of your passwords. 而且,如果您这样做,其他任何人都可以按照相同的步骤操作,并掌握您的密码。 You don't want that to happen. 您不希望这种情况发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM