当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > PHP Include

PHP
WINDOWS下安装MySQL
PHP 制作 网站/服务器 监视脚本
用PHP和CSS制作活动按钮
PHP 单件模式
PHP MVC模式,类封装以及HACK
PHP 中使用正则表达式
PHP 防止 SQL 注入攻击
PHP 跨站点脚本攻击
PHP 防止用户操纵 GET 变量
PHP 防止远程表单提交

PHP Include


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 1521 ::
收藏到网摘: n/a

Server Side Includes (SSI) are used to create functions, headers, footers, or elements that will be reused on multiple pages.
服务器端Includes[Server Side Includes (SSI)]的作用是:创建能够被多个页面使用的函数[functions]、页首[headers]、页脚[footers]、元素[elements]。


Server Side Includes
服务器端Include[Server Side Includes (SSI)]

You can insert the content of a file into a PHP file before the server executes it, with the include() or require() function. The two functions are identical in every way, except how they handle errors. The include() function generates a warning (but the script will continue execution) while the require() function generates a fatal error (and the script execution will stop after the error).
你可以使用include()或require()函数在服务器执行一个PHP文件之前将一段内容插入这个PHP文件。这两个函数的运行方式完全相同,不同点在于处理错误的方法。Include()函数遇到错误时生成一段警告[warning](但是脚本程序[script]将继续运行),然而require()函数则会生成一个“严重错误”的提示(并且错误发生后,脚本将停止执行)。

These two functions are used to create functions, headers, footers, or elements that can be reused on multiple pages.
这两个函数的作用是:创建能够被多个页面使用的函数[functions]、页首[headers]、页脚[footers]、元素[elements]。

This can save the developer a considerable amount of time. This means that you can create a standard header or menu file that you want all your web pages to include. When the header needs to be updated, you can only update this one include file, or when you add a new page to your site, you can simply change the menu file (instead of updating the links on all web pages).
这可以节省程序开发者思考的时间。它意味着你可以建立一套让所有页面内都包含的标准的页眉[header]或菜单[menu]文件。当页眉[header]需要升级时,你知要升级其中一个文件中的页眉[header],就可以了;或者当你需要向网站中加入新的页面时,你可以通过简单地改变菜单[menu]文件就可以了(无需再对所有的页面链接进行升级)。


The include() Function
include()函数

The include() function takes all the text in a specified file and copies it into the file that uses the include function.
include()函数可以把所有指定文件中的文本内容复制到使用“include”函数的文件中去。

Example 1
案例1

Assume that you have a standard header file, called "header.php". To include the header file in a page, use the include() function, like this:
假设你有一个标准的页眉[header]文件,名为“header.php”,使用include()函数把这个页眉文件加入一个页面。具体如下:

<html>
<body>
<?php include("header.php"); ?>
<h1>Welcome to my home page</h1>
<p>Some text</p>
</body>
</html>

Example 2
案例2

Now, let's assume we have a standard menu file that should be used on all pages (include files usually have a ".php" extension). Look at the "menu.php" file below:
现在,假定我们拥有一个需要被所有页面都使用的标准菜单[menu]文件(包含的文件通常含有“.php”扩展名)。这里的“menu.php”文件具体如下:

<html>
<body>
<a href="http://www.w3schools.com/default.php">Home</a> |

<a href="http://www.w3schools.com/about.php">About Us</a> |
<a href="http://www.w3schools.com/contact.php">Contact Us</a>

The three files, "default.php", "about.php", and "contact.php" should all include the "menu.php" file. Here is the code in "default.php":
这里的三个文件,"default.php"、 "about.php"、"contact.php"都应该包括“menu.php”文件。下面是“default.php”文件的代码:

<?php include("menu.php"); ?>
<h1>Welcome to my home page</h1>
<p>Some text</p>
</body>
</html>

If you look at the source code of the "default.php" in a browser, it will look something like this:
如果你在浏览其中查看"default.php"的源代码,你会看到下面的结果:

<html>
<body>
<a href="default.php">Home</a> |

<a href="about.php">About Us</a> |
<a href="contact.php">Contact Us</a>
<h1>Welcome to my home page</h1>
<p>Some text</p>

</body>
</html>

And, of course, we would have to do the same thing for "about.php" and "contact.php". By using include files, you simply have to update the text in the "menu.php" file if you decide to rename or change the order of the links or add another web page to the site.
当然,我们也应该对"about.php"和"contact.php"这两个文件做同样的操作。通过使用include文件,我们可以很轻松的对文件进行重命名、改变链接顺序或向网站中加入另一个页面来达到审计文件内容的目的。


The require() Function
Require()函数

The require() function is identical to include(), they only handle errors differently.
require()函数和include()的运行方式一样,唯一的不同点在于对错误的处理上。

The include() function generates a warning (but the script will continue execution) while the require() function generates a fatal error (and the script execution will stop after the error).
Include()函数遇到错误时生成一段警告[warning](但是脚本程序[script]将继续运行),然而require()函数则会生成一个“严重错误”的提示(并且错误发生后,脚本将停止执行)。

If you include a file with the include() function and an error occurs, you might get an error message like the one below.
如果你用include()函数来包含一个文件,当错误发生时,你会看到下面的错误信息:

PHP code:
PHP代码如下:

<html>
<body>

<?php
include("wrongFile.php");
echo "Hello World!";
?>

</body>
</html>

Error message:
错误信息:

Warning: include(wrongFile.php) [function.include]:
failed to open stream:
No such file or directory in C:homewebsitetest.php on line 5
Warning: include() [function.include]:
Failed opening 'wrongFile.php' for inclusion
(include_path='.;C:php5pear')
in C:homewebsitetest.php on line 5
Hello World!

Notice that the echo statement is still executed! This is because a Warning does not stop the script execution.
注意:错误产生时,“echo”语句仍在运行。这是因为使用Include()函数发生错误时,他不会停止运行脚本程序。

Now, let's run the same example with the require() function.
同样的,我们再举一个require()函数的例子:

PHP code:
PHP代码如下:

<html>
<body>

<?php
require("wrongFile.php");
echo "Hello World!";
?>

</body>
</html>

Error message:
错误信息:

Warning: require(wrongFile.php) [function.require]:
failed to open stream:
No such file or directory in C:homewebsitetest.php on line 5
Fatal error: require() [function.require]:
Failed opening required 'wrongFile.php'
(include_path='.;C:php5pear')
in C:homewebsitetest.php on line 5

The echo statement was not executed because the script execution stopped after the fatal error.
出现错误时“echo”语句停止执行。因为如果使用require()函数,当出现错误后,脚本停止运行。

It is recommended to use the require() function instead of include(), because scripts should not continue executing if files are missing or misnamed.
我们的建议是使用require()函数而不是include()函数。因为当文件丢失或出错时,脚本程序应该停止执行,这样的话比较好。

评论 (0) All

登陆 | 还没注册?