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 example.com/ example.com/stuff, and example.com/stuff/cool/12 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 example.com/stuff/cool/image.png, 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.
Imagine a script executed in a dispatch-method like manner (so calls to example.com/ example.com/stuff, and example.com/stuff/cool/12 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 example.com/stuff/cool/image.png, 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
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.
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.
ofcourse, if you have any, your favicon should be inhere too
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.
# 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.
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.
did you know about cygwin(www.cygwin.com) and mingw(www.mingw.org)?
so, tail is available for windows
so, tail is available for windows
If I understand your file watching requirements properly, on Linux you could hook into inotify - see e.g. http://inotify-tools.sourceforge.net/
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. http://inotify-tools.sourceforge.net/api/inotifytools_8h.html and two-thirds of the way down http://inotify-tools.sourceforge.net/
David
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. http://inotify-tools.sourceforge.net/api/inotifytools_8h.html and two-thirds of the way down http://inotify-tools.sourceforge.net/
David
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.
Dispatch scripts are a blessing and a curse