简体   繁体   English

我可以使用PHP将include添加代码到php脚本吗?

[英]PHP can I use include to add code to a php script?

When I try using include to add php code to a sendmail script called from a form it doesn't pull in the code like I would expect. 当我尝试使用include将php代码添加到从表单调用的sendmail脚本中时,它不会像我期望的那样提取代码。

Is there another method I should use to add code to a script? 我应该使用另一种方法向脚本添加代码吗?

Thanks in advance. 提前致谢。

Besides 除了

include( 'myFile.php' );

you might use 你可能会用

require( 'myFile.php' );

which fails, if the respective file doesn't exist. 如果相应的文件不存在,则失败。

In general, I'd propose to use 一般来说,我建议使用

require_once( 'myFile.php' );

which - even if executed several times - loads the file only once. 即使执行多次,该文件也只会加载一次。

Beside these function, give 除了这些功能,给

file_get_contents( 'file.ext' ); file_get_contents('file.ext');

a try or maybe 尝试或者也许

file_get_contents( dirname( __FILE__ ) . '/../file.ext' );

to read relative to the directory of the calling script. 读取相对于调用脚本目录的信息。

Finally, note that 最后,请注意

eval( $someStringVariable );

may be used to make PHP evaluate $someStringVariable as if its contents is PHP source-code. 可以用来使PHP评估$ someStringVariable,就好像其内容是PHP源代码一样。

If all this isn't what you are in search for, please provide more details. 如果这不是您要搜索的所有内容,请提供更多详细信息。

Before you include the form-handling php code, include a file with ONLY the following: 在包含表单处理php代码之前,请仅包含以下文件:

<?php
print '<pre>FILE WAS INCLUDED</pre>';
?>

Name it "test_php_file.php". 将其命名为“ test_php_file.php”。 Then at the top of the file that the form posts to, include the above file like so: 然后在表单发布到的文件的顶部,像上面这样包含上述文件:

<?php include('test_php_file.php'); ?>
<html>
<head>
...
</html>

If your page prints out "FILE WAS INCLUDED" at the top of the page (assuming Apache parsed it first), then you are good to include the real php code in the same manner. 如果您的页面在页面顶部打印出“文件已包含”(假设首先对Apache进行了解析),那么以相同的方式包含真实的php代码将是一件好事。 Remember that file inclusion works similarly to inline image rendering: the string you pass include is the filename as well as the relative path. 请记住,文件包含与内联图像渲染的工作方式类似:传递的包含字符串是文件名以及相对路径。 Keep the php file within the same directory as the file that includes it so that way you don't have to worry about the path: 将php文件与包含它的文件放在同一目录中,这样您就不必担心路径了:

forms/
    form.html
    test_php_file.php
    real_php_file.php

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

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