Added some input and security checks

If this script is public facing I think there should be some added validation. This is probably a good set of measures to defend random hits against this script.

A couple of screwy line ending changes but they shouldn't effect anything.