PHP: header not working

My PHP code is:

$expires_date = date('D, j FYH: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 the following:

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

What am I doing wrong?

UPDATE:

Just experimented, but it seems like I can't even turn off Expires via header_remove('Expires'); . I still see the date of 1970.

UPDATE:

My answer 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 
+7
source share
3 answers

look at the htaccess file:

 <FilesMatch "\.(htm|html|php)$"> Header set Expires "Thu, 01 Jan 1970 00:00:00 GMT" # TODO: Google.com setting are the following # Expires -1 # Cache-Control private, max-age=0 </FilesMatch> 

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

The comment of this header expires in your .htaccess, and see if the PHP header continues +10 year, giving the date 1/1/1970

+5
source

You have formatting errors.

  • Use d instead of j
  • Use M instead of F
  • Use gmdate() instead of date()

From the definition of headers (14.21):

An example of its use is

  Expires: Thu, 01 Dec 1994 16:00:00 GMT Note: if a response includes a Cache-Control field with the max- age directive (see section 14.9.3), that directive overrides the Expires field. 

HTTP / 1.1 clients and caches MUST handle other invalid date formats, especially including a value of "0" as in the past (that is, "has already expired").

To mark the answer โ€œhas already expired,โ€ the source server sends an expiration date equal to the value of the date header. (See Rules for Calculating Shelf Life in Section 13.2.4.)

To mark the response as "never expires," the origin server sends an expiration date of approximately one year from the date the response was sent. HTTP / 1.1 servers MUST NOT send expiration dates of more than one year in the future.

Therefore, you should not send Expires with more than one year in the future. Instead, indicate that the ommit header never expires or use Expires: -1 .

+2
source

Try using:

// Re-terminate the headers

 header("Cache-Control: no-cache, must-revalidate"); 

// Then set the expiration date

 header("Expires: Sat, 26 Jul 2011 05:20:00 GMT"); // Date to expire 

he fixed my problem before

+1
source

All Articles