when to use index.php instead of index.html

2019-01-13 08:11发布

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.

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?

An example of a sample situation would be great.

10条回答
萌系小妹纸
2楼-- · 2019-01-13 09:05

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

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

查看更多
姐就是有狂的资本
3楼-- · 2019-01-13 09:08

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.

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

查看更多
▲ chillily
4楼-- · 2019-01-13 09:09

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.

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.

查看更多
再贱就再见
5楼-- · 2019-01-13 09:09

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

<?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 .php to be able to have that service, because if I use the name, I can change it globally.

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.

查看更多
登录 后发表回答