Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: header expires not working

My PHP code:

$expires_date = date('D, j F Y H:i:s', strtotime('now + 10 years')) . ' GMT';           
header("Expires: $expires_date");
header('Content-type: text/javascript');

echo 'hello world';

When I check the response headers, I see this:

Expires:Thu, 01 Jan 1970 00:00:00 GMT

What am I doing wrong?

UPDATE:

Was just experimenting, but it seems that I can't even unset Expires via header_remove('Expires');. I still see the 1970 date.

UPDATE:

My response headers:

Cache-Control:private
Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:74
Content-Type:text/javascript
Date:Wed, 17 Oct 2012 22:40:45 GMT
Expires:Thu, 01 Jan 1970 00:00:00 GMT
Keep-Alive:timeout=5, max=98
Server:Apache/2.2.21 (Win32) PHP/5.3.9
Vary:Accept-Encoding
X-Powered-By:PHP/5.3.9
like image 748
StackOverflowNewbie Avatar asked Oct 17 '12 13:10

StackOverflowNewbie


People also ask

How to add expires headers in PHP?

To use Header you need mod_headers. Eg run a2enmod and type headers then restart Apache. Note that you can just use gmdate('r') which provides a valid RFC 2822 representation.

What does expires header mean?

The Expires HTTP header contains the date/time after which the response is considered expired. Invalid expiration dates with value 0 represent a date in the past and mean that the resource is already expired.


1 Answers

look at your htaccess file:

<FilesMatch "\.(htm|html|php)$">
        Header set Expires "Thu, 01 Jan 1970 00:00:00 GMT"

        # TODO: Google.com's setting are the following
        # Expires -1
        # Cache-Control private, max-age=0
</FilesMatch>

it looks like your FilesMatch .php is overriding the .htaccess Content-Type:text/javascript rule and the PHP expires header because the script is a .php file.

Comment out this header expires in your .htaccess and see if the PHP header +10 year expires still gives the 1/1/1970 date

like image 83
WebChemist Avatar answered Nov 15 '22 10:11

WebChemist