Apr 25 2010

This has always bugged me (scroll right to see the whole thing):

[Tue Apr 20 01:06:47 2010] [error] [client 192.168.5.84] [WAR] 7d (lib/web.php:916) Undefined variable: message, referer: http://mahara-test/admin/extensions/test.php?
[Tue Apr 20 01:06:47 2010] [error] [client 192.168.5.84] Call stack (most recent first):, referer: http://mahara-test/admin/extensions/test.php?
[Tue Apr 20 01:06:47 2010] [error] [client 192.168.5.84]   * log_message("Undefined variable: message", 8, true, true, "/home/nigel/src/mahara-test/htdocs/lib/web.php", 916) at /home/nigel/src/mahara-test/htdocs/lib/errors.php:446, referer: http://mahara-test/admin/extensions/test.php?
[Tue Apr 20 01:06:47 2010] [error] [client 192.168.5.84]   * error(8, "Undefined variable: message", "/home/nigel/src/mahara-test/htdocs/lib/web.php", 916, array(size 2)) at /home/nigel/src/mahara-test/htdocs/lib/web.php:916, referer: http://mahara-test/admin/extensions/test.php?
[Tue Apr 20 01:06:47 2010] [error] [client 192.168.5.84]   * json_reply(false) at /home/nigel/src/mahara-test/htdocs/admin/extensions/test.json.php:22, referer: http://mahara-test/admin/extensions/test.php?
[Tue Apr 20 01:06:47 2010] [error] [client 192.168.5.84] , referer: http://mahara-test/admin/extensions/test.php?

I like using log files to display messages instead of the screen, but somewhere along the way somebody decided that the 'referer' (sic) was needed in the logs too, and that makes them totally unreadable.

Here's my fix, when tailing a log file:

nigel@mahara-test:~$ sudo tail -f /var/log/apache2/mahara-test.error.log | perl -pe 's/, referer:.*//'

And, instantly:

[Tue Apr 20 01:06:47 2010] [error] [client 192.168.5.84] [WAR] 7d (lib/web.php:916) Undefined variable: message
[Tue Apr 20 01:06:47 2010] [error] [client 192.168.5.84] Call stack (most recent first):
[Tue Apr 20 01:06:47 2010] [error] [client 192.168.5.84]   * log_message("Undefined variable: message", 8, true, true, "/home/nigel/src/mahara-test/htdocs/lib/web.php", 916) at /home/nigel/src/mahara-test/htdocs/lib/errors.php:446
[Tue Apr 20 01:06:47 2010] [error] [client 192.168.5.84]   * error(8, "Undefined variable: message", "/home/nigel/src/mahara-test/htdocs/lib/web.php", 916, array(size 2)) at /home/nigel/src/mahara-test/htdocs/lib/web.php:916
[Tue Apr 20 01:06:47 2010] [error] [client 192.168.5.84]   * json_reply(false) at /home/nigel/src/mahara-test/htdocs/admin/extensions/test.json.php:22
[Tue Apr 20 01:06:47 2010] [error] [client 192.168.5.84]

Much better! But we can do better again:

nigel@mahara-test:~$ sudo tail -f /var/log/apache2/mahara-test.error.log | perl -pe 's/\[error\]\s+\[client.*?\]\s+//; s/, referer:.*//'

And we end up with:

[Tue Apr 20 01:06:47 2010] [WAR] 7d (lib/web.php:916) Undefined variable: message
[Tue Apr 20 01:06:47 2010] Call stack (most recent first):
[Tue Apr 20 01:06:47 2010]   * log_message("Undefined variable: message", 8, true, true, "/home/nigel/src/mahara-test/htdocs/lib/web.php", 916) at /home/nigel/src/mahara-test/htdocs/lib/errors.php:446
[Tue Apr 20 01:06:47 2010]   * error(8, "Undefined variable: message", "/home/nigel/src/mahara-test/htdocs/lib/web.php", 916, array(size 2)) at /home/nigel/src/mahara-test/htdocs/lib/web.php:916
[Tue Apr 20 01:06:47 2010]   * json_reply(false) at /home/nigel/src/mahara-test/htdocs/admin/extensions/test.json.php:22
[Tue Apr 20 01:06:47 2010]

