in reply to if statement based on HTTP_REFERER

Yes, it's possible.
You will most probably need some kind of locking (see flock) in order to avoid, that simultaneous invocations do interfere. Have a look at a logging framework like i.e. Log::Log4perl. It will allow you to have diffent loggers, log-rotation, locking etc.

Update: You also need to check for empty/undefined HTTP_REFERER.