Yet another hour burned debugging some weird issue. Let me give you the short version.

Imagine a script executed in a dispatch-method like manner (so calls to, and all go to the same script). It stores the value (12 in the last example) in a session for later use. Printing out that value on the page, doing weird things with it, etc. all work. You always get 12. Even printing out the value from the session as the last thing in the script then exiting, you still get 12. Load up another page, you get 0.

Is that even possible?

It turns out what was happening was my script was displaying information in a template, that was including another resource (say <img src=”image.png” />). The browser was obligingly requesting, which was re-executing the script, casting image.png over to an integer, resulting in a 0.

The fundamental stumbling block in my diagnosis of the issue was that I saw my browser making two requests, the first when I was being presented with a 12, the second when I was shown a 0. In fact my browser was making three requests, the first directly at my request for the page (setting the value), the second by my implied request to retrieve a referenced image (resetting the value), the third for the page showing zero.

Things that helped me debug the issue: IRC, (Daniel Banck in #zftalk), and Christine

Things I should have used, Firebug or Dragonfly to watch the requests go by.

Something I would love to have: An application that watched a file on disk, reporting all versions of the file over time. tail -f wont work as the file is a single line long, the line is changed but no new lines are added. I’m aware of similar tools for windows, none for mac/linux.

Comments »

No Trackbacks
I had a similar issue when were working on a CakePHP site: the login script was supposed to redirect to the page you attempted to access but it kept redirecting to the index. Took us about 30 minutes to realize it was because we had deleted the favorite icon without removing the reference in the template so Cake was setting the last visited page to the address of the favorite icon (which didn't physically exist), and Cake decided to redirect to the index for us.

Dispatch scripts are a blessing and a curse :-|
#1 James Logsdon (Homepage) on 2009-01-03 03:36 (Reply)

There is a really simple trick to solve this:

Put all your images in an /images/ directory, and your static scripts in a /scripts/ directory.
Always link to those absolute paths (base href is handy here ;-) ) and exclude them from beging url-rewritten in .htaccess:

RewriteEngine On

RewriteRule ^scripts/ - [L] [OR] #do not apply to any direct requests to /scripts OR
RewriteRule ^images/ - [L] #do not apply to any direct requests to /images

RewriteRule ^.* index.php #rewrite the rest to index.php or your controller.
#2 SchizoDuckie (Homepage) on 2009-01-03 03:52 (Reply)

ofcourse, if you have any, your favicon should be inhere too :-P
#3 SchizoDuckie (Homepage) on 2009-01-03 03:53 (Reply)

This is why we have -f

# Do not enable rewriting for files or dirs that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php/$1 [PT,L]

There's really no need to send every request through PHP.
#4 Martin Fjordvald on 2009-01-03 17:58 (Reply)

Actually, that's a matter of opinion ;-) My PHP also handles 404s, etc. And it's an extra security layer to the rest of your directories.
#5 SchizoDuckie (Homepage) on 2009-01-03 18:47 (Reply)

did you know about cygwin( and mingw(
so, tail is available for windows
#6 Alexandr (Homepage) on 2009-01-05 07:28 (Reply)

If I understand your file watching requirements properly, on Linux you could hook into inotify - see e.g.

I've seen various Perl scripts written in the past which hook in at a lower level - but the inotify-tools looks like it's easier to use.

You can track file opening, reading, updating, deletion, creation etc.

See e.g. and two-thirds of the way down

#7 David Goodwin (Homepage) on 2009-01-05 19:20 (Reply)

the tamper data extension for firefox is also really helpful when checking out http requests and whatnot, it has definitely saved my bacon a few times.
#8 kenrick (Homepage) on 2009-01-06 16:42 (Reply)

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.

Hi, I’m Paul Reinheimer, a developer working on the web.

I co-founded WonderProxy which provides access to over 200 proxies around the world to enable testing of geoip sensitive applications. We've since expanded to offer more granular tooling through Where's it Up

My hobbies are cycling, photography, travel, and engaging Allison Moore in intelligent discourse. I frequently write about PHP and other related technologies.