Is there a way to determine the timezone for a user agent without javasript?
normally, i would execute this snippet of javascript:
<script type="text/javascript" language="JavaScript">
var offset = new Date();
document.write('<input type="hidden" id="clientTzOffset"
name="clientTzOffset" value="' + offset.getTimezoneOffset() + '"/>');
</script>
However, for a lot of mobiles and clients, JS is either not existant or turned off. Is there something clever I can do or am I reduced to "tell me where you are and what timzeone you are in?"
Use both. Have javascript set the default value of a list or text field. The percentage of people without javascript is so small the burden is tiny.
I was going to say "Use the request Date: header" but that's a response header only it seems.
I thought of another solution but it is a little complex. Set up some fake HEAD requests with a max-age: 1 response header to coerce the browser into re-fetching them. You should then receive an if-modified-since header from any modern browser like so:
If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT
Just be careful not to send any Last-Modified headers with the first response because
To get best results when sending an If-
Modified-Since header field for cache validation, clients are
advised to use the exact date string received in a previous Last-
Modified header field whenever possible.
Note the "whenever possible" disclaimer. This, and other parts of the header description imply that the client will use its own clock when it doesn't know anything of the servers.
With the right combination of headers this could actually work very well.
EDIT: Tried some tests with FF but couldn't find a valid combination of headers to trigger an if-modified-since in client time. FF only sends the header if it got a last-modified header previously and then it just reflects the value back (even if it isn't a valid date).
Maybe with a server side language you could do an IP lookup, and then determine from their country what timezone they could be in.
I'd imagine this could be problematic, though.
If going down this road, this question (and answers) may be of assistance.
Getting the location from an IP address
I've seen a website where instead of asking what timezone the user was in it simply asked "pick the correct time" and it showed them a list of times. It's a subtle difference from "what timezone are you in" but much easier to understand. Unfortunately I can't find the website anymore
Related
I'm having issues setting session cookies in Internet Explorer 3.0. Does anyone know if there is something special that needs to be done?
I'm doing with PHP, like so: setcookie('test', '1');
I'm not using a path or expiration time, is that necessary?
Perhaps someone has some old experience, or a 1997-1998 web development book laying around?
In case anyone is wondering why I'm bothering with this, because no-one uses IE3 anymore, and it's hard, yadda yadda, it's an art project.
Edit: Just to clarify, I have not tried setting cookies via JS yet, this is being done via HTTP.
I'm not sure the setCookie was around at this time. You could, however, use the older way of setting cookie which is
document.cookie = "someCookie=someCookievalue"
or
document.cookie += "someCookie=someCookievalue"
if you don't want to override cookie.
You can then retreive it using the document.cookie property. This will return a string of all the cookie. you can parse them using this function.
function parseCookie(cookiesString) {
var cookiesOutput = {};
var cookieKeysAndValue = cookiesString.split(';');
for(var i = 0; i < cookieKeysAndValue.length; i++){
var keyAndValue = cookieKeysAndValue[i].split('=');
cookiesOutput[keyAndValue[0]] = keyAndValue[1];
}
return cookiesOutput
}
document.cookie = 'someCookie=SomeCookieValue';
console.log(parseCookie(document.cookie));
I'm not 100% sure this will work since, you know, you are using Internet Explorer 3. But it is worth a shot.
If you need more information on that, you can consult the MDN doc
P.S. I tried to write something old javascript type as much as I could, but there still might have some errors. I already miss the modern browser feature.
So I've gotten to the bottom of this issue, and there were three causes for my confusion:
First, if you don't set a path= parameter for the cookie, IE3 will assume that the cookie is for this exact path only. This means that it will send back the same cookie when accessing /test.php again, but nowhere else on the site.
The solution is to include path=/ when setting the cookie.
Second, IE3 does not recognize cookies without an Expires parameter. The solution is to include Expires= in the Set-Cookie header, in "standard" cookie format.
The third is caused by the new Max-Age parameter, helpfully added by PHP for some reason, which IE3 does not recognize. Instead, it assumes everything up to and including Max-Age is the cookie name.
The solution is to set the cookie using PHP's header() function instead, like so:
header('Set-Cookie: test2=hi; expires=Tue, 02-Feb-2021 04:20:00 GMT; path=/');
(In this case, test2 is the name of the cookie, hi is the contents, and it expires about a year from today, on Feb 2nd.)
When you return to certain websites, sometimes they have popups or messages which show to inform users of new updates to the site or important changes.
I'm having issues understanding how these are implemented because these messages only show up once. How would a backend know that a user has already seen a backup and would it not be strenuous checking every time a page is returned to if a user has seen a certain message?
There are multiple methods which I have thought of to implement this functionality:
Storing a cookie which is checked by the backend to see if the user has seen a recent message or popup.
Have a table in a database named 'viewed_message' or something that states all the users that have seen a certain message or not.
How would a website go about creating one-time only popup/messages? Thanks.
Typically cookies are used for that sort of thing. Just check the existence of some firstVisit cookie and pop up the message if it's not there. Keep in mind this means if you clear your cookies you'll see the popup again.
edit:
This is an example from the Document.cookie docs on Mozilla:
function doOnce() {
if (document.cookie.replace(/(?:(?:^|.*;\s*)doSomethingOnlyOnce\s*\=\s*([^;]*).*$)|^.*$/, "$1") !== "true") {
alert("Do something here!");
document.cookie = "doSomethingOnlyOnce=true; expires=Fri, 31 Dec 9999 23:59:59 GMT";
}
}
I think you are on the way.
Cookies are there for that, create a cookie wich never expires and be happy.
If you want to use in wich others browsers, check if the cookies exists if dont look in table and store a cookie.
I've got a few maildirs I grabbed with getmail (both inbox and sent) and I want to give the eml files names that represent the date and time each mail has arrived (or was sent):
johnsmith#example.org-inbox-2015-07-28T20.02.14+0000.eml
(I want Windows to read the files, so no colons)
I've noticed that there is only one occurence of "Date: " inside the eml files:
Date: Tue, 28 Jul 2015 20:02:14 +0000
Can I rely on this piece of header to rename the files? Is it reliable?
(I plan to write a posix or bash script for this task)
Short answer: nope.
The Date header (and most of the other headers) is set by the client (and is not required), so it could be just anything or absent.
Spams appart, since the Date field is set by the MUA(/MSA) and still a lot of people are not synced with NTP or did not care to configure their working station properly, it is more than often wrong.
I also often see missconfigured automated mailer or MTA...
The date found in the Received headers is slightly more trustworthy because it is set by the realying MTA and the probability they are well configured is higher.
Note that except for the last one (the top-most in appearing order) which is your server (in your case GMail) they can be forged too.
It is a well known problem that IE caches too much of html, even when giving a Cache-Control: no-cache or Last-Modified header to everypage.
This behaiviour is really troubling when working with querystrings to get dynamic information, as IE considers it to be the same page (i.e.: http://example.com/?id=10) and serves the cached version.
I've solved it adding either a random number or a timestring to the querystring (as others have done) like this http://example.com/?id=10&t=2009-08-06_13:12:56 that I just ignore serverside.
Is there a better option? Is there another, cleaner way to acomplish this? I'm aware that POST isn't cached, but it is semanticaly correct to use GET here.
Assuming you are using jQuery, instead of using $.get or $.getJson, use the more generic $.ajax and explicitly set the cache value to false. The following is an example:
$.ajax({
url: "/Controller/Action",
cache: false,
type: "GET",
dataType: "json",
success: function(data, textStatus) {
alert("success");
}
});
A little more code required (not much though) than using .getJson or .get but will solve the problem cleanly without appending random numbers.
You could also use the current Unix Time in milliseconds to avoid the problem of many requests in one second (it is much less likely to have multiple requests in one millisecond)
var url = "http://whatever.com/stuff?key=value&ie=" + (new Date()).getTime();
Using a random number (not timestamp) on the querystring, or actually changing the filename are the two methods recommended. Steve Souders and YAHOO!'s performance group has published a ton of useful information and practices they've discovered and developed while optimizing one of the world's most heavily-visited properties.
So, in the end, the only reliable way to do this (thanks to IE6) is using a
random,
or
time bound
querystring.
You could use a
time bound querystring
that only changes every 15 seconds (or any other amount of time), so you'd lower the server hit count, as you'd see locally cached content for those 15 seconds.
If you have
a
standard
compliant
browser, you can get away with only using
ETags.
I have the same problem, but take care, in one second there can be many requests. This is why I use this:
$.getJSON("http://server/example?param=value&dummy=" + Math.random(), ...);
Have you tried adding an ETag header in the response?
You might use a random one, or a checksum of the generated page so that the cached version is served when appropriate.
I'm not sure what's the behaviour of IE, but with recent versions it should work.
See also the HTTP RFC section on ETag
Is there a way to get the Last-Modified-Date of a file on a Web Site?
i.e. Here is an example file I have out there:
http://www.ymcadetroit.org/atf/cf/%7B2101903E-A11A-4532-A64D-9D823368A605%7D/Birmingham_Youth_Sports_Parent_Manual.pdf
Go to the website you want to know about, wait for it to fully load, then go to the address bar and write this:
javascript:alert(document.lastModified)
You'll get a popup that says when it was last modified.
The HTTP intends the Last-Modified header field to declare the last modification date. But the server needs to know that date.
On static files whose content is sent directly to the client and not interpreted otherwise by the server (e.g. .html, .css, .js) it uses the last modified date of that file. But on files that generated content dynamically (PHP, Python, etc.) the script needs to specify that information itself. But unfortunatly many scripts don’t to that.
So if a Last-Modified header field is present, you can use that information. But if not, you cannot determin the last modification date.
Here is some C# code to do it:
public DateTime GetLastModifyTime(string url)
{
WebRequest request = WebRequest.Create(url);
request.Credentials = CredentialCache.DefaultNetworkCredentials;
request.Method = "HEAD";
using (WebResponse response = request.GetResponse())
{
string lastModifyString = response.Headers.Get("Last-Modified");
DateTime remoteTime;
if (DateTime.TryParse(lastModifyString, out remoteTime))
{
return remoteTime;
}
return DateTime.MinValue;
}
}
I realize this question is 4 years old, but a search of the web proved that satisfactory answers remain rare. Peter's answer is part of the solution. When I had the same problem to solve, that got me started. But the rest of the solution...
As he said, the web server must be configured to send the last-modified date ... so how do you configure the web server?
Assuming you have the necessary level of control, you first need to enable server side includes. There are several ways to do this - one of which is the "xbithack". A good reference is http://httpd.apache.org/docs/current/howto/ssi.html.
Assuming you've done this, you need to set the execute bit on any html file that needs to have server-side includes parsed. This can be done at the command line of a UNIX-like system: chmod u+x file.html or on the Mac using get-info (command-I) on the file.
This leaves the snippet to actually put in your file, which looks like this:
This document last modified <!--#flastmod file="index.html" -->
Since I found many, many recommendations that didn't include this, and simply used the javascript document.lastModified, I suspect that some servers give you what you want with the javascript version, whereas some (including the one hosting our stuff) don't.
To obtain the last modified date from client side, you can access the HTML DOM using the lastModified property using JavaScript.
The lastModified property grabs the information from the head portion sent with all web requests. The value can be manually set by developers on the web-server side of things so it may not reflect the actual last modified date of the file responsible for delivering the content.
Example
<!DOCTYPE html>
<html>
<body>
<b>document.lastModified : </b>
<script>document.write( document.lastModified );</script>
</body>
</html>
The specific command in JavaScript that retrieves this is document.lastModified and can easily be converted into a Date object as follows :
var x = new Date(document.lastModified);
More information can be found on the site I used as a reference w3 schools : HTML DOM lastModified Property
I believe the web server must be configured to send the last-modified date in an HTTP-header, this is certainly one way. Check out section 14.29 Last-Modified of this document:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
You can do the following to get Last-Modified:
https://superuser.com/a/991895
Using curl:
curl -s -v -X HEAD http://foo.com/bar/baz.pdf 2>&1 | grep '^< Last-Modified:'
Using wget:
wget --server-response --spider http://example.com/bar/example.pdf 2>&1 | grep -i Last-Modified
With just plain HTML, no you cannot.
You can with PHP, or ASP, or any other server side language.
I'm not an expert in headers, but believe you are looking for this:
There is a way to check the date when a file was modified:
View HTTP headers in Google Chrome?
Check in there (Chrome's Developer Tools / Network / Selected File / Headers) the "If-Modified-Since" variable.
Until now this has helped me to achieve what you are asking, get a file's modification date.
In php:
print getlastmod();
print gmdate('D, d M Y H:i:s', getlastmod());