Hooray! Readable PHP log files.

You don't want to have to remember to type all of that, so for best results, put it in a script, chmod +x and you have:

nigel@mahara-test:~$ sudo tail -f /var/log/apache2/mahara-test.error.log | phptail

You could even combine it with Monkeytail to make viewing the logs even easier ;).

I've trawled through the PHP codebase looking for where it adds the referer in a vain attempt to change it, or provide a patch to make it a configuration option. Sadly, I never found it. If anyone finds out, please let me know!

Thanks to Martyn for the original idea, and assisting with the arcane perl stuff.

Like this post? Subscribe to my RSS feed and follow me on twitter to hear about new posts early.

Want to share this post?

Oct 13 2009

From http://nz2.php.net/manual/en/language.types.integer.php

"An integer is a number of the set Z = {..., -2, -1, 0, 1, 2, ...}."

Yes, that's nice. Except, this definition of integers does not seem to apply in PHP land. I respectfully request you change this definition to the following:

"In PHP, an integer is a number of the set Z = {-2^32, ... , -1, 0, 1, ..., 2^32-1}. This is markedly smaller than the mathematical definition of integers. Beware!"

All kidding aside, I don't care how big the underlying C datatype is. I want my integers to behave like actual, mathematically defined integers. Which means that a number > 2GB is not actually some horribly large negative number.

Oh coding god in the sky, why didn't python catch on as the newb language of choice? [1]

Edit: Benny Baumann pointed out to me that the size is 2^64 on a 64 bit system, which I grant is larger, but is still retarded. It so happens that many people are running 32 bit web servers for whatever reason. He also found this gem in the PHP source code:

#ifndef LONG_MAX
#define LONG_MAX 2147483647L
#endif
#ifndef LONG_MIN
#define LONG_MIN (- LONG_MAX - 1)
#endif

Ahem.

[1]I feel at pains to point out that I don't choose to code in PHP because I'm a newb. Far from it, in fact. I'm coding in php because Mahara is written in PHP, and as an OSS webapp, it makes sense to code these things in PHP so newbs can hack on it and install it on annoying shared hosting.

Like this post? Subscribe to my RSS feed and follow me on twitter to hear about new posts early.

Want to share this post?

Nov 24 2008

From my testing on PHP 5.2.0, here is a little information about these two ini settings:

  1. upload_max_filesize refers to the size of the file (no surprise)
  2. post_max_size refers to the size of the post data - also no surprise, but this means the size of the post data not including any on-the-wire encoding.
  3. From 1 and 2, you can assume that post_max_size only needs to be a small amount bigger than upload_max_filesize for a file upload to work, even for large files [1]. BUT, if you need to upload more than one file on any given form, post_max_size will have to be more than double the size, and so on for more files.
  4. Despite what the php.net manual says, the uploaded files are NOT loaded into memory, and so it seems that memory_limit has no effect on whether you can or cannot upload files.

Based on these observations, if your application only allows one file to be uploaded in any one request, and you only want files of up to 50M in size, then safe settings are upload_max_filesize = 50M and post_max_size = 55M. If your application allows multiple files to be uploaded in one request - e.g. you have a mass file upload page that allows 10 files to be uploaded - then more sensible settings might be upload_max_filesize = 50M and post_max_size = 505M.

[1]For a small form, post_max_size only needs to be about 2K bigger for the maximum sized file to be uploaded. Don't forget if you give people a textarea to write in as well they could go on and on and on and on and on and need a bigger limit.

Like this post? Subscribe to my RSS feed and follow me on twitter to hear about new posts early.

Want to share this post?