PH

Anybody know the most efficient way with PHP or Javascript to connect to a web server, to get the Last-Modified time of a JavaScript file? Either from the "Last-Modified" response header or some other method that doesn't involve downloading the whole remote file? I tried PHP's filemtime(), but I was getting some errors.

add a comment
0

10 Answers

  • Votes
  • Oldest
  • Latest
PH

Thanks, I didn't think of using HEAD.

I wrote a curl function that just connects to whatever URL I give it, and gets the headers. However, I don't know how to get JUST the last-modified header? It's in there if I just print the raw result, but not if I use curl_getinfo(). I need to pull out the last-modified header and then convert it to a nix timestamp, but surely there's gotta be a way to do it without some sort of preg-match function and text to int, date conversion that I don't even know how to do.

<?php
function check_url($url) {
    $c = curl_init();
    curl_setopt($c, CURLOPT_URL,$url);
    curl_setopt($c, CURLOPT_HEADER,1);//Include Header In Output
    curl_setopt($c, CURLOPT_NOBODY,1);//Set to HEAD & Exclude body
    curl_setopt($c, CURLOPT_RETURNTRANSFER,1);//No Echo/Print
    curl_setopt($c, CURLOPT_TIMEOUT,5);//5 seconds max, to get the HEAD header.
    $cURL_RESULT = curl_exec($c);
    if($cURL_RESULT !== FALSE)
    {
    return print_r(curl_getinfo($c),TRUE);
    }
 
return FALSE;
}?>

I still can't figure out how to get the Last-Modified header in timestamp format. I'm not sure if it's slower/faster either.

add a comment
0
PH

That's exactly what I needed. Thanks. Shows how much I convert dates from HTTP headers, 🤣 .

I found a solution to my problem. Maybe it will help one of you guys. I installed the PHP PECL HTTP extension, and wow is it nice. This is an example of getting the status code & last-modified header/timestamp of some Google Javascripts.

<?php
try {
    $pool = new HttpRequestPool(
        new HttpRequest('http://pagead2.googlesyndication.com/pagead/show_ads.js', HttpRequest::METH_HEAD),
        new HttpRequest('http://www.google-analytics.com/ga.js', HttpRequest::METH_GET)
    );
    $pool->send();
    foreach($pool as $request) {
        printf("%s is %s (%d) Last Modified: %s (%s)\n<br />",
            $request->getUrl(),
            $request->getResponseCode() ? 'alive' : 'not alive',
            $request->getResponseCode(),
            $request->getResponseHeader('Last-Modified'),
            strtotime($request->getResponseHeader('Last-Modified'))
        );
    }
} catch (HttpException $e) {
    echo $e;
}
?>

Apparently google analytics will return a 405, if you run a HEAD request to it, so I had to make it a GET request, sucks though, cause I think that still downloads the file AND it's throwing random "last-modified" dates at me, I think it's cause they have multiple servers or something though.

I also need to figure out how to close the connection it opens though. It's not closing them.

add a comment
0
BO

Awesome 😁 Glad you found the solution.

add a comment
0
JO
new HttpRequest('http://pagead2.googlesyndication.com/pagead/show_ads.js', HttpRequest::METH_HEAD),
		new HttpRequest('http://www.google-analytics.com/ga.js', HttpRequest::METH_GET)

raises eyebrow

What are you up to ?

  • 0
    WRONG 🙂 — PolishHurricane
add a comment
0
PH

Well, I used to host google adsense/analytics JavaScript locally, because it speeds things up. But they do modify their scripts every once in a while, and they have been modifying them a lot lately. So I temporarily stopped hosting them locally on my website. However, I noticed a few things...

With google adsense/analytics hosted locally, I managed a YSlow score of 93 on a good load, which isn't bad for a site with adsense/analytics on it, because they still run images/scripts on the google/doubleclick servers, even if the main JavaScript files are hosted locally. But when I linked to the JavaScript files externally. It knocked my score down to 70.

I noticed it can add (depending on what ads/stuff you get in 1 load), usually...

  • Around 6 external JavaScript files. (It used to be 3 or 4 on average, it seems they've added more)
  • Multiple images/other files
  • All of the included files have no "far future Expires or cache-control: max-age header:"
  • Some of the JS isn't minified. But usually just a 3 line doubleclick.net file they include (talk about a waste of an HTTP request).

So I just programmed a script, where if the local adsense/analytics JavaScript file hasn't been modified in the past 24 hours, it will send a request to google for those 2 JavaScript files using the PHP PECL HTTP extension, and if the 'Last-Modified' header of googles JavaScript files is newer (among a few other checks), it will save the content to my local JavaScript files, with a little PHP to give it the proper content type headers (incase google changes them, which they have).

At first I was going to use a HEAD request, then if they were modified, run a GET request to grab the JavaScript. However...

  • Analytics doesn't allow HEAD requests (I get an HTTP 405 code)
  • And I figured it was too much of an issue running a HEAD request, then a GET request for adsense because I was having problems.

So I just stuck with 1 GET request for each. The script is done, but I'm still playing with how to close the 2 connections from my server it opens, I'm gonna do that after I eat dinner.

I figured I'd just include the google js updater into my main site content with this...

<?php
if((rand()&100) % 100 == 0) //this should hopefully only run 1/100th of the time
{
//update google files
}?>
add a comment
0
BO

damn, you're too fast

add a comment
0
JO

I think you're being misled by yslow in this situation, though not intentionally.

There's a VERY good chance that the Google JS is going to already be cached in the visitors browser when they get to you. If you host it, there's a 100% chance that they will have to download it on their first visit.

I would ignore the yslow recomendations in this case.

add a comment
0
PH

Yeah I don't mind that, but it causes so many problems, including lagging my site if the Google server is slower, which happens a lot. Lately once in a while the ads haven't been loading either.

By any chance do you guys know how to close a connection using the PHP PECL HttpRequest class?

The connection was in an "ESTABLISHED" state on my server after my script finished. But, I managed to get the connection into "CLOSE_WAIT" state on my server by sending "Connection: close" with the HTTP header. However I still can't get the connection to close at all.

add a comment
0