Keep in mind that scripts run from a web server run as the userid of the server (unless the setuid bit is set, and the server allows such things). It also may not be using the home directory you expect. Since you have removed the error-checking, I cannot see what you are currently checking for.
fnord