何时使用 index.php 而不是 index.html

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14208930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:51:45  来源:igfitidea点击:

when to use index.php instead of index.html

phpweb-applicationswebweb-application-projectweb-application-design

提问by qre0ct

I am relatively new to php. There is a very basic thing that has been troubling me. I understand that php is used to make web sites dynamic. I also understand that php is one of the many server side scripting languages that can be used to make dynamic web sites.

我对 php 比较陌生。有一件非常基本的事情一直困扰着我。我知道 php 用于使网站动态化。我也知道 php 是可用于制作动态网站的众多服务器端脚本语言之一。

However, what I do not understand is that when do i need to use an index.php page. Say for example if i have just a simple login page on my index page, it could very well just be a simple html page as well. Right? Then why would i want to make it index.php instead of index.html?

但是,我不明白的是我什么时候需要使用 index.php 页面。例如,如果我的索引页面上只有一个简单的登录页面,它也很可能只是一个简单的 html 页面。对?那我为什么要使用 index.php 而不是 index.html 呢?

An example of a sample situation would be great.

示例情况的示例会很棒。

回答by hek2mgl

You will have to choose the PHP extension (.php) when you want php code to be executed in the file. PHP code is code between the opening <?phpor <?and the closing ?>tags.

当您希望在文件中执行 php 代码时,您必须选择 PHP 扩展名 (.php)。PHP 代码是开始<?php<?结束?>标记之间的代码。

When no PHP code should be executed you can use the .html extension.

当不应该执行 PHP 代码时,您可以使用 .html 扩展名。

Usually when using the .php extension you are telling the web server, that it should use a php interpreter to process the file before it will be delivered to the browser. The php interpreter will then replace all content between the <?phpand ?>by the output of the PHP code. Just as if you wrote it manually. The processed file will then be delivered to the browser.

通常,当使用 .php 扩展名时,您是在告诉 Web 服务器,它应该使用 php 解释器来处理文件,然后才能将文件传送到浏览器。然后 php 解释器将用PHP 代码的输出替换<?php和之间的所有内容?>。就像您手动编写一样。处理后的文件将被传送到浏览器。

However, using the .php extension to tell the web server to process php code is configurable. If you want you can use other file extensions too.

但是,使用 .php 扩展名告诉 Web 服务器处理 php 代码是可配置的。如果需要,您也可以使用其他文件扩展名。

There is another thing that should be pointed out. When you only type the url path (without a filename) like :

还有一点需要指出。当您只输入 url 路径(没有文件名)时:

http://www.myserver.com/

there is an order of extensions (filenames) which the webserver (apache) searches for an index document. For example an apache config may contain a section like:

网络服务器 (apache) 搜索索引文档有一个扩展名(文件名)的顺序。例如,一个 apache 配置可能包含如下部分:

<IfModule mod_dir.c>
      DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>

Meaning that the index document is searched in the order above. This means if you place an index.html and a index.php in the same folder - and having the configuration above - always the index.htmlwould be delivered by the server.

这意味着按上述顺序搜索索引文档。这意味着如果您将 index.html 和 index.php 放在同一个文件夹中 - 并具有上述配置 - 总是索引。html将由服务器提供。

回答by LessQuesar

It will not hurt a website if you serve every page as .php. Apache is very fast to serve any php, let alone one which contains only static html.

如果您将每个页面都作为 .php 提供,它不会损害网站。Apache 为任何 php 提供服务都非常快,更不用说只包含静态 html 的了。

As a beginner you may find php will benefit you, by allowing you to creating simple templates. Site header and footer includes for instance could be written in one file, then included in all the other pages.

作为初学者,您可能会发现 php 会让您受益,因为它允许您创建简单的模板。例如,站点页眉和页脚包含可以写在一个文件中,然后包含在所有其他页面中。

回答by Devon Bernard

You can use which-ever you prefer: If you prefer keeping forms and basic pages that don't use data in HTML, and keep pages that use php in php format that is fine.

您可以使用您喜欢的任何一种:如果您更喜欢保留不使用 HTML 数据的表单和基本页面,并且保留使用 php 格式的 php 页面,那很好。

But one method I and I assume most others use is just make all of your pages php files. This is because you can include a html document in the php file and display it just the same. But you cannot do php queries from a html file so it is easy to just always use php just incase you want to add some php scripts to it.

