in reply to CGI Auto Login

"...and have it log into my webmail automatically..."

And then what? Do you want 'the thing that logged you in' to just disappear and allow you access WebMail via your browser. Probably not going to work. Do you want to scrape each screen and deliver the contents via some proxy? That would work, but it seems uneccesary. I would definitely use WWW::Mechanize for that, but all of that work just so you can be lazy and not have to enter a password ... the payoff just isn't there for me. However, if you want to do this for your own education ... speed on! We will be glad to help you if you get stuck.

Update: code review time :)

  1. tuck your usage string into a sub, and call that sub instead of copying and pasting code. Remember, real coders use abstraction instead of copy-paste. But ... you can't supply a usage like that for a CGI script. Instead, one typically reports which required fields were missing (putting the message in red right next to the form field is a plus).
  2. your tests don't check for parameters that might only contain white space. A user name of " " is valid according to your code. Try something like:
    my $username = trim(param('username')); sub trim { my $dirty = shift; $dirty =~ s/^\s*//g; # remove leading whitespace $dirty =~ s/\s*$//g; # remove trailing whitespace return $dirty || ''; # return empty string if $dirty is undef }
  3. using multiple prints is a waste of typing, consider this:
    print q|<html> <head> <title>Logging in ...</title> </head> <body> |;
    looks much nicer, but now consider using CGI.pm:
    use CGI qw(:standard); print header, start_html('Logging in ...');
    I also recommend using HTML::Template, but you might not be ready for that yet. (it comes with it's own learning curve)
Oh, and welcome to the Monastery. :)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re: (jeffa) Re: CGI Auto Login
by cdubbs94 (Initiate) on May 06, 2003 at 19:40 UTC
    Well, basically, if I remove the 'print "' and the '";' at the end of each line and put it in an HTML file, the page will load and then it will automatically get redirected to my webmail. This is what I want, however I am getting an Internal Server Error when I try to run this. I am not too worried about error checking at this point, however I will eventually want to put some in. I would just like to get it running first. Any pointers on what might be wrong right now? I do not readily have access to the error logs, cause my hosting provider is cheap.

    #!/usr/bin/perl ##################################################################### # Function : # # Load Webmail using the password provided # # Use it : # # webmail?username=myusername&password=mypassword&domain=mydomain # ###################################################################### my $username = param('username'); my $password = param('password'); my $domain = param('domain'); my $correct_usage = "Correct Usage: webmail.cgi\?username=YourUser +name\&password=YourPassword\&domain=YourDomain"; if ($username eq ""){ print "No username given.\n $correct_usage" } elsif ($password eq ""){ print "No password given.\n $correct_usage" } elsif ($domain eq ""){ print "No domain given\n $correct_usage" } else{ print "<HTML> <HEAD> <TITLE>Logging In...</TITLE> </HEAD> <BODY> <CE_NTER><B_>Logging into your webmail...please wa +it...</B_></CEN_TER> <FORM name=f action=https://webmail.secureserver.net/s +rc/redirect.php method=post> <INPUT type=hidden name=login_username value=$user +name> <INPUT type=hidden name=domain value=$domain> <INPUT type=hidden name=secretkey value=$password> <INPUT type=hidden name=js_autodetect_results valu +e=0 > <INPUT type=hidden name=just_logged_in value=1> <INPUT type=hidden name=activate_timeout value=yes +> </FORM>"; <SCRIPT language=JavaScript>document.f.submit();</SCRIPT> </BODY></HTML>"; }
      Ahh, you are forgetting to print a content header. All CGI scripts must send a header so the web server will know how to deliver the content:
      print "Content-Type: text/html;\n\n";
      or better yet ... use CGI.pm!!!!!
      use CGI qw(:standard); print header;

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
        WOW, that was it. I have it working now. I think I will attempt to put some error checking in, but my main concern was just getting it running. Thanks so much jeffa!