0

My situation
I am following the advice of Yahoo's YSlow and I would like to store my static content on a subdomain. However, I would like to use the function filemtime() to control the cache of the files.

The problem
The problem is that this function doesn't seem to be working on subdomains.

My question
Is there a way to achieve both of my goals? Or do you have a suggestion for me? Any solution or workaround would be highly appreciated.


Update
The function filemtime() is working on the subdomain now. I have made a mistake in typing the path. But it still ain't working on other domains.

The error is as follows:

Warning: filemtime() [function.filemtime]: stat failed for ../../otherdomain.com/file.css in /mywebsite/public_html/index.php on line 7

Michiel Pater
  • 22,377
  • 5
  • 43
  • 57

2 Answers2

2

You may have set cookies for domain.com, so they will be passed to sub.domain.com.

Can you use a whole new domain, e.g. cdndomain.com ?

As for using a file modified cache buster, try this...

<?php
$filename = 'script.js';
?>

<script type="text/javascript" src="<?php echo $filename . '?' . filemtime($filename); ?>"></script>

Then make sure script.js sends an expiry header way in the future (1 year is good enough).

Then, when you update this file, the versioning will change, breaking the cache and allowing the client to download the new copy.

alex
  • 479,566
  • 201
  • 878
  • 984
  • @alex: I can, but then I will still have this problem with the `filemtime()` function. – Michiel Pater Mar 04 '11 at 09:02
  • @alex: My problem is `The problem is that this function doesn't seem to be working on subdomains.`. I am linking from one domain to the other i.e. `$filename = 'http://otherdomain.com/script.js';`. And when I call the function `filemtime()` it gives an error. By the way, would it be a good idea to buy one domain to put multiple static content on for multiple websites? Something like `site1.mystaticdomain.com`? – Michiel Pater Mar 04 '11 at 09:07
  • @Michiel Do both domains point to the same server? If so, don't use the full URL, just the relative path (and adjust the `src` attribute accordingly). – alex Mar 04 '11 at 09:09
  • @alex: Yes, they do. I did use relative paths, but it didn't work either. Also, did you read my second question? – Michiel Pater Mar 04 '11 at 09:12
  • @Michiel What is your second question? – alex Mar 04 '11 at 09:15
  • @alex: `By the way, would it be a good idea to buy one domain to put multiple static content on for multiple websites? Something like site1.mystaticdomain.com?` – Michiel Pater Mar 04 '11 at 09:24
  • @alex: Thanks. I have also updated my question and added the error message. – Michiel Pater Mar 04 '11 at 09:33
  • 1
    @Michiel As I said before, use a relative path when using `filemtime()`, and the full URL in your `src` attribute. You may have url `fopen()` disabled in `php.ini` too, but this will be irrelevant if you use a relative path. – alex Mar 04 '11 at 09:40
  • @alex: I am unable to use a relative path to a different domain. I am getting the exact same error. – Michiel Pater Mar 04 '11 at 09:47
  • @Michiel I asked above *Do both domains point to the same server?* to which you replied *Yes, they do.*. So, unless you didn't mean to say that, you can use a relative path (or absolute, doesn't matter, just don't invoke the HTTP). – alex Mar 04 '11 at 09:51
  • @alex: They are on the same server and I am using a relative path, but it still doesn't work. – Michiel Pater Mar 04 '11 at 09:59
0

I have solved the problem by creating a PHP file on the other domain and sending a request to this file to check the file's modification time.

Michiel Pater
  • 22,377
  • 5
  • 43
  • 57