etag??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

etag??

Post by Vegan »

been looking at etags, for conditional http get

any suggestions?
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: etag??

Post by requinix »

Suggestions for what?
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: etag??

Post by Vegan »

dealing with the ability to tell a search bot, no change etc
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: etag??

Post by requinix »

Suggestions for how to use an ETag? Well, you should be familiar with the specifications for ETag, If-Match, and If-None-Match, and entity tags in general.
Generate the ETag for a resource, before executing the rest of the request, and decide what to do from there: a matching If-Match means to continue else 412 (and stop), while a matching If-None-Match means 412 else to continue. Make sure to send the ETag regardless.

Can you try writing, like, at least two or three sentences of what you want? Throwing a handful of words in a post doesn't do a whole lot.
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: etag??

Post by Vegan »

I was wondering, would a md5 hash do the job to notice if a given page is modified to deal with it

http://www.microsoft.com/search/Tools/default.aspx
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: etag??

Post by requinix »

Sure, hash of the filename + modification time should cover most the bases. It depends on your needs: is the modification time alone enough to identify a version of a page?
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: etag??

Post by Vegan »

It would but I am not so sure how to massage that

I know how to display the modification date

Code: Select all

<?php echo "Last modified: " . date ("F d, Y (H:i:s)", getlastmod()); ?>
so how can i massage this into something the bot would like
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: etag??

Post by requinix »

Hash it.

Code: Select all

header("ETag: " . md5(date("YmdHis", getlastmod())));
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: etag??

Post by Vegan »

thanks it now recognizes an etag as desired, but now how about the conditional GET, or is that more of a server issue?
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: etag??

Post by requinix »

It's entirely server-side. If you're sending ETags then the client may send any of those If-*-Match headers I mentioned earlier (including a third one that I didn't list there). Check the links to see what you're supposed to do if you see them.
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: etag??

Post by Vegan »

the bot check recognizes the etag generated fine, but its still balking at conditional get

so is the bot sending me an etag that i need to check?
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: etag??

Post by requinix »

Clients don't send ETags. Your server does. They send If-*-Match headers.

For example, if you reply with "ETag: abc123" then their next request may include "If-None-Match: abc123"; if it does match then you respond with a 412 and no content. Or maybe they send "If-Match: abc123", and you give a 412 if it doesn't match.
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: etag??

Post by Vegan »

maybe something like this?

Code: Select all

function isModified($mtime, $etag) {
    return !( (
        isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
        && 
        strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $mtime
    ) || (
        isset($_SERVER['HTTP_IF_NONE_MATCH'])
        && 
        $_SERVER['HTTP_IF_NONE_MATCH'] == $etag
    ) ) ;
}
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: etag??

Post by requinix »

Yes, except you react differently for If-Modified-Since and If-None-Match: for the former you return a 304 Not Modified in case of failure and for the latter you return a 412. So you can't just bundle the two checks together like that.
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: etag??

Post by Vegan »

given I only want to tell a bot I am modified

isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])

so is the bot sending me an etag I need to handle?
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
Post Reply