use your web server's access/authentication mechanism. for apache, create a .htpasswd file in the directory where the cgi scripts live. iis has same kind of control through the administrative interface. it's not a good idea to put your only security measure inside the cgi, as the permissions on the cgi are typically fairly lax, to allow everyone access to it through the web server.
the hardest line to type correctly is: stty erase ^H