简体   繁体   English

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

[英]when to use index.php instead of index.html

I am relatively new to php.我对 php 比较陌生。 There is a very basic thing that has been troubling me.有一件非常基本的事情一直困扰着我。 I understand that php is used to make web sites dynamic.我知道 php 用于使网站动态化。 I also understand that php is one of the many server side scripting languages that can be used to make dynamic web sites.我也知道 php 是可用于制作动态网站的众多服务器端脚本语言之一。

However, what I do not understand is that when do i need to use an index.php page.但是,我不明白的是我什么时候需要使用 index.php 页面。 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.例如,如果我的索引页面上只有一个简单的登录页面,它也很可能只是一个简单的 html 页面。 Right?对? Then why would i want to make it index.php instead of index.html?那我为什么要使用 index.php 而不是 index.html 呢?

An example of a sample situation would be great.示例情况的示例会很棒。

You will have to choose the PHP extension (.php) when you want php code to be executed in the file.当您希望在文件中执行 php 代码时,您必须选择 PHP 扩展名 (.php)。 PHP code is code between the opening <?php or <? PHP 代码是<?php<? and the closing ?> tags.和结束?>标签。

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.通常,当使用 .php 扩展名时,您是在告诉 Web 服务器,它应该使用 php 解释器来处理文件,然后才能将文件传送到浏览器。 The php interpreter will then replace all content between the <?php and ?> by the output of the PHP code.然后,php 解释器将用 PHP 代码的输出替换<?php?>之间的所有内容。 Just as if you wrote it manually.就像您手动编写一样。 The processed file will then be delivered to the browser.处理后的文件将被传送到浏览器。

However, using the .php extension to tell the web server to process php code is configurable.但是,使用 .php 扩展名告诉 Web 服务器处理 php 代码是可配置的。 If you want you can use other file extensions too.如果需要,您也可以使用其他文件扩展名。

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.网络服务器 (apache) 搜索索引文档有一个扩展名(文件名)的顺序。 For example an apache config may contain a section like:例如,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.这意味着如果您将 index.html 和 index.php 放在同一个文件夹中 - 并具有上述配置 - 总是索引。 html would be delivered by the server. html将由服务器提供。

It will not hurt a website if you serve every page as .php.如果您将每个页面都作为 .php 提供,它不会损害网站。 Apache is very fast to serve any php, let alone one which contains only static html. Apache 为任何 php 提供服务都非常快,更不用说只包含静态 html 的了。

As a beginner you may find php will benefit you, by allowing you to creating simple templates.作为初学者,您可能会发现 php 会让您受益,因为它允许您创建简单的模板。 Site header and footer includes for instance could be written in one file, then included in all the other pages.例如,站点页眉和页脚包含可以写在一个文件中,然后包含在所有其他页面中。

Its not a big deal whether you use index.php or index.html.无论您使用 index.php 还是 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.唯一的问题是您需要 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 中。

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.但是我和我假设大多数其他人使用的一种方法就是制作所有页面的 php 文件。 This is because you can include a html document in the php file and display it just the same.这是因为您可以在 php 文件中包含一个 html 文档并以相同的方式显示它。 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.但是您不能从 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 default, the apache server needs a file with .php extension to parse the PHP code inside it .默认情况下,apache 服务器需要一个扩展名为 .php 的文件来解析其中的 PHP 代码 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.但是,如果您愿意,您可以通过在配置文件中添加一行来配置您的服务器,以使用带有任何扩展名的文件,其中包含 PHP 代码。 You can edit the apache by yourself to support php also in .HTML extension.您可以自己编辑 apache 以支持 .HTML 扩展名中的 php。

It is beacuse sometimes you may have some logic written on index.php .这是因为有时你可能在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.您也可以像移动设备一样进行基于设备的重定向。

You can always choose to create index.html but you do not know when youy may need to have some logic there.你总是可以选择创建index.html但你不知道什么时候你可能需要在那里有一些逻辑。

In simple terms, you can easily access index.html file and get the data beneath it.But index.php is difficult to access.简单来说,你可以很容易地访问index.html文件并获取它下面的数据。但是index.php很难访问。 For your simple application index.html will do the trick.对于您的简单应用程序,index.html 可以解决问题。 If you are planning for some big and secure application go for index.php如果您正在计划一些大型且安全的应用程序,请访问 index.php

I am relatively new to php.我对 php 比较陌生。 There is a very basic thing that has been troubling me.有一件非常基本的事情一直困扰着我。 I understand that php is used to make web sites dynamic.我知道 php 用于使网站动态化。 I also understand that php is one of the many server side scripting languages that can be used to make dynamic web sites.我也知道 php 是可用于制作动态网站的众多服务器端脚本语言之一。

However, what I do not understand is that when do i need to use an index.php page.但是,我不明白的是我什么时候需要使用 index.php 页面。 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.例如,如果我的索引页面上只有一个简单的登录页面,它也很可能只是一个简单的 html 页面。 Right?正确的? Then why would i want to make it index.php instead of index.html?那我为什么要使用 index.php 而不是 index.html 呢?

An example of a sample situation would be great.示例情况的示例会很棒。

For example, in my index.php or 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.因为我<?php echo $sitepath ?>每次我在我的网站上链接图像时,它都不会<?php echo $sitepath ?>或其他东西。 Since I'm reusing the name all the time, I use .php to be able to have that service, because if I use the name, I can change it globally.因为我一直在重用这个名字,所以我使用.php来获得那个服务,因为如果我使用这个名字,我可以全局更改它。

I think that simple pages like 404.html , aboutus.html , or ViewOneBlogPost.html could be an HTML page, you might not need any functionality/variables for that.我认为像404.htmlaboutus.htmlViewOneBlogPost.html这样的简单页面可能是 HTML 页面,您可能不需要任何功能/变量。

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

Well you can use HTML if you have a simple few pages where the code doesn't repeat itself a lot.好吧,如果您有几个简单的页面,其中代码不会重复很多,那么您可以使用 HTML。 But trust me, if you have a whole website in HTML with more than a thousand lines per file, you will want to use PHP.但是相信我,如果你有一个 HTML 格式的网站,每个文件超过一千行,你会想要使用 PHP。 Why?为什么? Because of require and include .因为requireinclude These will clean up your code when you need to use layouts, so you don't need to keep copying the hole code of your layout header, footer etc... It can be really difficult to maintain when you need to insert content and there's a lot of code in the way.当您需要使用布局时,这些将清理您的代码,因此您无需继续复制布局页眉、页脚等的孔代码......当您需要插入内容时,维护起来真的很困难,而且还有很多代码的方式。

Here's some documentation about it这里有一些关于它的文档

https://www.php.net/manual/en/function.include.php https://www.php.net/manual/en/function.include.php

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

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