Tag Archives: php

PHP Session locks

At work we have a page that loads multiple PHP scripts (in parallel), using XMLHttpRequest.

Later, we noticed that if one PHP script responds very slowly (i.e. when we put sleep(120) inside), all the others are waiting till the first one finishes loading. In other words: the load is serial, not parallel!

What? Why?!
A little research revealed an interesting phenomena: PHP session locks the session file till the session is closed. By default, the lock starts with session_start() call, and ends at the end of the PHP script!

Continue reading

Session cookie preserving; other browser window/tabs

Many websites use session-cookies (PHP session installs such a cookie, for example). Session-cookies have expiry time of zero, and the browser usually deletes them after closing its window. This is very useful for a 'log-in' session, like in your bank account, gmail, etc.

So all browsers delete the cookie as you close them, but what happens when you open a new browser window or tab? Does it also respect this temporary, session cookie? If you're logged in to gmail from one window, can you open a new browser window with another gmail account?

The answer is not surprising: that depends on your browser.

Continue reading

Time zones – how to do it right

After some struggling with how the Daylight Saving Time change affected my code, I realized that one shouldn't mess with the time zone offsets on his own (i.e. Add or Subtract hours manually from the UTC time), because underlying libs already implement it - and probably better (yes, including DST changes, works flawlessly!). Here are the new simple rules I set to myself:

  • Absolute time: Store dates only relative to UTC (usually [milli]seconds since Unix epoch or another reference date. The familiar Timestamps are measured as UTC and not as Local Time!
  • Absolute-to-Local: When local date matters, convert UTC-to-local (and vice versa) using the already-existing methods:
  • Absolute-to-somewhere-on-the-globe: instead of the OS-defined local time zone settings, PHP seems to have a way, but for JavaScript I haven't found anything unfortunately.

Bottom line: stick to UTC as much as you can, it's good for you.

Trivia line: Morocco is GMT+0 and has no Daylight Saving Time (so it's identical to UTC). Good for them, eh?