但是我和我假设大多数其他人使用的一种方法就是制作所有页面的 php 文件。这是因为您可以在 php 文件中包含一个 html 文档并以相同的方式显示它。但是您不能从 html 文件进行 php 查询,因此很容易始终使用 php,以防万一您想向其中添加一些 php 脚本。

Index.php:

索引.php:

<?php
$var = 5;
?>
<html>
<head>
<title>My Page</title>
</head>
<body>
<h2>Your variable was <?php echo $var; ?></h2>
</body>
</html>

回答by Muhammad Talha Akbar

By default, the apache server needs a file with .php extension to parse the PHP code inside it. Though, if you wish, you may configure your server, by adding a single line to the configuration file, to use files with any extension with PHP code inside it. You can edit the apache by yourself to support php also in .HTML extension.

默认情况下,apache 服务器需要一个扩展名为 .php 的文件来解析其中的 PHP 代码。但是,如果您愿意,您可以通过在配置文件中添加一行来配置您的服务器,以使用带有任何扩展名的文件,其中包含 PHP 代码。您可以自己编辑 apache 以支持 .HTML 扩展名中的 php。

回答by Sankalp Mishra

Its not a big deal whether you use index.php or index.html. You ca use anyone of either. Only thing is you need PHP(or any other server side scripting language) to make your site dynamic.

无论您使用 index.php 还是 index.html,这都不是什么大问题。你可以使用任何一个。唯一的问题是您需要 PHP(或任何其他服务器端脚本语言)来使您的站点动态化。

Like you have a login page,you can surely make it as inde.html but your logics would either have to be in a different file or embedded in HTMl.

就像您有一个登录页面一样,您当然可以将其设为 inde.html,但您的逻辑要么必须在不同的文件中,要么必须嵌入到 HTMl 中。

回答by Arvind Bhardwaj

It is beacuse sometimes you may have some logic written on index.php. Like you may check if user is logged in or not and then redirect user to some specific page. Also you may do device based redirection as in case of mobile devices.

这是因为有时您可能会在index.php. 就像您可以检查用户是否已登录,然后将用户重定向到某个特定页面。您也可以像移动设备一样进行基于设备的重定向。

You can always choose to create index.htmlbut you do not know when youy may need to have some logic there.

你总是可以选择创建,index.html但你不知道什么时候你可能需要在那里有一些逻辑。

回答by Php Geek

In simple terms, you can easily access index.html file and get the data beneath it.But index.php is difficult to access. For your simple application index.html will do the trick. If you are planning for some big and secure application go for index.php

简单来说,你可以轻松访问index.html文件并获取其下的数据。但是index.php很难访问。对于您的简单应用程序,index.html 可以解决问题。如果您正在计划一些大型且安全的应用程序,请访问 index.php

回答by Zeus

I also have the same problem mate. But, I think .php is much better than .html since it is more dynamic plus you can do a bunch of cool stuff also.

我也有同样的问题队友。但是,我认为 .php 比 .html 好得多,因为它更具动态性,而且您还可以做很多很酷的事情。

回答by Maddocks

For example, in my index.phpor another main page I mainly use php because I use variables w/ them in the rest of my site:

例如,在我的index.php或另一个主页中,我主要使用 php,因为我在网站的其余部分使用了带有它们的变量:

<?php 
     $sitepath="http://www.example.com/public";
     $author="your name here";
?>

Because I <?php echo $sitepath ?>every time I link an image in my website so it doesn't break or something. Since I'm reusing the name all the time, I use .phpto be able to have that service, because if I use the name, I can change it globally.

因为我<?php echo $sitepath ?>每次在我的网站上链接图像时,它都不会损坏或其他东西。由于我一直在重复使用该名称.php,所以我过去可以使用该服务,因为如果我使用该名称,我可以全局更改它。

I think that simple pages like 404.html, aboutus.html, or ViewOneBlogPost.htmlcould be an HTML page, you might not need any functionality/variables for that.

我认为像404.htmlaboutus.htmlViewOneBlogPost.html可能是 HTML 页面这样的简单页面,您可能不需要任何功能/变量。

回答by aditya

To check current settings for file extension priority in apache2 with linux

使用 linux 检查 apache2 中文件扩展名优先级的当前设置

/etc/apache2/mods-enabled/dir.conf

/etc/apache2/mods-enabled/dir.conf