简体   繁体   English

设置过期标头而不在PHP中启用mod_expires或mod_headers可能吗?

[英]Set expires headers without enabling mod_expires or mod_headers possible in php?

I'm building a guestbook in php. 我正在用php建立留言簿。 When I checked the page with a SEO-checker it said that I should set expires headers. 当我使用SEO检查器检查页面时,它说我应该设置过期标题。 It's the very first time I'm working with expires headers and I've tried adding them in the .htaccess-file which failed as neither mod_expires nor mod_headers is on on the server and I won't be able to change it. 这是我第一次使用expires头文件,并且尝试将它们添加到.htaccess文件中,该文件失败了,因为服务器上既没有mod_expires也没有mod_headers,并且我将无法更改它。 My question is, if there is a possibility to set these expires headers in my php code without having mod_expires or mod_headers on? 我的问题是,是否有可能在我的php代码中设置这些有效的标头,而无需启用mod_expires或mod_headers? If so, how would I have to implement it in my code so that it'll work? 如果是这样,我将如何在我的代码中实现它才能起作用? And if you want to give me the answer "with header(....)" please explain how to set this up and where to put this, I don't just wanna copy-paste code, I want to learn how to do it. 如果您想给我答案“ with header(....)”,请解释如何设置它以及将其放置在哪里,我不仅要复制粘贴代码,还想学习如何做它。

I would like to set expires headers for images (jpg/jpeg/gif/png) as well (if possible) for my stylesheet (css). 我想为样式表(css)设置图片(jpg / jpeg / gif / png)的过期标头(如果可能)。

Down below I listed the questions on StackOverflow which I've already checked, but some of them are really hard to understand for a newbie like me or they never really get an answer. 在下面,我列出了我已经检查过的关于StackOverflow的问题,但是对于像我这样的新手来说,其中一些问题确实很难理解,或者它们从未真正得到答案。 Some of them never say where to implement the code snippets, which makes it hard to use an accepted answer. 他们中的一些人从来没有说过要在哪里实现代码段,这使得很难使用公认的答案。

I'm very new with programming web-pages, so please write explanations why I should do what, so I'll learn it for the future. 我是编程网页的新手,所以请写出为什么要做什么的解释,以便将来学习。 If you need any more information, please don't hesitate to write so in the comments. 如果您需要更多信息,请随时在评论中写下。 I'll try to answer whatever I can. 我将尽力回答。 Thank you. 谢谢。

(Already checked questions: (已检查的问题:

)

PHP-Version: 7.0.10 PHP版本:7.0.10

(PS: I don't know why mod_headers and mod_expires aren't on, but I'm sure that the company won't change it just for my small project.) (PS:我不知道为什么不启用mod_headers和mod_expires,但是我确信该公司不会仅出于我的小项目而更改它。)

You cannot set something with PHP where PHP is not involved in. Generally your webserver serve images and static files, so the webserver have to handle the expire headers for you. 您无法使用不涉及PHP的PHP进行设置。通常,您的Web服务器提供图像和静态文件,因此Web服务器必须为您处理expire头。 All other things, for example serve images with PHP is really not recommended and requires a lot more work than just configuring the webserver properly. 实际上,不建议所有其他事情,例如,使用PHP提供图像,并且比正确配置Web服务器需要更多的工作。

So, expire headers for static files (images, javascript, etc...) is managed by your webserver, not PHP. 因此,静态文件(图像,javascript等)的过期标头是由您的网络服务器而非PHP管理的。

Tip: On most default webhosting services is apache installed, so you can use a .htaccess file to set those things properly. 提示:在大多数默认的虚拟主机服务上,已安装apache,因此您可以使用.htaccess文件正确设置这些内容。

Edit, more explanation: You must difference between for what files you want to set expire headers. 编辑,更多说明:对于要设置过期标题的文件,您必须区别对待。 For sure, you can set expire header in your PHP files too but this only affect the pages that are served from PHP. 当然,您也可以在PHP文件中设置expire标头,但这只会影响PHP提供的页面。 And php is mostly used to display dynamic web pages, so an expire header here makes no sense. 而且php通常用于显示动态网页,因此,这里的expire标头没有任何意义。 Static images and all those files never get passed to PHP so you have to set expire in the webserver config. 静态映像和所有这些文件永远都不会传递给PHP,因此您必须在Web服务器配置中设置expire。 And because images and other static files are static files that not change (or not often) it is recommend to set expire header to allow the browser to cache it properly 并且由于图像和其他静态文件是不会更改(或不经常更改)的静态文件,因此建议设置到期标头以允许浏览器正确缓存它

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

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